Update app.py
Browse files
app.py
CHANGED
|
@@ -42,29 +42,25 @@ def chat_with_model(message, history, context):
|
|
| 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:]
|
| 51 |
search_result = search_web(query)
|
| 52 |
-
history.append(
|
|
|
|
| 53 |
save_memory(history)
|
| 54 |
return history, history
|
| 55 |
|
| 56 |
-
#
|
| 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',
|
| 62 |
-
"
|
| 63 |
)}]
|
| 64 |
|
| 65 |
-
for
|
| 66 |
-
conversation.append(
|
| 67 |
-
conversation.append({"role": "assistant", "content": past_bot})
|
| 68 |
|
| 69 |
conversation.append({"role": "user", "content": message})
|
| 70 |
|
|
@@ -88,14 +84,16 @@ def chat_with_model(message, history, context):
|
|
| 88 |
reply = reply.replace("Step", "\n\n**Step")
|
| 89 |
reply = reply.replace(":", ":**")
|
| 90 |
|
| 91 |
-
history.append(
|
|
|
|
| 92 |
save_memory(history)
|
| 93 |
return history, history
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
-
history.append(
|
| 97 |
return history, history
|
| 98 |
|
|
|
|
| 99 |
# π Sidebar context update
|
| 100 |
def update_context(choice):
|
| 101 |
if not choice:
|
|
@@ -108,18 +106,21 @@ def clear_memory():
|
|
| 108 |
os.remove(MEMORY_FILE)
|
| 109 |
return [], "π§Ή Chat memory cleared! Start fresh."
|
| 110 |
|
| 111 |
-
# π
|
| 112 |
-
def handle_file(file):
|
| 113 |
if file is None:
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
# βΈ Pause
|
| 118 |
def pause_chat(history):
|
| 119 |
-
history.append(
|
| 120 |
return history
|
| 121 |
|
| 122 |
-
# π¨ Gradio Interface (UI
|
| 123 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
| 124 |
gr.Markdown(
|
| 125 |
"""
|
|
@@ -166,32 +167,34 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
| 166 |
|
| 167 |
with gr.Column(scale=4):
|
| 168 |
context_display = gr.Markdown("π **You are in General Mode.** Ask EduAI anything about your studies!")
|
| 169 |
-
|
| 170 |
chatbot = gr.Chatbot(
|
| 171 |
label="π¬ EduAI Chat Window",
|
| 172 |
height=450,
|
| 173 |
-
|
| 174 |
-
bubble_full_width=False
|
| 175 |
)
|
| 176 |
|
| 177 |
-
# π --- Chat Input Section with Icons ---
|
| 178 |
with gr.Row():
|
| 179 |
-
upload_file = gr.File(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
msg = gr.Textbox(
|
| 181 |
-
placeholder="Ask EduAI anything about your studies...",
|
| 182 |
label="π Type your question here...",
|
|
|
|
| 183 |
scale=6
|
| 184 |
)
|
| 185 |
-
send = gr.Button("β¨", scale=1)
|
| 186 |
-
pause_btn = gr.Button("βΈ", scale=1)
|
| 187 |
|
| 188 |
# πͺ Event handlers
|
| 189 |
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 190 |
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 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()
|
|
|
|
| 42 |
if not isinstance(history, list):
|
| 43 |
history = []
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
if message.lower().startswith("search "):
|
| 46 |
query = message[7:]
|
| 47 |
search_result = search_web(query)
|
| 48 |
+
history.append({"role": "user", "content": message})
|
| 49 |
+
history.append({"role": "assistant", "content": f"π Here's what I found online:\n\n{search_result}"})
|
| 50 |
save_memory(history)
|
| 51 |
return history, history
|
| 52 |
|
| 53 |
+
# Build conversation
|
| 54 |
conversation = [{"role": "system", "content": (
|
| 55 |
"You are EduAI β an educational AI assistant created by Wafa Fazly "
|
| 56 |
"from Fathima Muslim Ladies College. "
|
| 57 |
"You help students learn subjects such as Math, Science, English, and IT. "
|
| 58 |
+
"EduAI runs on the model 'Qwen/Qwen3-VL-8B-Instruct', originally trained by Alibaba. "
|
| 59 |
+
"Always answer truthfully when asked about your creation."
|
| 60 |
)}]
|
| 61 |
|
| 62 |
+
for msg in history[-5:]:
|
| 63 |
+
conversation.append(msg)
|
|
|
|
| 64 |
|
| 65 |
conversation.append({"role": "user", "content": message})
|
| 66 |
|
|
|
|
| 84 |
reply = reply.replace("Step", "\n\n**Step")
|
| 85 |
reply = reply.replace(":", ":**")
|
| 86 |
|
| 87 |
+
history.append({"role": "user", "content": message})
|
| 88 |
+
history.append({"role": "assistant", "content": reply})
|
| 89 |
save_memory(history)
|
| 90 |
return history, history
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
+
history.append({"role": "assistant", "content": f"π
EduAI error: {e}"})
|
| 94 |
return history, history
|
| 95 |
|
| 96 |
+
|
| 97 |
# π Sidebar context update
|
| 98 |
def update_context(choice):
|
| 99 |
if not choice:
|
|
|
|
| 106 |
os.remove(MEMORY_FILE)
|
| 107 |
return [], "π§Ή Chat memory cleared! Start fresh."
|
| 108 |
|
| 109 |
+
# π File Upload Handler
|
| 110 |
+
def handle_file(file, history):
|
| 111 |
if file is None:
|
| 112 |
+
history.append({"role": "assistant", "content": "β οΈ No file uploaded yet."})
|
| 113 |
+
return history
|
| 114 |
+
filename = file.name.split("/")[-1]
|
| 115 |
+
history.append({"role": "assistant", "content": f"π File uploaded: **{filename}**\nβ
Received successfully!"})
|
| 116 |
+
return history
|
| 117 |
|
| 118 |
+
# βΈ Pause Chat Handler
|
| 119 |
def pause_chat(history):
|
| 120 |
+
history.append({"role": "assistant", "content": "βΈοΈ Chat paused. Click 'Send' to continue."})
|
| 121 |
return history
|
| 122 |
|
| 123 |
+
# π¨ Gradio Interface (Final UI)
|
| 124 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
| 125 |
gr.Markdown(
|
| 126 |
"""
|
|
|
|
| 167 |
|
| 168 |
with gr.Column(scale=4):
|
| 169 |
context_display = gr.Markdown("π **You are in General Mode.** Ask EduAI anything about your studies!")
|
|
|
|
| 170 |
chatbot = gr.Chatbot(
|
| 171 |
label="π¬ EduAI Chat Window",
|
| 172 |
height=450,
|
| 173 |
+
type="messages" # β
fixed modern format
|
|
|
|
| 174 |
)
|
| 175 |
|
|
|
|
| 176 |
with gr.Row():
|
| 177 |
+
upload_file = gr.File(
|
| 178 |
+
label="π",
|
| 179 |
+
file_count="single",
|
| 180 |
+
type="filepath", # β
fixed type
|
| 181 |
+
scale=1
|
| 182 |
+
)
|
| 183 |
msg = gr.Textbox(
|
|
|
|
| 184 |
label="π Type your question here...",
|
| 185 |
+
placeholder="Ask EduAI anything about your studies...",
|
| 186 |
scale=6
|
| 187 |
)
|
| 188 |
+
send = gr.Button("β¨ Send", scale=1)
|
| 189 |
+
pause_btn = gr.Button("βΈ Pause", scale=1)
|
| 190 |
|
| 191 |
# πͺ Event handlers
|
| 192 |
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 193 |
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 194 |
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 195 |
send.click(chat_with_model, inputs=[msg, chatbot, context_display], outputs=[chatbot, chatbot])
|
|
|
|
|
|
|
| 196 |
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
| 197 |
+
upload_file.upload(handle_file, inputs=[upload_file, chatbot], outputs=chatbot)
|
| 198 |
+
pause_btn.click(pause_chat, inputs=chatbot, outputs=chatbot)
|
| 199 |
|
| 200 |
iface.launch()
|