Update app.py
Browse files
app.py
CHANGED
|
@@ -1,125 +1,186 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
""")
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
with gr.Row():
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
with gr.Column(scale=4):
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# ๐ Web search function
|
| 7 |
+
def search_web(query):
|
| 8 |
+
try:
|
| 9 |
+
url = "https://api.duckduckgo.com/"
|
| 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:
|
| 22 |
+
return f"Search error: {e}"
|
| 23 |
+
|
| 24 |
+
# ๐ง Memory setup
|
| 25 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 26 |
+
MEMORY_FILE = "memory.json"
|
| 27 |
+
|
| 28 |
+
def load_memory():
|
| 29 |
+
if os.path.exists(MEMORY_FILE):
|
| 30 |
+
with open(MEMORY_FILE, "r") as f:
|
| 31 |
+
return json.load(f)
|
| 32 |
+
return []
|
| 33 |
+
|
| 34 |
+
def save_memory(memory):
|
| 35 |
+
with open(MEMORY_FILE, "w") as f:
|
| 36 |
+
json.dump(memory, f)
|
| 37 |
+
|
| 38 |
+
memory = load_memory()
|
| 39 |
+
|
| 40 |
+
# ๐ฌ Chat function
|
| 41 |
+
def chat_with_model(message, history, context):
|
| 42 |
+
if not isinstance(history, list):
|
| 43 |
+
history = []
|
| 44 |
+
|
| 45 |
+
# ๐ Web search mode
|
| 46 |
+
if message.lower().startswith("search "):
|
| 47 |
+
query = message[7:]
|
| 48 |
+
search_result = search_web(query)
|
| 49 |
+
history.append((message, 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 = [
|
| 55 |
+
{"role": "system", "content": (
|
| 56 |
+
"You are EduAI, a multilingual educational AI assistant created by a Sri Lankan student named Wafa Fazly. "
|
| 57 |
+
"When solving math, explain step-by-step like a professional tutor. "
|
| 58 |
+
"Use Markdown and LaTeX formatting for equations (use \\[ and \\]). "
|
| 59 |
+
"Keep answers neat, structured, and student-friendly."
|
| 60 |
+
)}
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
for past_user, past_bot in history[-5:]:
|
| 64 |
+
conversation.append({"role": "user", "content": past_user})
|
| 65 |
+
conversation.append({"role": "assistant", "content": past_bot})
|
| 66 |
+
|
| 67 |
+
conversation.append({"role": "user", "content": message})
|
| 68 |
+
|
| 69 |
+
# ๐ Send to Hugging Face model
|
| 70 |
+
try:
|
| 71 |
+
response = requests.post(
|
| 72 |
+
"https://router.huggingface.co/v1/chat/completions",
|
| 73 |
+
headers={
|
| 74 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 75 |
+
"Content-Type": "application/json"
|
| 76 |
+
},
|
| 77 |
+
json={
|
| 78 |
+
"model": "deepseek-ai/DeepSeek-V3.2-Exp:novita",
|
| 79 |
+
"messages": conversation
|
| 80 |
+
}
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
data = response.json()
|
| 84 |
+
reply = data["choices"][0]["message"]["content"]
|
| 85 |
+
|
| 86 |
+
# ๐งฎ Clean up math formatting
|
| 87 |
+
reply = reply.replace("Step", "\n\n**Step")
|
| 88 |
+
reply = reply.replace(":", ":**")
|
| 89 |
+
reply = reply.replace("\\[", "\n\n\\[")
|
| 90 |
+
reply = reply.replace("\\]", "\\]\n\n")
|
| 91 |
+
|
| 92 |
+
if "\\" in reply or "log_" in reply or "^" in reply:
|
| 93 |
+
reply = f"{reply}"
|
| 94 |
+
|
| 95 |
+
history.append((message, reply))
|
| 96 |
+
save_memory(history)
|
| 97 |
+
return history, history
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print("Error:", e)
|
| 101 |
+
history.append((message, "๐
EduAI is having trouble connecting right now. Please try again later!"))
|
| 102 |
+
return history, history
|
| 103 |
+
|
| 104 |
+
# ๐ Sidebar context update
|
| 105 |
+
def update_context(choice):
|
| 106 |
+
if not choice:
|
| 107 |
+
return "๐ **You are in General Mode.** Ask EduAI anything about your studies!"
|
| 108 |
+
return f"๐ **You selected {choice} mode.** Ask anything related to this topic!"
|
| 109 |
+
|
| 110 |
+
# ๐งน Clear chat memory
|
| 111 |
+
def clear_memory():
|
| 112 |
+
if os.path.exists(MEMORY_FILE):
|
| 113 |
+
os.remove(MEMORY_FILE)
|
| 114 |
+
return [], "๐งน Chat memory cleared! Start fresh."
|
| 115 |
+
|
| 116 |
+
# ๐จ Gradio Interface (UI Improved)
|
| 117 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
| 118 |
+
gr.Markdown(
|
| 119 |
+
"""
|
| 120 |
+
# ๐ **EduAI โ Your Smart Study Companion**
|
| 121 |
+
Welcome to **EduAI**, your friendly study assistant! ๐ฌ
|
| 122 |
+
Get help in **Science, ICT, English, Mathematics**, and more.
|
| 123 |
+
"""
|
| 124 |
+
)
|
| 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 |
+
"""
|
| 154 |
+
EduAI was designed and fine-tuned by **Wafa Fazly**,
|
| 155 |
+
a passionate Sri Lankan student ๐ฉโ๐ป
|
| 156 |
+
to help learners explore **Science, ICT, English, and more** โ
|
| 157 |
+
in a smart and friendly way! ๐
|
| 158 |
+
"""
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
with gr.Column(scale=4):
|
| 162 |
+
context_display = gr.Markdown("๐ **You are in General Mode.** Ask EduAI anything about your studies!")
|
| 163 |
+
chatbot = gr.Chatbot(
|
| 164 |
+
label="๐ฌ EduAI Chat Window",
|
| 165 |
+
height=450,
|
| 166 |
+
render_markdown=True,
|
| 167 |
+
bubble_full_width=False,
|
| 168 |
+
latex_delimiters=[
|
| 169 |
+
{"left": "$$", "right": "$$", "display": True},
|
| 170 |
+
{"left": "\\[", "right": "\\]", "display": True}
|
| 171 |
+
]
|
| 172 |
+
)
|
| 173 |
+
msg = gr.Textbox(
|
| 174 |
+
label="๐ญ Type your question here...",
|
| 175 |
+
placeholder="Ask EduAI anything about your studies..."
|
| 176 |
+
)
|
| 177 |
+
send = gr.Button("โจ Send Message")
|
| 178 |
+
|
| 179 |
+
# ๐ช Event handlers
|
| 180 |
+
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 181 |
+
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 182 |
+
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 183 |
+
send.click(chat_with_model, inputs=[msg, chatbot, context_display], outputs=[chatbot, chatbot])
|
| 184 |
+
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
| 185 |
+
|
| 186 |
+
iface.launch()
|