Wfafa commited on
Commit
d55032c
ยท
verified ยท
1 Parent(s): ef3020d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -26
app.py CHANGED
@@ -10,12 +10,11 @@ def search_web(query):
10
  params = {"q": query, "format": "json", "no_html": 1, "skip_disambig": 1}
11
  response = requests.get(url, params=params)
12
  data = response.json()
13
-
14
  if data.get("AbstractText"):
15
  return data["AbstractText"]
16
  elif data.get("RelatedTopics"):
17
  topics = [t.get("Text", "") for t in data["RelatedTopics"] if "Text" in t]
18
- return " ".join(topics[:3]) # just a few results
19
  else:
20
  return "No useful information found."
21
  except Exception as e:
@@ -84,7 +83,6 @@ def chat_with_model(message, history, context, file_input=None):
84
  "messages": conversation
85
  }
86
  )
87
-
88
  data = response.json()
89
  reply = data["choices"][0]["message"]["content"]
90
 
@@ -94,10 +92,6 @@ def chat_with_model(message, history, context, file_input=None):
94
  reply = reply.replace("\\[", "\n\n\\[")
95
  reply = reply.replace("\\]", "\\]\n\n")
96
 
97
- # โœ… Add Markdown + LaTeX support
98
- if "\\" in reply or "log_" in reply or "^" in reply:
99
- reply = f"{reply}"
100
-
101
  history.append((message, reply))
102
  save_memory(history)
103
  return history, history
@@ -121,57 +115,83 @@ def clear_memory():
121
 
122
  # ๐ŸŽจ Gradio Interface
123
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
124
- gr.Markdown("# ๐ŸŽ“ **EduAI โ€” Your Smart Study Companion**")
125
 
126
  with gr.Row():
127
- with gr.Column(scale=1, min_width=230):
128
- gr.Markdown("### ๐Ÿงญ **Main Menu**")
 
129
 
130
  with gr.Accordion("๐Ÿ“š Subject Tutor", open=False):
131
  subj = gr.Radio(
132
  ["Science ๐Ÿงช", "ICT ๐Ÿ’ป", "English ๐Ÿ“˜", "Mathematics โž—"],
133
- label="Choose a subject"
 
134
  )
135
 
136
  with gr.Accordion("๐Ÿ—“ Study Planner", open=False):
137
  planner = gr.Radio(
138
  ["View Plan ๐Ÿ“…", "Add Task โœ๏ธ", "Study Tips ๐Ÿ’ก"],
139
- label="Planner Options"
 
140
  )
141
 
142
  with gr.Accordion("๐ŸŒ Languages", open=False):
143
  lang = gr.Radio(
144
  ["Learn Sinhala ๐Ÿ‡ฑ๐Ÿ‡ฐ", "Learn Tamil ๐Ÿ‡ฎ๐Ÿ‡ณ", "Learn English ๐Ÿ‡ฌ๐Ÿ‡ง", "Learn Spanish ๐Ÿ‡ช๐Ÿ‡ธ"],
145
- label="Language Options"
 
146
  )
147
 
148
  with gr.Accordion("โš™๏ธ Settings", open=False):
149
- clear_btn = gr.Button("๐Ÿงน Clear Memory")
150
 
151
  with gr.Accordion("๐Ÿ‘ฉโ€๐ŸŽ“ About", open=False):
152
  gr.Markdown("""
153
- EduAI โ€“ developed and fine-tuned by **Wafa Fazly** using a pre-trained AI model,
154
- to help learners understand **Science, ICT, English, and more** โ€”
155
- in a simple and friendly way! ๐Ÿ’ฌ
156
- """)
 
 
 
 
 
 
157
 
158
- with gr.Column(scale=4):
159
- context_display = gr.Markdown("๐Ÿ“˜ **You are in General Mode.** Ask EduAI anything about your studies!")
160
  chatbot = gr.Chatbot(
161
  label="EduAI Chat",
162
- height=450,
163
  render_markdown=True,
 
164
  latex_delimiters=[{"left": "$$", "right": "$$", "display": True}, {"left": "\\[", "right": "\\]", "display": True}]
165
  )
166
- msg = gr.Textbox(label="Ask EduAI:")
167
- file_input = gr.File(label="๐Ÿ“‚ Upload a study file (PDF, DOCX, or image):")
168
- send = gr.Button("Send โœˆ๏ธ")
169
 
170
- # ๐Ÿช„ Event handlers
 
 
 
 
 
171
  subj.change(update_context, inputs=subj, outputs=context_display)
172
  planner.change(update_context, inputs=planner, outputs=context_display)
173
  lang.change(update_context, inputs=lang, outputs=context_display)
174
  send.click(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, chatbot])
175
  clear_btn.click(clear_memory, outputs=[chatbot, context_display])
176
 
177
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  params = {"q": query, "format": "json", "no_html": 1, "skip_disambig": 1}
11
  response = requests.get(url, params=params)
12
  data = response.json()
 
13
  if data.get("AbstractText"):
14
  return data["AbstractText"]
15
  elif data.get("RelatedTopics"):
16
  topics = [t.get("Text", "") for t in data["RelatedTopics"] if "Text" in t]
17
+ return " ".join(topics[:3])
18
  else:
19
  return "No useful information found."
20
  except Exception as e:
 
83
  "messages": conversation
84
  }
85
  )
 
86
  data = response.json()
87
  reply = data["choices"][0]["message"]["content"]
88
 
 
92
  reply = reply.replace("\\[", "\n\n\\[")
93
  reply = reply.replace("\\]", "\\]\n\n")
94
 
 
 
 
 
95
  history.append((message, reply))
96
  save_memory(history)
97
  return history, history
 
115
 
116
  # ๐ŸŽจ Gradio Interface
117
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
118
+ gr.Markdown("# ๐ŸŽ“ **EduAI โ€” Your Smart Study Companion**", elem_classes="header-title")
119
 
120
  with gr.Row():
121
+ # Sidebar
122
+ with gr.Column(scale=1, min_width=250, elem_classes="sidebar"):
123
+ gr.Markdown("### ๐Ÿงญ **Main Menu**", elem_classes="menu-title")
124
 
125
  with gr.Accordion("๐Ÿ“š Subject Tutor", open=False):
126
  subj = gr.Radio(
127
  ["Science ๐Ÿงช", "ICT ๐Ÿ’ป", "English ๐Ÿ“˜", "Mathematics โž—"],
128
+ label="Choose a subject",
129
+ type="index"
130
  )
131
 
132
  with gr.Accordion("๐Ÿ—“ Study Planner", open=False):
133
  planner = gr.Radio(
134
  ["View Plan ๐Ÿ“…", "Add Task โœ๏ธ", "Study Tips ๐Ÿ’ก"],
135
+ label="Planner Options",
136
+ type="index"
137
  )
138
 
139
  with gr.Accordion("๐ŸŒ Languages", open=False):
140
  lang = gr.Radio(
141
  ["Learn Sinhala ๐Ÿ‡ฑ๐Ÿ‡ฐ", "Learn Tamil ๐Ÿ‡ฎ๐Ÿ‡ณ", "Learn English ๐Ÿ‡ฌ๐Ÿ‡ง", "Learn Spanish ๐Ÿ‡ช๐Ÿ‡ธ"],
142
+ label="Language Options",
143
+ type="index"
144
  )
145
 
146
  with gr.Accordion("โš™๏ธ Settings", open=False):
147
+ clear_btn = gr.Button("๐Ÿงน Clear Memory", elem_classes="btn-clear")
148
 
149
  with gr.Accordion("๐Ÿ‘ฉโ€๐ŸŽ“ About", open=False):
150
  gr.Markdown("""
151
+ EduAI โ€“ developed by **Wafa Fazly** using a pre-trained AI model.
152
+ Helps learners understand **Science, ICT, English, and more** in a simple, friendly way! ๐Ÿ’ฌ
153
+ """, elem_classes="about-text")
154
+
155
+ # Main Chat Area
156
+ with gr.Column(scale=4, elem_classes="main-chat"):
157
+ context_display = gr.Markdown(
158
+ "๐Ÿ“˜ **You are in General Mode.** Ask EduAI anything about your studies!",
159
+ elem_classes="context-box"
160
+ )
161
 
 
 
162
  chatbot = gr.Chatbot(
163
  label="EduAI Chat",
164
+ height=500,
165
  render_markdown=True,
166
+ elem_classes="chatbox",
167
  latex_delimiters=[{"left": "$$", "right": "$$", "display": True}, {"left": "\\[", "right": "\\]", "display": True}]
168
  )
 
 
 
169
 
170
+ with gr.Row():
171
+ msg = gr.Textbox(label="Ask EduAI:", placeholder="Type your question here...", elem_classes="chat-input")
172
+ file_input = gr.File(label="๐Ÿ“‚ Upload a study file", file_types=[".pdf", ".docx", ".png", ".jpg"], elem_classes="file-upload")
173
+ send = gr.Button("Send โœˆ๏ธ", elem_classes="btn-send")
174
+
175
+ # Event Handlers
176
  subj.change(update_context, inputs=subj, outputs=context_display)
177
  planner.change(update_context, inputs=planner, outputs=context_display)
178
  lang.change(update_context, inputs=lang, outputs=context_display)
179
  send.click(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, chatbot])
180
  clear_btn.click(clear_memory, outputs=[chatbot, context_display])
181
 
182
+ # ๐Ÿ–Œ Custom CSS for professional look
183
+ custom_css = """
184
+ .header-title { font-size: 36px; text-align:center; margin-bottom:20px; }
185
+ .sidebar { background-color:#f5f3ff; padding:15px; border-radius:12px; box-shadow:0 2px 6px rgba(0,0,0,0.1); }
186
+ .menu-title { font-size:20px; font-weight:bold; margin-bottom:10px; }
187
+ .main-chat { padding:10px; }
188
+ .context-box { background-color:#ffffff; padding:10px; border-radius:10px; box-shadow:0 1px 4px rgba(0,0,0,0.1); margin-bottom:10px; }
189
+ .chatbox .message.user { background-color:#e0d7ff; border-radius:10px; padding:8px; margin:5px 0; }
190
+ .chatbox .message.bot { background-color:#ffffff; border-radius:10px; padding:8px; margin:5px 0; box-shadow:0 1px 2px rgba(0,0,0,0.05);}
191
+ .chat-input { border-radius:10px; padding:8px; width:100%; }
192
+ .btn-send { background-color:#7c3aed; color:white; border-radius:10px; padding:8px 16px; }
193
+ .btn-clear { background-color:#ef4444; color:white; border-radius:10px; padding:6px 12px; }
194
+ .file-upload { border:2px dashed #d4b5ff; border-radius:10px; padding:10px; text-align:center; }
195
+ .about-text { font-size:14px; color:#4b5563; }
196
+ """
197
+ iface.launch(css=custom_css)