generated from nyu-software-engineering/containerized-app-exercise
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
137 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ COPY . . | |
|
||
RUN pip install -r requirements.txt | ||
|
||
CMD ["python"] | ||
CMD ["python", "machine_learning_client.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ def analyzeData(): | |
if __name__ == "__main__": | ||
PORT = os.getenv('PORT', 5000) | ||
app.run(debug=True, port=PORT) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
flask | ||
pymongo |