Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,25 +67,23 @@ def generate_feedback(user_id, question_choice, strategy_choice, message, feedba
|
|
| 67 |
return feedback
|
| 68 |
|
| 69 |
@spaces.GPU(duration=60)
|
| 70 |
-
|
| 71 |
communicate = edge_tts.Communicate(feedback_buffer)
|
| 72 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 73 |
tmp_path = tmp_file.name
|
| 74 |
-
|
| 75 |
return tmp_path
|
| 76 |
|
| 77 |
-
|
| 78 |
current_audio_output = None
|
| 79 |
|
| 80 |
if audio is None:
|
| 81 |
-
|
| 82 |
-
return
|
| 83 |
|
| 84 |
sample_rate, audio_data = audio
|
| 85 |
|
| 86 |
if audio_data is None or len(audio_data) == 0:
|
| 87 |
-
|
| 88 |
-
return
|
| 89 |
|
| 90 |
audio_path = "audio.wav"
|
| 91 |
if not isinstance(audio_data, np.ndarray):
|
|
@@ -93,37 +91,29 @@ async def predict(question_choice, strategy_choice, feedback_level, audio):
|
|
| 93 |
sf.write(audio_path, audio_data, sample_rate)
|
| 94 |
|
| 95 |
chat_history = [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcribing your audio, please listen to your oral response while waiting ...")]
|
| 96 |
-
yield chat_history, current_audio_output
|
| 97 |
|
| 98 |
try:
|
| 99 |
-
|
| 100 |
-
student_response = await asyncio.wrap_future(transcription_future)
|
| 101 |
|
| 102 |
if not student_response.strip():
|
| 103 |
-
|
| 104 |
-
return
|
| 105 |
|
| 106 |
chat_history.append(("Student", student_response))
|
| 107 |
-
yield chat_history, current_audio_output
|
| 108 |
|
| 109 |
chat_history.append(("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription complete. Generating feedback. Please continue listening to your oral response while waiting ..."))
|
| 110 |
-
yield chat_history, current_audio_output
|
| 111 |
|
| 112 |
-
|
| 113 |
-
feedback = await asyncio.wrap_future(feedback_future)
|
| 114 |
|
| 115 |
chat_history.append(("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", feedback))
|
| 116 |
-
yield chat_history, current_audio_output
|
| 117 |
|
| 118 |
-
|
| 119 |
-
audio_output_path = await asyncio.wrap_future(audio_future)
|
| 120 |
|
| 121 |
current_audio_output = (24000, audio_output_path)
|
| 122 |
-
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
logging.error(f"An error occurred: {str(e)}", exc_info=True)
|
| 126 |
-
|
| 127 |
|
| 128 |
# Modify the toggle_oral_coach_visibility function to call add_user_privacy and store the returned user_id in user_state.value
|
| 129 |
def toggle_oral_coach_visibility(class_name, index_no, policy_checked):
|
|
|
|
| 67 |
return feedback
|
| 68 |
|
| 69 |
@spaces.GPU(duration=60)
|
| 70 |
+
def generate_audio_feedback(feedback_buffer):
|
| 71 |
communicate = edge_tts.Communicate(feedback_buffer)
|
| 72 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 73 |
tmp_path = tmp_file.name
|
| 74 |
+
asyncio.run(communicate.save(tmp_path))
|
| 75 |
return tmp_path
|
| 76 |
|
| 77 |
+
def predict(question_choice, strategy_choice, feedback_level, audio):
|
| 78 |
current_audio_output = None
|
| 79 |
|
| 80 |
if audio is None:
|
| 81 |
+
return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "No audio data received. Please try again.")], current_audio_output
|
|
|
|
| 82 |
|
| 83 |
sample_rate, audio_data = audio
|
| 84 |
|
| 85 |
if audio_data is None or len(audio_data) == 0:
|
| 86 |
+
return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "No audio data received. Please try again.")], current_audio_output
|
|
|
|
| 87 |
|
| 88 |
audio_path = "audio.wav"
|
| 89 |
if not isinstance(audio_data, np.ndarray):
|
|
|
|
| 91 |
sf.write(audio_path, audio_data, sample_rate)
|
| 92 |
|
| 93 |
chat_history = [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcribing your audio, please listen to your oral response while waiting ...")]
|
|
|
|
| 94 |
|
| 95 |
try:
|
| 96 |
+
student_response = transcribe(audio_path)
|
|
|
|
| 97 |
|
| 98 |
if not student_response.strip():
|
| 99 |
+
return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription failed. Please try again or seek assistance.")], current_audio_output
|
|
|
|
| 100 |
|
| 101 |
chat_history.append(("Student", student_response))
|
|
|
|
| 102 |
|
| 103 |
chat_history.append(("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "Transcription complete. Generating feedback. Please continue listening to your oral response while waiting ..."))
|
|
|
|
| 104 |
|
| 105 |
+
feedback = generate_feedback(int(user_state.value), question_choice, strategy_choice, student_response, feedback_level)
|
|
|
|
| 106 |
|
| 107 |
chat_history.append(("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", feedback))
|
|
|
|
| 108 |
|
| 109 |
+
audio_output_path = generate_audio_feedback(feedback)
|
|
|
|
| 110 |
|
| 111 |
current_audio_output = (24000, audio_output_path)
|
| 112 |
+
return chat_history, current_audio_output
|
| 113 |
|
| 114 |
except Exception as e:
|
| 115 |
logging.error(f"An error occurred: {str(e)}", exc_info=True)
|
| 116 |
+
return [("Oral Coach ⚡ϞϞ(๑⚈ ․̫ ⚈๑)∩ ⚡", "An error occurred. Please try again or seek assistance.")], current_audio_output
|
| 117 |
|
| 118 |
# Modify the toggle_oral_coach_visibility function to call add_user_privacy and store the returned user_id in user_state.value
|
| 119 |
def toggle_oral_coach_visibility(class_name, index_no, policy_checked):
|