Skip to content

Commit

Permalink
microphone input
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew0022 committed Nov 30, 2023
1 parent 8f5e874 commit 68da194
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
35 changes: 25 additions & 10 deletions web-app/app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
from flask import Flask, render_template, request, redirect, url_for, make_response, session
from flask import Flask, render_template, request, jsonify
import os
from pymongo import MongoClient
import pymongo
import datetime
from bson.objectid import ObjectId
import sys
import subprocess

app = Flask('project4')

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('/anaylzeData', methods=['POST'])
def anaylzeData():
return
@app.route('/analyzeData', methods=['POST'])
def analyzeData():
try:
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.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)


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

except Exception as e:
return jsonify({"status": "error", "message": str(e)})

if __name__ == "__main__":
PORT = os.getenv('PORT', 5000)
app.run(debug=True,port=PORT)
app.run(debug=True, port=PORT)
70 changes: 35 additions & 35 deletions web-app/templates/root.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,64 @@
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
{% extends 'base.html' %} {% block Base %}
{% extends 'base.html' %}
{% block Base %}
<div class="buttonContainer">
<h2 style="text-align:center">What is our machine Learning app?</h2>
<button type="submit" onclick="beginRecording()"> Start Recording Audio </button>
<button type="submit" onclick="stopRecording()"> Stop Recording Audio </button>
</div>
<div class="buttonContainer">
<p class="displayReuslts"> reuslts of analysis will be place here</p>
<p class="displayReuslts">Results of analysis will be placed here</p>
</div>

<script>
let mediaRecorder;
let audioChunks = [];
let wordCount = 0;

async function beginRecording() {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const audioContext = new AudioContext();
const audioInput = audioContext.createMediaStreamSource(stream);
mediaRecorder = new MediaREcorder(audioContext);
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });

mediaRecorder.ondataavailable = (event) =>{
audioChunks.push(event.data);
};
if (stream instanceof MediaStream) {
mediaRecorder = new MediaRecorder(stream);

mediaRecorder.onstop = async () =>{
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};

const formData = new FormData();
formData.append('audio', audioBlob);
mediaRecorder.onstop = async () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });

const response = await fetch('/anaylzeData', {
method: 'POST',
body: formData
});
const formData = new FormData();
formData.append('audio', audioBlob);

/**
* const result = await response.json();
* const transcription = result.transcription;
*
*
*
*/
try {
const response = await fetch('/analyzeData', {
method: 'POST',
body: formData
});

}
const result = await response.json();
console.log(result);
} catch (error) {
console.error('Error sending data to server:', error);
}
}

//audioInput.connect(mediaRecorder);
//mediaRecorder.connect(audioContext.destination);
//mediaRecorder.start();
mediaRecorder.start();
} else {
console.error('Failed to obtain a valid MediaStream object.');
}
} catch (error) {
console.error('Error getting user media:', error);
}
}

function stopRecording(){
mediaRecorder.stop()
function stopRecording() {
mediaRecorder.stop();
}
</script>

{% endblock %}


</body>
</html>
</html>
Binary file added web-app/uploads/user_audio.wav
Binary file not shown.

0 comments on commit 68da194

Please sign in to comment.