Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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])
|
| 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 |
-
|
| 128 |
-
|
|
|
|
| 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
|
| 154 |
-
|
| 155 |
-
|
| 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=
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|