Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,10 +179,13 @@ def initialize_chat():
|
|
| 179 |
try:
|
| 180 |
welcome = tutor.get_welcome_message()
|
| 181 |
welcome_audio = text_to_speech(welcome)
|
| 182 |
-
|
|
|
|
| 183 |
except Exception as e:
|
| 184 |
print(f"Error initializing chat: {str(e)}")
|
| 185 |
-
|
|
|
|
|
|
|
| 186 |
|
| 187 |
def process_audio(audio, history, transcript, corrections):
|
| 188 |
try:
|
|
@@ -195,29 +198,25 @@ def process_audio(audio, history, transcript, corrections):
|
|
| 195 |
|
| 196 |
bot_response_json = tutor.get_bot_response(user_message)
|
| 197 |
|
| 198 |
-
# Extract the JSON response for corrections
|
| 199 |
try:
|
| 200 |
response_dict = json.loads(bot_response_json) if isinstance(bot_response_json, str) else bot_response_json
|
| 201 |
except:
|
| 202 |
response_dict = {"response": bot_response_json}
|
| 203 |
|
| 204 |
-
# Format response for TTS (without corrections and vocabulary)
|
| 205 |
tts_response = response_dict.get("response", bot_response_json)
|
| 206 |
if response_dict.get("encouragement"):
|
| 207 |
tts_response += f" {response_dict['encouragement']}"
|
| 208 |
|
| 209 |
-
# Generate audio from clean response
|
| 210 |
audio_response = text_to_speech(tts_response)
|
| 211 |
|
| 212 |
-
# Format full response for chat display
|
| 213 |
formatted_response = tutor._format_response(response_dict) if hasattr(tutor, '_format_response') else bot_response_json
|
| 214 |
|
| 215 |
history = history or []
|
| 216 |
-
history.append(
|
|
|
|
| 217 |
|
| 218 |
new_transcript = transcript + f"\n\nπ€ You: {user_message}\nπ€ Sam: {tts_response}"
|
| 219 |
|
| 220 |
-
# Update corrections display
|
| 221 |
new_corrections = corrections
|
| 222 |
if response_dict.get("corrections") or response_dict.get("vocabulary"):
|
| 223 |
correction_text = ""
|
|
@@ -253,8 +252,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 253 |
with gr.Column(scale=3):
|
| 254 |
chatbot = gr.Chatbot(
|
| 255 |
height=500,
|
| 256 |
-
bubble_full_width=False,
|
| 257 |
show_label=False,
|
|
|
|
| 258 |
avatar_images=("π€", "π€")
|
| 259 |
)
|
| 260 |
|
|
@@ -276,27 +275,25 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 276 |
)
|
| 277 |
|
| 278 |
with gr.Column(scale=2):
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
)
|
| 289 |
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
)
|
| 300 |
|
| 301 |
with gr.Row():
|
| 302 |
clear_btn = gr.Button("π Start New Conversation", variant="secondary", size="lg")
|
|
|
|
| 179 |
try:
|
| 180 |
welcome = tutor.get_welcome_message()
|
| 181 |
welcome_audio = text_to_speech(welcome)
|
| 182 |
+
history = [{"role": "assistant", "content": welcome}]
|
| 183 |
+
return history, welcome_audio, f"π€ Sam: {welcome}", ""
|
| 184 |
except Exception as e:
|
| 185 |
print(f"Error initializing chat: {str(e)}")
|
| 186 |
+
welcome_msg = "Hi! I'm Sam, your English tutor. What's your name?"
|
| 187 |
+
history = [{"role": "assistant", "content": welcome_msg}]
|
| 188 |
+
return history, None, f"π€ Sam: {welcome_msg}", ""
|
| 189 |
|
| 190 |
def process_audio(audio, history, transcript, corrections):
|
| 191 |
try:
|
|
|
|
| 198 |
|
| 199 |
bot_response_json = tutor.get_bot_response(user_message)
|
| 200 |
|
|
|
|
| 201 |
try:
|
| 202 |
response_dict = json.loads(bot_response_json) if isinstance(bot_response_json, str) else bot_response_json
|
| 203 |
except:
|
| 204 |
response_dict = {"response": bot_response_json}
|
| 205 |
|
|
|
|
| 206 |
tts_response = response_dict.get("response", bot_response_json)
|
| 207 |
if response_dict.get("encouragement"):
|
| 208 |
tts_response += f" {response_dict['encouragement']}"
|
| 209 |
|
|
|
|
| 210 |
audio_response = text_to_speech(tts_response)
|
| 211 |
|
|
|
|
| 212 |
formatted_response = tutor._format_response(response_dict) if hasattr(tutor, '_format_response') else bot_response_json
|
| 213 |
|
| 214 |
history = history or []
|
| 215 |
+
history.append({"role": "user", "content": user_message})
|
| 216 |
+
history.append({"role": "assistant", "content": formatted_response})
|
| 217 |
|
| 218 |
new_transcript = transcript + f"\n\nπ€ You: {user_message}\nπ€ Sam: {tts_response}"
|
| 219 |
|
|
|
|
| 220 |
new_corrections = corrections
|
| 221 |
if response_dict.get("corrections") or response_dict.get("vocabulary"):
|
| 222 |
correction_text = ""
|
|
|
|
| 252 |
with gr.Column(scale=3):
|
| 253 |
chatbot = gr.Chatbot(
|
| 254 |
height=500,
|
|
|
|
| 255 |
show_label=False,
|
| 256 |
+
type='messages',
|
| 257 |
avatar_images=("π€", "π€")
|
| 258 |
)
|
| 259 |
|
|
|
|
| 275 |
)
|
| 276 |
|
| 277 |
with gr.Column(scale=2):
|
| 278 |
+
gr.Markdown("### π Live Transcript")
|
| 279 |
+
transcript_display = gr.Textbox(
|
| 280 |
+
lines=10,
|
| 281 |
+
max_lines=10,
|
| 282 |
+
show_label=False,
|
| 283 |
+
interactive=False,
|
| 284 |
+
placeholder="Your conversation will appear here...",
|
| 285 |
+
container=True
|
| 286 |
+
)
|
|
|
|
| 287 |
|
| 288 |
+
gr.Markdown("### π Learning Corner")
|
| 289 |
+
corrections_display = gr.Textbox(
|
| 290 |
+
lines=8,
|
| 291 |
+
max_lines=8,
|
| 292 |
+
show_label=False,
|
| 293 |
+
interactive=False,
|
| 294 |
+
placeholder="Grammar corrections and vocabulary suggestions will appear here...",
|
| 295 |
+
container=True
|
| 296 |
+
)
|
|
|
|
| 297 |
|
| 298 |
with gr.Row():
|
| 299 |
clear_btn = gr.Button("π Start New Conversation", variant="secondary", size="lg")
|