Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -197,23 +197,24 @@ def text_to_speech(text):
|
|
| 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 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
| 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,41 +226,68 @@ def chat_function(audio, history):
|
|
| 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 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 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()
|
|
|
|
| 197 |
# إنشاء كائن المعلم
|
| 198 |
tutor = EnglishTutor()
|
| 199 |
|
| 200 |
+
def chat_function(audio, text_input, 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 |
+
user_message = None
|
| 211 |
+
if audio is not None:
|
| 212 |
+
user_message = convert_audio_to_text(audio)
|
| 213 |
+
elif text_input:
|
| 214 |
+
user_message = text_input
|
| 215 |
|
|
|
|
|
|
|
| 216 |
if not user_message:
|
| 217 |
+
return "", history, None
|
| 218 |
|
| 219 |
# الحصول على رد البوت
|
| 220 |
bot_response = tutor.get_bot_response(user_message)
|
|
|
|
| 226 |
history = history or []
|
| 227 |
history.append((user_message, bot_response))
|
| 228 |
|
| 229 |
+
return "", history, audio_response
|
| 230 |
except Exception as e:
|
| 231 |
print(f"Error in chat function: {str(e)}")
|
| 232 |
+
return "", history, None
|
| 233 |
+
|
| 234 |
+
def clear_conversation():
|
| 235 |
+
"""إعادة تعيين المحادثة"""
|
| 236 |
+
global tutor
|
| 237 |
+
tutor = EnglishTutor()
|
| 238 |
+
return [], None, None, ""
|
| 239 |
|
| 240 |
# إنشاء واجهة المستخدم
|
| 241 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 242 |
gr.Markdown("# 🎓 English Learning Assistant")
|
| 243 |
+
gr.Markdown("Speak or type naturally and I'll help you improve your English!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
with gr.Row():
|
| 246 |
+
with gr.Column(scale=2):
|
| 247 |
+
chatbot = gr.Chatbot(
|
| 248 |
+
height=500,
|
| 249 |
+
bubble_full_width=False,
|
| 250 |
+
show_label=False
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
with gr.Row():
|
| 254 |
+
text_input = gr.Textbox(
|
| 255 |
+
placeholder="Type here or use voice input...",
|
| 256 |
+
label="Your Message",
|
| 257 |
+
show_label=False
|
| 258 |
+
)
|
| 259 |
+
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
| 260 |
+
|
| 261 |
+
with gr.Row():
|
| 262 |
+
audio_input = gr.Audio(
|
| 263 |
+
label="Voice Input",
|
| 264 |
+
type="filepath",
|
| 265 |
+
show_label=True
|
| 266 |
+
)
|
| 267 |
+
audio_output = gr.Audio(
|
| 268 |
+
label="Tutor's Voice",
|
| 269 |
+
type="filepath",
|
| 270 |
+
show_label=True
|
| 271 |
+
)
|
| 272 |
|
| 273 |
# ربط الأحداث
|
| 274 |
+
text_input.submit(
|
| 275 |
+
chat_function,
|
| 276 |
+
inputs=[None, text_input, chatbot],
|
| 277 |
+
outputs=[text_input, chatbot, audio_output]
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
audio_input.change(
|
| 281 |
chat_function,
|
| 282 |
+
inputs=[audio_input, None, chatbot],
|
| 283 |
+
outputs=[text_input, chatbot, audio_output]
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
clear_btn.click(
|
| 287 |
+
clear_conversation,
|
| 288 |
+
outputs=[chatbot, audio_input, audio_output, text_input]
|
| 289 |
)
|
| 290 |
|
| 291 |
# تشغيل التطبيق
|
| 292 |
if __name__ == "__main__":
|
| 293 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|