YoussefA7med commited on
Commit
a994885
·
verified ·
1 Parent(s): 1b2eef9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -58
app.py CHANGED
@@ -36,6 +36,7 @@ MAIN_SYSTEM_PROMPT = {
36
  'level_assessment': Current assessment of user's English level (beginner/intermediate/advanced),
37
  'encouragement': A motivating comment,
38
  'context_memory': Important details about the user to remember (interests, job, etc.)
 
39
  Guidelines:
40
  1. Maintain natural conversation while gathering user information
41
  2. Adapt language complexity to user's level
@@ -51,6 +52,7 @@ MAIN_SYSTEM_PROMPT = {
51
  - If user mentions hobbies → Engage in that topic
52
  - For beginners → Use simple words and short sentences
53
  - For intermediate/advanced → Introduce more complex vocabulary
 
54
  Always maintain conversation history and adapt based on it."""
55
  }
56
 
@@ -195,24 +197,23 @@ def text_to_speech(text):
195
  # إنشاء كائن المعلم
196
  tutor = EnglishTutor()
197
 
198
- def chat_function(audio, text_input, history):
199
  """الدالة الرئيسية للمحادثة"""
200
  try:
201
  # إذا كانت أول محادثة، نعرض رسالة الترحيب
202
  if not history:
203
  welcome = tutor.get_welcome_message()
204
  welcome_audio = text_to_speech(welcome)
205
- return "", [(None, welcome)], welcome_audio
206
 
207
- # تحديد مصدر الإدخال (صوت أو نص)
208
- user_message = None
209
- if audio is not None:
210
- user_message = convert_audio_to_text(audio)
211
- elif text_input:
212
- user_message = text_input
213
 
 
 
214
  if not user_message:
215
- return "", history, None
216
 
217
  # الحصول على رد البوت
218
  bot_response = tutor.get_bot_response(user_message)
@@ -224,68 +225,41 @@ def chat_function(audio, text_input, history):
224
  history = history or []
225
  history.append((user_message, bot_response))
226
 
227
- return "", history, audio_response
228
  except Exception as e:
229
  print(f"Error in chat function: {str(e)}")
230
- return "", history, None
231
-
232
- def clear_conversation():
233
- """إعادة تعيين المحادثة"""
234
- global tutor
235
- tutor = EnglishTutor()
236
- return [], None, None, ""
237
 
238
  # إنشاء واجهة المستخدم
239
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
240
  gr.Markdown("# 🎓 English Learning Assistant")
241
- gr.Markdown("Speak or type naturally and I'll help you improve your English!")
 
 
 
 
 
 
242
 
243
  with gr.Row():
244
- with gr.Column(scale=2):
245
- chatbot = gr.Chatbot(
246
- height=500,
247
- bubble_full_width=False,
248
- show_label=False
249
- )
250
-
251
- with gr.Row():
252
- text_input = gr.Textbox(
253
- placeholder="Type here or use voice input...",
254
- label="Your Message",
255
- show_label=False
256
- )
257
- clear_btn = gr.Button("Clear Chat", variant="secondary")
258
-
259
- with gr.Row():
260
- audio_input = gr.Audio(
261
- label="Voice Input",
262
- type="filepath",
263
- show_label=True
264
- )
265
- audio_output = gr.Audio(
266
- label="Tutor's Voice",
267
- type="filepath",
268
- show_label=True
269
- )
270
 
271
  # ربط الأحداث
272
- text_input.submit(
273
- chat_function,
274
- inputs=[None, text_input, chatbot],
275
- outputs=[text_input, chatbot, audio_output]
276
- )
277
-
278
  audio_input.change(
279
  chat_function,
280
- inputs=[audio_input, None, chatbot],
281
- outputs=[text_input, chatbot, audio_output]
282
- )
283
-
284
- clear_btn.click(
285
- clear_conversation,
286
- outputs=[chatbot, audio_input, audio_output, text_input]
287
  )
288
 
289
  # تشغيل التطبيق
290
  if __name__ == "__main__":
291
- demo.launch()
 
36
  'level_assessment': Current assessment of user's English level (beginner/intermediate/advanced),
37
  'encouragement': A motivating comment,
38
  'context_memory': Important details about the user to remember (interests, job, etc.)
39
+
40
  Guidelines:
41
  1. Maintain natural conversation while gathering user information
42
  2. Adapt language complexity to user's level
 
52
  - If user mentions hobbies → Engage in that topic
53
  - For beginners → Use simple words and short sentences
54
  - For intermediate/advanced → Introduce more complex vocabulary
55
+
56
  Always maintain conversation history and adapt based on it."""
57
  }
58
 
 
197
  # إنشاء كائن المعلم
198
  tutor = EnglishTutor()
199
 
200
+ def chat_function(audio, history):
201
  """الدالة الرئيسية للمحادثة"""
202
  try:
203
  # إذا كانت أول محادثة، نعرض رسالة الترحيب
204
  if not history:
205
  welcome = tutor.get_welcome_message()
206
  welcome_audio = text_to_speech(welcome)
207
+ return [(None, welcome)], welcome_audio
208
 
209
+ # إذا لم يكن هناك صوت، نرجع بدون تغيير
210
+ if audio is None:
211
+ return history, None
 
 
 
212
 
213
+ # تحويل الصوت إلى نص
214
+ user_message = convert_audio_to_text(audio)
215
  if not user_message:
216
+ return history, None
217
 
218
  # الحصول على رد البوت
219
  bot_response = tutor.get_bot_response(user_message)
 
225
  history = history or []
226
  history.append((user_message, bot_response))
227
 
228
+ return history, audio_response
229
  except Exception as e:
230
  print(f"Error in chat function: {str(e)}")
231
+ return history, None
 
 
 
 
 
 
232
 
233
  # إنشاء واجهة المستخدم
234
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
235
  gr.Markdown("# 🎓 English Learning Assistant")
236
+ gr.Markdown("Speak naturally and I'll help you improve your English!")
237
+
238
+ chatbot = gr.Chatbot(
239
+ height=400,
240
+ bubble_full_width=False,
241
+ show_label=False
242
+ )
243
 
244
  with gr.Row():
245
+ audio_input = gr.Audio(
246
+ label="Your Voice",
247
+ type="filepath",
248
+ show_label=True
249
+ )
250
+ audio_output = gr.Audio(
251
+ label="Tutor's Voice",
252
+ type="filepath",
253
+ show_label=True
254
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
  # ربط الأحداث
 
 
 
 
 
 
257
  audio_input.change(
258
  chat_function,
259
+ inputs=[audio_input, chatbot],
260
+ outputs=[chatbot, audio_output]
 
 
 
 
 
261
  )
262
 
263
  # تشغيل التطبيق
264
  if __name__ == "__main__":
265
+ demo.launch(server_name="0.0.0.0", server_port=7860)