Update app.py
Browse files
app.py
CHANGED
|
@@ -42,6 +42,9 @@ def chat_with_model(message, history, context):
|
|
| 42 |
if not isinstance(history, list):
|
| 43 |
history = []
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
# ๐ Web search mode
|
| 46 |
if message.lower().startswith("search "):
|
| 47 |
query = message[7:]
|
|
@@ -50,13 +53,13 @@ def chat_with_model(message, history, context):
|
|
| 50 |
save_memory(history)
|
| 51 |
return history, history
|
| 52 |
|
| 53 |
-
# ๐ง
|
| 54 |
conversation = [{"role": "system", "content": (
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
)}]
|
| 61 |
|
| 62 |
for past_user, past_bot in history[-5:]:
|
|
@@ -82,22 +85,15 @@ def chat_with_model(message, history, context):
|
|
| 82 |
data = response.json()
|
| 83 |
reply = data["choices"][0]["message"]["content"]
|
| 84 |
|
| 85 |
-
# ๐งฎ Clean up math formatting
|
| 86 |
reply = reply.replace("Step", "\n\n**Step")
|
| 87 |
reply = reply.replace(":", ":**")
|
| 88 |
-
reply = reply.replace("\\[", "\n\n\\[")
|
| 89 |
-
reply = reply.replace("\\]", "\\]\n\n")
|
| 90 |
-
|
| 91 |
-
if "\\" in reply or "log_" in reply or "^" in reply:
|
| 92 |
-
reply = f"{reply}"
|
| 93 |
|
| 94 |
history.append((message, reply))
|
| 95 |
save_memory(history)
|
| 96 |
return history, history
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
-
|
| 100 |
-
history.append((message, "๐
EduAI is having trouble connecting right now. Please try again later!"))
|
| 101 |
return history, history
|
| 102 |
|
| 103 |
# ๐ Sidebar context update
|
|
@@ -119,8 +115,9 @@ def handle_file(file):
|
|
| 119 |
return [(f"๐ File uploaded: {file.name}", "โ
Received successfully!")]
|
| 120 |
|
| 121 |
# โธ Pause Button
|
| 122 |
-
def pause_chat():
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
# ๐จ Gradio Interface (UI Improved)
|
| 126 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
@@ -174,11 +171,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
| 174 |
label="๐ฌ EduAI Chat Window",
|
| 175 |
height=450,
|
| 176 |
render_markdown=True,
|
| 177 |
-
bubble_full_width=False
|
| 178 |
-
latex_delimiters=[
|
| 179 |
-
{"left": "$$", "right": "$$", "display": True},
|
| 180 |
-
{"left": "\\[", "right": "\\]", "display": True}
|
| 181 |
-
]
|
| 182 |
)
|
| 183 |
|
| 184 |
# ๐ญ --- Chat Input Section with Icons ---
|
|
@@ -198,7 +191,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
| 198 |
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 199 |
send.click(chat_with_model, inputs=[msg, chatbot, context_display], outputs=[chatbot, chatbot])
|
| 200 |
upload_file.upload(handle_file, inputs=upload_file, outputs=chatbot)
|
| 201 |
-
pause_btn.click(pause_chat, outputs=chatbot)
|
| 202 |
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
| 203 |
|
| 204 |
iface.launch()
|
|
|
|
| 42 |
if not isinstance(history, list):
|
| 43 |
history = []
|
| 44 |
|
| 45 |
+
if not message:
|
| 46 |
+
return history, history # Prevent empty messages from causing issues
|
| 47 |
+
|
| 48 |
# ๐ Web search mode
|
| 49 |
if message.lower().startswith("search "):
|
| 50 |
query = message[7:]
|
|
|
|
| 53 |
save_memory(history)
|
| 54 |
return history, history
|
| 55 |
|
| 56 |
+
# ๐ง System context
|
| 57 |
conversation = [{"role": "system", "content": (
|
| 58 |
+
"You are EduAI โ an educational AI assistant created by Wafa Fazly "
|
| 59 |
+
"from Fathima Muslim Ladies College. "
|
| 60 |
+
"You help students learn subjects such as Math, Science, English, and IT. "
|
| 61 |
+
"EduAI runs on the model 'Qwen/Qwen3-VL-8B-Instruct', which was originally "
|
| 62 |
+
"trained by Alibaba. Always answer truthfully when asked about your creation."
|
| 63 |
)}]
|
| 64 |
|
| 65 |
for past_user, past_bot in history[-5:]:
|
|
|
|
| 85 |
data = response.json()
|
| 86 |
reply = data["choices"][0]["message"]["content"]
|
| 87 |
|
|
|
|
| 88 |
reply = reply.replace("Step", "\n\n**Step")
|
| 89 |
reply = reply.replace(":", ":**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
history.append((message, reply))
|
| 92 |
save_memory(history)
|
| 93 |
return history, history
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
+
history.append((message, f"๐
EduAI connection error: {e}"))
|
|
|
|
| 97 |
return history, history
|
| 98 |
|
| 99 |
# ๐ Sidebar context update
|
|
|
|
| 115 |
return [(f"๐ File uploaded: {file.name}", "โ
Received successfully!")]
|
| 116 |
|
| 117 |
# โธ Pause Button
|
| 118 |
+
def pause_chat(history):
|
| 119 |
+
history.append(("โธ", "โธ๏ธ Chat paused. Click 'Send' to continue."))
|
| 120 |
+
return history
|
| 121 |
|
| 122 |
# ๐จ Gradio Interface (UI Improved)
|
| 123 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
|
|
| 171 |
label="๐ฌ EduAI Chat Window",
|
| 172 |
height=450,
|
| 173 |
render_markdown=True,
|
| 174 |
+
bubble_full_width=False
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
)
|
| 176 |
|
| 177 |
# ๐ญ --- Chat Input Section with Icons ---
|
|
|
|
| 191 |
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 192 |
send.click(chat_with_model, inputs=[msg, chatbot, context_display], outputs=[chatbot, chatbot])
|
| 193 |
upload_file.upload(handle_file, inputs=upload_file, outputs=chatbot)
|
| 194 |
+
pause_btn.click(pause_chat, inputs=chatbot, outputs=chatbot)
|
| 195 |
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
| 196 |
|
| 197 |
iface.launch()
|