Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,7 +54,7 @@ def generate_with_deepseek(prompt: str) -> str:
|
|
| 54 |
return "حدث خطأ أثناء الاتصال بنموذج DeepSeek عبر OpenRouter."
|
| 55 |
|
| 56 |
# ============================================================
|
| 57 |
-
# 4. CHATBOT LOGIC
|
| 58 |
# ============================================================
|
| 59 |
|
| 60 |
def chatbot_fn(message: str, history: list):
|
|
@@ -71,8 +71,15 @@ def chatbot_fn(message: str, history: list):
|
|
| 71 |
print("Error in chatbot_fn:", e)
|
| 72 |
response = "حدث خطأ أثناء معالجة الطلب."
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# ============================================================
|
| 78 |
# 5. GRADIO INTERFACE (FIXED)
|
|
@@ -86,7 +93,12 @@ with gr.Blocks(title="Chatbot الإسعافات الأولية") as demo:
|
|
| 86 |
"""
|
| 87 |
)
|
| 88 |
|
| 89 |
-
chatbot_ui = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
user_input = gr.Textbox(
|
|
@@ -101,31 +113,21 @@ with gr.Blocks(title="Chatbot الإسعافات الأولية") as demo:
|
|
| 101 |
# State to maintain conversation history
|
| 102 |
chat_state = gr.State([])
|
| 103 |
|
| 104 |
-
# Actions
|
| 105 |
send_btn.click(
|
| 106 |
chatbot_fn,
|
| 107 |
inputs=[user_input, chat_state],
|
| 108 |
-
outputs=[chatbot_ui, user_input]
|
| 109 |
-
show_progress="hidden"
|
| 110 |
-
).then(
|
| 111 |
-
lambda: gr.update(interactive=True), # Re-enable input after processing
|
| 112 |
-
None,
|
| 113 |
-
[user_input]
|
| 114 |
)
|
| 115 |
|
| 116 |
user_input.submit(
|
| 117 |
chatbot_fn,
|
| 118 |
inputs=[user_input, chat_state],
|
| 119 |
-
outputs=[chatbot_ui, user_input]
|
| 120 |
-
show_progress="hidden"
|
| 121 |
-
).then(
|
| 122 |
-
lambda: gr.update(interactive=True),
|
| 123 |
-
None,
|
| 124 |
-
[user_input]
|
| 125 |
)
|
| 126 |
|
| 127 |
clear_btn.click(
|
| 128 |
-
lambda: ([], []),
|
| 129 |
outputs=[chatbot_ui, chat_state]
|
| 130 |
)
|
| 131 |
|
|
|
|
| 54 |
return "حدث خطأ أثناء الاتصال بنموذج DeepSeek عبر OpenRouter."
|
| 55 |
|
| 56 |
# ============================================================
|
| 57 |
+
# 4. CHATBOT LOGIC (FIXED FOR MESSAGES FORMAT)
|
| 58 |
# ============================================================
|
| 59 |
|
| 60 |
def chatbot_fn(message: str, history: list):
|
|
|
|
| 71 |
print("Error in chatbot_fn:", e)
|
| 72 |
response = "حدث خطأ أثناء معالجة الطلب."
|
| 73 |
|
| 74 |
+
# For type="messages", append messages in the correct format
|
| 75 |
+
if history is None:
|
| 76 |
+
history = []
|
| 77 |
+
|
| 78 |
+
# Append user message and assistant response in the correct format
|
| 79 |
+
history.append({"role": "user", "content": message})
|
| 80 |
+
history.append({"role": "assistant", "content": response})
|
| 81 |
+
|
| 82 |
+
return history, ""
|
| 83 |
|
| 84 |
# ============================================================
|
| 85 |
# 5. GRADIO INTERFACE (FIXED)
|
|
|
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
|
| 96 |
+
chatbot_ui = gr.Chatbot(
|
| 97 |
+
label="المحادثة",
|
| 98 |
+
type="messages",
|
| 99 |
+
height=500,
|
| 100 |
+
show_copy_button=True # Optional: adds copy button to messages
|
| 101 |
+
)
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
user_input = gr.Textbox(
|
|
|
|
| 113 |
# State to maintain conversation history
|
| 114 |
chat_state = gr.State([])
|
| 115 |
|
| 116 |
+
# Actions
|
| 117 |
send_btn.click(
|
| 118 |
chatbot_fn,
|
| 119 |
inputs=[user_input, chat_state],
|
| 120 |
+
outputs=[chatbot_ui, user_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
user_input.submit(
|
| 124 |
chatbot_fn,
|
| 125 |
inputs=[user_input, chat_state],
|
| 126 |
+
outputs=[chatbot_ui, user_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
)
|
| 128 |
|
| 129 |
clear_btn.click(
|
| 130 |
+
lambda: ([], []), # Clear both chatbot display and state
|
| 131 |
outputs=[chatbot_ui, chat_state]
|
| 132 |
)
|
| 133 |
|