Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kei edits two #22

Merged
merged 8 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y portaudio19-dev
- name: Install dependencies
if: ${{ hashFiles('**/*.py') != '' }}
run: |
python -m pip install --upgrade pip
Expand Down
4 changes: 4 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pymongo = "*"
speechrecognition = "*"
pyaudio = "*"
setuptools = "*"
tomli = "*"
typing-extensions = "*"
dill = "*"
python-dotenv = "*"

[dev-packages]

Expand Down
210 changes: 115 additions & 95 deletions Pipfile.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions database/database.py.py → database/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""
database file to connect ot database
"""
import os

from pymongo import MongoClient
from dotenv import load_dotenv

Expand All @@ -18,4 +20,4 @@
DB = client[os.getenv("MONGODB_DATABASE")]
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e: # pylint: disable=broad-except
print(e)
print(e)
75 changes: 4 additions & 71 deletions machine-learning-client/machine_learning_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from enum import Enum
import pyaudio
import speech_recognition as sr
<<<<<<< HEAD


class ML:
"""Machine Learning class functions """
"""Machine Learning class functions"""

pa = pyaudio.PyAudio()

def list_all_mic(self):
Expand Down Expand Up @@ -56,66 +57,7 @@ def audio_to_text(self, audio_file_string):

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

def grade_response(self, transcription):
"""
Give out a score based on the transcribed audio


=======


class ML:
"""Machine Learning class functions """
pa = pyaudio.PyAudio()

def list_all_mic(self):
"""
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(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"

Expand All @@ -124,7 +66,6 @@ def grade_response(self, transcription):
Give out a score based on the transcribed audio


>>>>>>> 5a0af371e4dbfe2fdc0328cf9e5f1e13a84def20
Args:
transcription (str): transcribed audio
"""
Expand All @@ -140,19 +81,11 @@ def main():
"""Main Method"""
print("Tell me a little bit about yourself")
ml = ML()
<<<<<<< HEAD
audio = r'..\\web-app\uploads\user_audio.wav'
audio = r"..\\web-app\uploads\user_audio.wav"
transcription = ml.audio_to_text(audio)
result = ml.grade_response(transcription)
print(result)
print("test main")

=======
audio = ml.record_microphone()
transcription = ml.audio_to_text(audio)
result = ml.grade_response(transcription)
print(result)
>>>>>>> 5a0af371e4dbfe2fdc0328cf9e5f1e13a84def20


if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions machine-learning-client/tests/ml_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from machine_learning_client import ML




class Tests:
""" Tests class for ml_tests.py """
"""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,
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"
r"\4-containerized-app-exercise-rizzballs\machine-learning-client\tests\richard.aiff",
)
assert isinstance(
actual, str
Expand All @@ -30,5 +30,5 @@ def test_grade_response(self):
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 \
), f"Expected the result to be 'your response had hello appearing 5 times \
ugh appearing 0 times '. Instead it returned {actual}"
66 changes: 46 additions & 20 deletions web-app/app.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
from flask import Flask, render_template, request, jsonify
"""
app.py file that helps serve as the front end of our application
"""

import os
import subprocess
from flask import Flask, render_template, request, jsonify


app = Flask("project4")

app = Flask('project4')
app.config["UPLOAD_FOLDER"] = "uploads"
os.makedirs(app.config["UPLOAD_FOLDER"], exist_ok=True)

app.config['UPLOAD_FOLDER'] = 'uploads'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

@app.route('/')
def RootPage():
return render_template('root.html')
@app.route("/")
def root_page():
"""
template to render root page
"""
return render_template("root.html")

@app.route('/analyzeData', methods=['POST'])
def analyzeData():

@app.route("/analyzeData", methods=["POST"])
def analyze_data():
"""
Function to send genreated audio file to the machine learning client
"""
try:
if 'audio' not in request.files:
if "audio" not in request.files:
return jsonify({"status": "error", "message": "No audio file provided"})

audio_file = request.files['audio']
audio_path = os.path.join(app.config['UPLOAD_FOLDER'], 'user_audio.wav')
audio_file = request.files["audio"]
audio_path = os.path.join(app.config["UPLOAD_FOLDER"], "user_audio.wav")
audio_file.save(audio_path)


print("Audio file saved at:", audio_path)
result = subprocess.run(["C:\\Users\\Andrew - User\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe", "E:\\4-containerized-app-exercise-rizzballs\\machine-learning-client\\machine_learning_client.py", audio_path], capture_output=True, text=True)

print("Audio file saved at:", audio_path)
result = subprocess.run(
[
"C:\\Users\\Andrew - User\\AppData\\Local\\Microsoft\\WindowsApps\\python.exe",
"E:\\4-containerized-app-exercise-rizzballs\\machine-learning-client\\machine_learning_client.py", # pylint: disable=line-too-long
audio_path, # pylint: disable=line-too-long disable=trailing-whitespace
],
capture_output=True,
text=True,
check=True,
)
if result.returncode == 0:
print("Process succeeded!")
else:
print("Process failed. Exit code:", result.returncode)

return jsonify({"status": "success"})

except Exception as e:
return jsonify({"status": "error", "message": str(e)})
except FileNotFoundError as e:
return jsonify({"status": "error", "message": f"File not found: {str(e)}"})

except subprocess.CalledProcessError as e:
return jsonify({"status": "error", "message": f"Subprocess failed: {str(e)}"})


if __name__ == "__main__":
PORT = os.getenv('PORT', 5000)
PORT = os.getenv("PORT", "5000")
app.run(debug=True, port=PORT)

Loading