Wfafa commited on
Commit
98cd9fb
Β·
verified Β·
1 Parent(s): dd4987e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -30
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((message, f"πŸ”Ž Here's what I found online:\n\n{search_result}"))
 
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:]:
66
- conversation.append({"role": "user", "content": past_user})
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((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
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
- # πŸ“Ž Handle File Upload
112
- def handle_file(file):
113
  if file is None:
114
- return []
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:
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
- render_markdown=True,
174
- bubble_full_width=False
175
  )
176
 
177
- # πŸ’­ --- Chat Input Section with Icons ---
178
  with gr.Row():
179
- upload_file = gr.File(label="πŸ“Ž", file_count="single", type="file", scale=1)
 
 
 
 
 
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()