Skip to content

Commit

Permalink
Merge branch 'main' into Andrew
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew0022 authored Nov 30, 2023
2 parents 68da194 + 5a0af37 commit bad50e6
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 61 deletions.
18 changes: 11 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ services:
- backend
command: python app.py # command to start up the front-end once the container is up and running


mongodb:
image: mongodb/mongodb-community-server:6.0-ubi8
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=pass
volumes:
- type: bind
source: ./data
target: /data/db

backend:
build:
ports:
-
depends_on:
- db
command: python machine_learning_client.py
build: ./machine-learning-client

2 changes: 1 addition & 1 deletion machine-learning-client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ COPY . .

RUN pip install -r requirements.txt

CMD ["python"]
CMD ["python", "machine_learning_client.py"]
129 changes: 76 additions & 53 deletions machine-learning-client/machine_learning_client.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,89 @@
import os
from pymongo import MongoClient
import pymongo
import datetime
"""
Machine Learning python file providing conversion of audio
to text transcription and analysis of audio transcription
"""
from enum import Enum
import pyaudio
import speech_recognition as sr
import sys

r = sr.Recognizer()
pa = pyaudio.PyAudio()
def listAllMic():
"""
List all available microphone device
"""
if (len(sr.Microphone.list_microphone_names()) == 0):
print("no device available")
return

for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))

def record_microphone():
listAllMic() #Use for debug
mc = None
try:
mc = sr.Microphone()
print("Mic init successful")
except OSError:
mc = sr.Microphone(0)
print("no default mic")


with mc as source:
print("Please give your answer:")
audio = r.listen(source)
return audio
class ML:
"""Machine Learning class functions """
pa = pyaudio.PyAudio()

def audio_to_text(audio):
try:
transcription = r.recognize_google(audio)
print(transcription)
print(type(transcription))
except sr.UnknownValueError:
print("Sorry, we could not recognize your response.")
except sr.RequestError as e:
print("Sorry, there appears to be an error with Google Speech to Text")
def list_all_mic(self):
"""
List all available microphone device
"""
if len(sr.Microphone.list_microphone_names()) == 0:
print("no device available")
return

def grade_response(transcription):
"""
Give out a score based on the transcribed audio
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print(f'Microphone with name "{name}"')
print(f" found for `Microphone(device_index={index})`")

def record_microphone(self):
"""Function for recording microphone input"""
ML.list_all_mic(self) # Use for debug
mc = None
try:
mc = sr.Microphone()
print("Mic init successful")
except OSError:
mc = sr.Microphone(0)
print("no default mic")
with mc as source:
print("Please give your answer:")
r = sr.Recognizer()
audio = r.listen(source)
return audio

def audio_to_text(self, audio_file_string):
"""Function for converting audio file to text transcription"""
r = sr.Recognizer()
with sr.AudioFile(audio_file_string) as source:
ad = r.listen(source)
try:
transcription = r.recognize_google(ad)
print(transcription)
print(type(transcription))
except sr.UnknownValueError:
print("Sorry, we could not recognize your response.")
except sr.RequestError:
print("Sorry, there appears to be an error with Google Speech to Text")
return transcription

class BuzzWord(Enum):
"""List of Buzzwords"""
HELLO = "hello"
UGH = "ugh"

def grade_response(self, transcription):
"""
Give out a score based on the transcribed audio
Args:
transcription (str): transcribed audio
"""
print("working on it...")
grade = "Your response had "
for bw in ML.BuzzWord:
bw_count = (str)(transcription.lower().count(bw.value))
grade += bw.value + " appearing " + bw_count + " times "
return grade

Args:
transcription (str): transcribed audio
"""
print("working on it...")


def main():
"""Main Method"""
print("Tell me a little bit about yourself")
audio = record_microphone()
transcription = audio_to_text(audio)
result = grade_response(transcription)
ml = ML()
audio = ml.record_microphone()
transcription = ml.audio_to_text(audio)
result = ml.grade_response(transcription)
print(result)


if __name__ == "__main__":
main()
main()
Binary file added machine-learning-client/tests/Recording.aiff
Binary file not shown.
Empty file.
34 changes: 34 additions & 0 deletions machine-learning-client/tests/ml_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
tests for machine-learning-client
"""
from machine_learning_client import ML




class Tests:
""" Tests class for ml_tests.py """
def test_audio_text_conversion(self):
"""testing audio_to_text function in ML with valid audiofile as input"""
actual = ML.audio_to_text(self,
r"C:\Users\Administrator\Desktop\NYU_Undergrad\SoftwareEngineering"
r"\4-containerized-app-exercise-rizzballs\machine-learning-client\tests\richard.aiff"
)
assert isinstance(
actual, str
), f"Expected audio_to_text to return a string. Instead it returned a {actual}"
assert (
actual.lower() == "my name is richard"
), f"Expected the converted audio to be 'my name is richard'. Instead it returned {actual}"

def test_grade_response(self):
"""testing grade_response function in ML with a transcription with 5 hellos"""
actual = ML.grade_response(self, "hello hello hello hello hello")
assert isinstance(
actual, str
), f"Expected grade_response to return a string. Instead it returned a {actual}"
assert (
actual.lower()
== "your response had hello appearing 5 times ugh appearing 0 times "
),f"Expected the result to be 'your response had hello appearing 5 times \
ugh appearing 0 times '. Instead it returned {actual}"
Binary file added machine-learning-client/tests/richard.aiff
Binary file not shown.
12 changes: 12 additions & 0 deletions web-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM python:3.11
WORKDIR /app
ADD . /app

RUN pip install --trusted-host pypi.python.org -r requirements.txt

EXPOSE 80

# Define an environment variable... this will be available to programs running inside the container
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
1 change: 1 addition & 0 deletions web-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ def analyzeData():
if __name__ == "__main__":
PORT = os.getenv('PORT', 5000)
app.run(debug=True, port=PORT)

2 changes: 2 additions & 0 deletions web-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
pymongo

0 comments on commit bad50e6

Please sign in to comment.