YoussefA7med commited on
Commit
489e668
Β·
verified Β·
1 Parent(s): 27ccdcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -20
app.py CHANGED
@@ -25,13 +25,15 @@ MAIN_SYSTEM_PROMPT = {
25
  "role": "system",
26
  "content": """You are Sam, an intelligent and proactive English tutor. You drive the conversation and actively engage students. Your responses must be in JSON format with these keys:
27
  'response': Your main response (keep it conversational and engaging),
28
- 'corrections': Specific grammar or pronunciation corrections with examples,
29
- 'vocabulary': Alternative words/phrases with explanations,
30
  'level_assessment': Current assessment (beginner/intermediate/advanced),
31
  'encouragement': A motivating comment,
32
  'context_memory': Important details about the user,
33
  'next_question': A follow-up question to keep conversation flowing
34
 
 
 
35
  Your personality:
36
  - Be the conversation driver - ask follow-up questions
37
  - Show genuine interest in the student's life
@@ -42,10 +44,17 @@ MAIN_SYSTEM_PROMPT = {
42
  - Ask about their day, work, hobbies, culture, goals
43
 
44
  Correction guidelines:
45
- - Always provide corrections if there are grammar mistakes
46
- - Suggest better vocabulary choices
47
- - Give pronunciation tips when needed
48
  - Use format: "Instead of 'X', try saying 'Y'"
 
 
 
 
 
 
 
 
49
 
50
  Conversation flow:
51
  - Start with personal questions (name, country, job, hobbies)
@@ -253,14 +262,26 @@ def process_audio(audio, history, transcript, corrections):
253
  # Update transcript
254
  new_transcript = transcript + f"\n\n🎀 You: {user_message}\nπŸ€– Sam: {main_response}"
255
 
256
- # Update corrections and vocabulary (THIS IS THE FIX!)
257
  new_corrections = corrections
258
  correction_parts = []
259
 
260
- if bot_response.get("corrections") and bot_response["corrections"].strip():
 
 
 
 
 
 
 
 
 
 
 
261
  correction_parts.append(f"✍️ **Grammar Corrections:**\n{bot_response['corrections']}")
262
 
263
- if bot_response.get("vocabulary") and bot_response["vocabulary"].strip():
 
264
  vocab = bot_response['vocabulary']
265
  if isinstance(vocab, dict):
266
  vocab_text = "\n".join([f"β€’ '{k}' β†’ '{v}'" for k, v in vocab.items()])
@@ -268,12 +289,31 @@ def process_audio(audio, history, transcript, corrections):
268
  vocab_text = str(vocab)
269
  correction_parts.append(f"πŸ“š **Vocabulary Suggestions:**\n{vocab_text}")
270
 
271
- if bot_response.get("level_assessment"):
272
- correction_parts.append(f"πŸ“Š **Current Level:** {bot_response['level_assessment'].title()}")
 
 
 
 
 
 
 
 
 
 
273
 
274
- if correction_parts:
275
- new_correction_text = "\n\n".join(correction_parts)
276
- new_corrections = new_corrections + f"\n\n--- Latest Feedback ---\n{new_correction_text}" if new_corrections else new_correction_text
 
 
 
 
 
 
 
 
 
277
 
278
  return history, audio_response, new_transcript, new_corrections
279
  except Exception as e:
@@ -290,7 +330,7 @@ def clear_chat():
290
 
291
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
292
  gr.Markdown("# πŸŽ“ English Learning Assistant with Sam")
293
- gr.Markdown("🎀 **Record your voice and click submit** - Sam will actively guide your conversation and help improve your English!")
294
 
295
  with gr.Row():
296
  with gr.Column(scale=3):
@@ -302,15 +342,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
302
  )
303
 
304
  with gr.Row():
305
- with gr.Column(scale=2):
306
  audio_input = gr.Audio(
307
- label="πŸŽ™οΈ Record your voice",
308
  type="filepath",
309
  show_label=True
310
  )
311
  with gr.Column(scale=1):
312
- submit_btn = gr.Button("πŸ“€ Submit Recording", variant="primary", size="lg")
313
- with gr.Column(scale=2):
314
  audio_output = gr.Audio(
315
  label="πŸ”Š Sam's response",
316
  type="filepath",
@@ -343,8 +381,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
343
  clear_btn = gr.Button("πŸ”„ Start New Conversation", variant="secondary", size="lg")
344
  gr.Markdown("πŸ’‘ **Tip**: Sam will actively guide the conversation and provide personalized feedback!")
345
 
346
- submit_btn.click(
347
- submit_recording,
 
348
  inputs=[audio_input, chatbot, transcript_display, corrections_display],
349
  outputs=[chatbot, audio_output, transcript_display, corrections_display]
350
  )
 
25
  "role": "system",
26
  "content": """You are Sam, an intelligent and proactive English tutor. You drive the conversation and actively engage students. Your responses must be in JSON format with these keys:
27
  'response': Your main response (keep it conversational and engaging),
28
+ 'corrections': ALWAYS provide specific grammar or pronunciation corrections with examples (if none needed, say "Great grammar!"),
29
+ 'vocabulary': ALWAYS suggest alternative words/phrases with explanations (if none needed, suggest related vocabulary),
30
  'level_assessment': Current assessment (beginner/intermediate/advanced),
31
  'encouragement': A motivating comment,
32
  'context_memory': Important details about the user,
33
  'next_question': A follow-up question to keep conversation flowing
34
 
35
+ IMPORTANT: You MUST always provide corrections and vocabulary suggestions in every response. Even if the student speaks perfectly, provide positive feedback and suggest advanced vocabulary or alternative expressions.
36
+
37
  Your personality:
38
  - Be the conversation driver - ask follow-up questions
39
  - Show genuine interest in the student's life
 
44
  - Ask about their day, work, hobbies, culture, goals
45
 
46
  Correction guidelines:
47
+ - ALWAYS provide corrections field - even if it's positive feedback
48
+ - ALWAYS provide vocabulary field - suggest alternatives or related words
 
49
  - Use format: "Instead of 'X', try saying 'Y'"
50
+ - Give pronunciation tips when needed
51
+ - If no mistakes, say "Excellent grammar!" or "Perfect sentence structure!"
52
+
53
+ Vocabulary guidelines:
54
+ - ALWAYS suggest vocabulary - even if it's synonyms or advanced alternatives
55
+ - Provide explanations for suggested words
56
+ - Use format: "Instead of 'good', try 'excellent' or 'outstanding'"
57
+ - Suggest topic-related vocabulary
58
 
59
  Conversation flow:
60
  - Start with personal questions (name, country, job, hobbies)
 
262
  # Update transcript
263
  new_transcript = transcript + f"\n\n🎀 You: {user_message}\nπŸ€– Sam: {main_response}"
264
 
265
+ # Update corrections and vocabulary with debugging
266
  new_corrections = corrections
267
  correction_parts = []
268
 
269
+ # Debug: Print the bot response to see what we're getting
270
+ print(f"DEBUG - Bot response keys: {bot_response.keys()}")
271
+ print(f"DEBUG - Corrections: '{bot_response.get('corrections', 'NOT FOUND')}'")
272
+ print(f"DEBUG - Vocabulary: '{bot_response.get('vocabulary', 'NOT FOUND')}'")
273
+ print(f"DEBUG - Level: '{bot_response.get('level_assessment', 'NOT FOUND')}'")
274
+
275
+ # Always show current level
276
+ if bot_response.get("level_assessment"):
277
+ correction_parts.append(f"πŸ“Š **Current Level:** {bot_response['level_assessment'].title()}")
278
+
279
+ # Show corrections if available
280
+ if bot_response.get("corrections") and str(bot_response["corrections"]).strip() and bot_response["corrections"] != "":
281
  correction_parts.append(f"✍️ **Grammar Corrections:**\n{bot_response['corrections']}")
282
 
283
+ # Show vocabulary if available
284
+ if bot_response.get("vocabulary") and str(bot_response["vocabulary"]).strip() and bot_response["vocabulary"] != "":
285
  vocab = bot_response['vocabulary']
286
  if isinstance(vocab, dict):
287
  vocab_text = "\n".join([f"β€’ '{k}' β†’ '{v}'" for k, v in vocab.items()])
 
289
  vocab_text = str(vocab)
290
  correction_parts.append(f"πŸ“š **Vocabulary Suggestions:**\n{vocab_text}")
291
 
292
+ # Show encouragement
293
+ if bot_response.get("encouragement"):
294
+ correction_parts.append(f"πŸ’‘ **Encouragement:**\n{bot_response['encouragement']}")
295
+
296
+ # Always show user info if available
297
+ if tutor.user_info.get("name"):
298
+ info_parts = []
299
+ if tutor.user_info.get("name"): info_parts.append(f"Name: {tutor.user_info['name']}")
300
+ if tutor.user_info.get("country"): info_parts.append(f"Country: {tutor.user_info['country']}")
301
+ if tutor.user_info.get("level"): info_parts.append(f"Level: {tutor.user_info['level']}")
302
+ if info_parts:
303
+ correction_parts.append(f"πŸ‘€ **Your Profile:**\n{' | '.join(info_parts)}")
304
 
305
+ # If still no corrections, show a default message
306
+ if not correction_parts:
307
+ correction_parts.append("🎯 **Feedback:** Keep practicing! Sam is analyzing your English and will provide feedback soon.")
308
+
309
+ # Create the new corrections text
310
+ new_correction_text = "\n\n".join(correction_parts)
311
+ timestamp = f"[{user_message[:30]}...]" if len(user_message) > 30 else f"[{user_message}]"
312
+
313
+ if new_corrections:
314
+ new_corrections = new_corrections + f"\n\n--- Latest Response {timestamp} ---\n{new_correction_text}"
315
+ else:
316
+ new_corrections = f"--- Latest Response {timestamp} ---\n{new_correction_text}"
317
 
318
  return history, audio_response, new_transcript, new_corrections
319
  except Exception as e:
 
330
 
331
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
332
  gr.Markdown("# πŸŽ“ English Learning Assistant with Sam")
333
+ gr.Markdown("🎀 **Record your voice** - Sam will automatically respond when you finish recording and help improve your English!")
334
 
335
  with gr.Row():
336
  with gr.Column(scale=3):
 
342
  )
343
 
344
  with gr.Row():
345
+ with gr.Column(scale=1):
346
  audio_input = gr.Audio(
347
+ label="πŸŽ™οΈ Record your voice (auto-submits when finished)",
348
  type="filepath",
349
  show_label=True
350
  )
351
  with gr.Column(scale=1):
 
 
352
  audio_output = gr.Audio(
353
  label="πŸ”Š Sam's response",
354
  type="filepath",
 
381
  clear_btn = gr.Button("πŸ”„ Start New Conversation", variant="secondary", size="lg")
382
  gr.Markdown("πŸ’‘ **Tip**: Sam will actively guide the conversation and provide personalized feedback!")
383
 
384
+ # Auto-submit when audio is recorded
385
+ audio_input.change(
386
+ process_audio,
387
  inputs=[audio_input, chatbot, transcript_display, corrections_display],
388
  outputs=[chatbot, audio_output, transcript_display, corrections_display]
389
  )