dschandra commited on
Commit
646145e
·
verified ·
1 Parent(s): 8acfe6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -85,19 +85,16 @@ html_code = """
85
  let mediaRecorder;
86
  let audioChunks = [];
87
  let isConversationActive = false;
88
-
89
  micButton.addEventListener('click', () => {
90
  if (!isConversationActive) {
91
  isConversationActive = true;
92
  startConversation();
93
  }
94
  });
95
-
96
  function startConversation() {
97
  status.textContent = 'Listening...';
98
  startListening();
99
  }
100
-
101
  function startListening() {
102
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
103
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
@@ -108,22 +105,18 @@ html_code = """
108
  const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
109
  const formData = new FormData();
110
  formData.append('audio', audioBlob);
111
-
112
  status.textContent = 'Processing...';
113
  try {
114
  const result = await fetch('/process-audio', { method: 'POST', body: formData });
115
  const data = await result.json();
116
  response.textContent = data.response;
117
  response.style.display = 'block';
118
-
119
  try {
120
  const utterance = new SpeechSynthesisUtterance(data.response);
121
  speechSynthesis.speak(utterance);
122
-
123
  utterance.onend = () => {
124
  console.log("Speech synthesis completed.");
125
  };
126
-
127
  utterance.onerror = (e) => {
128
  console.error("Speech synthesis error:", e.error);
129
  status.textContent = 'Error with speech output.';
@@ -132,7 +125,6 @@ html_code = """
132
  console.error("Speech synthesis not supported or failed:", speechError);
133
  response.textContent = "Speech output unavailable. Please check your browser.";
134
  }
135
-
136
  if (data.response.includes("Goodbye")) {
137
  status.textContent = 'Conversation ended. Press the mic button to start again.';
138
  isConversationActive = false;
@@ -177,7 +169,7 @@ def process_audio():
177
  if os.path.getsize(temp_file.name) == 0:
178
  raise BadRequest("Uploaded audio file is empty.")
179
 
180
- # Convert audio to PCM WAV format
181
  converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
182
  try:
183
  ffmpeg.input(temp_file.name).output(
@@ -193,9 +185,16 @@ def process_audio():
193
  recognizer = sr.Recognizer()
194
  with sr.AudioFile(converted_file.name) as source:
195
  audio_data = recognizer.record(source)
196
- command = recognizer.recognize_google(audio_data)
197
- logging.info(f"Recognized command: {command}")
198
- response = process_command(command)
 
 
 
 
 
 
 
199
 
200
  return jsonify({"response": response})
201
 
 
85
  let mediaRecorder;
86
  let audioChunks = [];
87
  let isConversationActive = false;
 
88
  micButton.addEventListener('click', () => {
89
  if (!isConversationActive) {
90
  isConversationActive = true;
91
  startConversation();
92
  }
93
  });
 
94
  function startConversation() {
95
  status.textContent = 'Listening...';
96
  startListening();
97
  }
 
98
  function startListening() {
99
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
100
  mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm;codecs=opus' });
 
105
  const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
106
  const formData = new FormData();
107
  formData.append('audio', audioBlob);
 
108
  status.textContent = 'Processing...';
109
  try {
110
  const result = await fetch('/process-audio', { method: 'POST', body: formData });
111
  const data = await result.json();
112
  response.textContent = data.response;
113
  response.style.display = 'block';
 
114
  try {
115
  const utterance = new SpeechSynthesisUtterance(data.response);
116
  speechSynthesis.speak(utterance);
 
117
  utterance.onend = () => {
118
  console.log("Speech synthesis completed.");
119
  };
 
120
  utterance.onerror = (e) => {
121
  console.error("Speech synthesis error:", e.error);
122
  status.textContent = 'Error with speech output.';
 
125
  console.error("Speech synthesis not supported or failed:", speechError);
126
  response.textContent = "Speech output unavailable. Please check your browser.";
127
  }
 
128
  if (data.response.includes("Goodbye")) {
129
  status.textContent = 'Conversation ended. Press the mic button to start again.';
130
  isConversationActive = false;
 
169
  if os.path.getsize(temp_file.name) == 0:
170
  raise BadRequest("Uploaded audio file is empty.")
171
 
172
+ # Convert audio to PCM WAV format (16kHz, mono)
173
  converted_file = NamedTemporaryFile(delete=False, suffix=".wav")
174
  try:
175
  ffmpeg.input(temp_file.name).output(
 
185
  recognizer = sr.Recognizer()
186
  with sr.AudioFile(converted_file.name) as source:
187
  audio_data = recognizer.record(source)
188
+ try:
189
+ command = recognizer.recognize_google(audio_data)
190
+ logging.info(f"Recognized command: {command}")
191
+ response = process_command(command)
192
+ except sr.UnknownValueError:
193
+ logging.error("Google Speech Recognition could not understand the audio")
194
+ response = "Sorry, I couldn't understand your request. Please try again."
195
+ except sr.RequestError as e:
196
+ logging.error(f"Error with Google Speech Recognition service: {e}")
197
+ response = "Sorry, there was an issue with the speech recognition service."
198
 
199
  return jsonify({"response": response})
200