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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -66
app.py CHANGED
@@ -50,12 +50,12 @@ def chat_with_model(message, history, context):
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
 
@@ -64,7 +64,7 @@ def chat_with_model(message, history, context):
64
 
65
  conversation.append({"role": "user", "content": message})
66
 
67
- # 🚀 Send to Hugging Face model
68
  try:
69
  response = requests.post(
70
  "https://router.huggingface.co/v1/chat/completions",
@@ -77,36 +77,18 @@ def chat_with_model(message, history, context):
77
  "messages": conversation
78
  }
79
  )
80
-
81
  data = response.json()
82
  reply = data["choices"][0]["message"]["content"]
83
-
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:
100
- return "📘 **You are in General Mode.** Ask EduAI anything about your studies!"
101
- return f"📘 **You selected {choice} mode.** Ask anything related to this topic!"
102
-
103
- # 🧹 Clear chat memory
104
- def clear_memory():
105
- if os.path.exists(MEMORY_FILE):
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."})
@@ -115,20 +97,14 @@ def handle_file(file, history):
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
- """
127
- # 🎓 **EduAI — Your Smart Study Companion**
128
- Welcome to **EduAI**, your friendly study assistant! 💬
129
- Get help in **Science, ICT, English, Mathematics**, and more.
130
- """
131
- )
132
 
133
  with gr.Row():
134
  with gr.Column(scale=1, min_width=230):
@@ -140,61 +116,69 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
140
  label="Choose a subject"
141
  )
142
 
143
- with gr.Accordion("🗓 Study Planner", open=False):
144
- planner = gr.Radio(
145
- ["View Plan 📅", "Add Task ✏️", "Study Tips 💡"],
146
- label="Planner Options"
147
- )
148
-
149
- with gr.Accordion("🌐 Languages", open=False):
150
- lang = gr.Radio(
151
- ["Learn Sinhala 🇱🇰", "Learn Tamil 🇮🇳", "Learn English 🇬🇧", "Learn Spanish 🇪🇸"],
152
- label="Language Options"
153
- )
154
-
155
  with gr.Accordion("⚙️ Settings", open=False):
156
  clear_btn = gr.Button("🧹 Clear Memory")
157
 
158
- with gr.Accordion("👩‍🎓 About", open=False):
159
- gr.Markdown(
160
- """
161
- EduAI was designed and fine-tuned by **Wafa Fazly**,
162
- a passionate Sri Lankan student 👩‍💻
163
- to help learners explore **Science, ICT, English, and more** —
164
- in a smart and friendly way! 🌟
165
- """
166
- )
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()
 
50
  save_memory(history)
51
  return history, history
52
 
53
+ # System role
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', trained by Alibaba. "
59
  "Always answer truthfully when asked about your creation."
60
  )}]
61
 
 
64
 
65
  conversation.append({"role": "user", "content": message})
66
 
67
+ # 🚀 Send to Hugging Face
68
  try:
69
  response = requests.post(
70
  "https://router.huggingface.co/v1/chat/completions",
 
77
  "messages": conversation
78
  }
79
  )
 
80
  data = response.json()
81
  reply = data["choices"][0]["message"]["content"]
 
 
 
 
82
  history.append({"role": "user", "content": message})
83
  history.append({"role": "assistant", "content": reply})
84
  save_memory(history)
85
  return history, history
 
86
  except Exception as e:
87
  history.append({"role": "assistant", "content": f"😅 EduAI error: {e}"})
88
  return history, history
89
 
90
 
91
+ # 📎 Handle File Uploads
 
 
 
 
 
 
 
 
 
 
 
 
92
  def handle_file(file, history):
93
  if file is None:
94
  history.append({"role": "assistant", "content": "⚠️ No file uploaded yet."})
 
97
  history.append({"role": "assistant", "content": f"📁 File uploaded: **{filename}**\n✅ Received successfully!"})
98
  return history
99
 
100
+ # ⏸ Pause Chat
101
  def pause_chat(history):
102
  history.append({"role": "assistant", "content": "⏸️ Chat paused. Click 'Send' to continue."})
103
  return history
104
 
105
+ # 🎨 Interface
106
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
107
+ gr.Markdown("# 🎓 **EduAI — Your Smart Study Companion**")
 
 
 
 
 
 
108
 
109
  with gr.Row():
110
  with gr.Column(scale=1, min_width=230):
 
116
  label="Choose a subject"
117
  )
118
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  with gr.Accordion("⚙️ Settings", open=False):
120
  clear_btn = gr.Button("🧹 Clear Memory")
121
 
 
 
 
 
 
 
 
 
 
 
122
  with gr.Column(scale=4):
123
  context_display = gr.Markdown("📘 **You are in General Mode.** Ask EduAI anything about your studies!")
124
+
125
  chatbot = gr.Chatbot(
126
  label="💬 EduAI Chat Window",
127
  height=450,
128
+ type="messages"
129
  )
130
 
131
  with gr.Row():
132
+ # 🧷 Tiny file upload icon (like ChatGPT)
133
  upload_file = gr.File(
134
+ scale=1,
135
  file_count="single",
136
+ type="filepath",
137
+ elem_classes=["icon-upload"]
138
  )
139
+
140
  msg = gr.Textbox(
141
+ placeholder="Ask EduAI anything...",
 
142
  scale=6
143
  )
 
 
144
 
145
+ send = gr.Button("✨", scale=1)
146
+ pause_btn = gr.Button("⏸", scale=1)
147
+
148
+ # 🌟 Events
149
+ subj.change(lambda s: f"📘 You selected {s} mode.", inputs=subj, outputs=context_display)
150
  send.click(chat_with_model, inputs=[msg, chatbot, context_display], outputs=[chatbot, chatbot])
151
+ clear_btn.click(lambda: ([], "🧹 Chat memory cleared!"), outputs=[chatbot, context_display])
152
  upload_file.upload(handle_file, inputs=[upload_file, chatbot], outputs=chatbot)
153
  pause_btn.click(pause_chat, inputs=chatbot, outputs=chatbot)
154
 
155
+ # 💅 Add custom CSS for icon style
156
+ iface.load(
157
+ None,
158
+ None,
159
+ None,
160
+ _js="""
161
+ () => {
162
+ const upload = document.querySelector('.icon-upload input');
163
+ const parent = document.querySelector('.icon-upload');
164
+ if (upload && parent) {
165
+ parent.style.border = 'none';
166
+ parent.style.background = 'none';
167
+ parent.style.width = '32px';
168
+ parent.style.height = '32px';
169
+ parent.style.cursor = 'pointer';
170
+ parent.style.display = 'flex';
171
+ parent.style.alignItems = 'center';
172
+ parent.style.justifyContent = 'center';
173
+ const icon = document.createElement('span');
174
+ icon.textContent = '📎';
175
+ icon.style.fontSize = '22px';
176
+ icon.style.cursor = 'pointer';
177
+ parent.appendChild(icon);
178
+ icon.onclick = () => upload.click();
179
+ }
180
+ }
181
+ """
182
+ )
183
+
184
  iface.launch()