Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,13 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
# 🌍 Web search
|
| 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"):
|
|
@@ -37,36 +36,32 @@ def save_memory(memory):
|
|
| 37 |
|
| 38 |
memory = load_memory()
|
| 39 |
|
| 40 |
-
# 💬 Chat
|
| 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 |
-
|
| 49 |
-
history.append((message, f"🔎 Here's what I found online:\n\n{
|
| 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
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
-
"Keep answers neat, structured, and student-friendly."
|
| 60 |
)}
|
| 61 |
]
|
| 62 |
|
| 63 |
-
for
|
| 64 |
-
conversation.append({"role": "user", "content":
|
| 65 |
-
conversation.append({"role": "assistant", "content":
|
| 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",
|
|
@@ -79,53 +74,93 @@ def chat_with_model(message, history, context):
|
|
| 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 |
-
|
| 101 |
-
history.append((message, "😅 EduAI is having trouble connecting right now. Please try again later!"))
|
| 102 |
return history, history
|
| 103 |
|
| 104 |
-
# 📘
|
| 105 |
def update_context(choice):
|
| 106 |
if not choice:
|
| 107 |
-
return "📘 **You are in General Mode.** Ask EduAI anything
|
| 108 |
-
return f"📘 **You selected {choice} mode.**
|
| 109 |
|
| 110 |
-
# 🧹 Clear
|
| 111 |
def clear_memory():
|
| 112 |
if os.path.exists(MEMORY_FILE):
|
| 113 |
os.remove(MEMORY_FILE)
|
| 114 |
-
return [], "🧹
|
| 115 |
-
|
| 116 |
-
# 🎨
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
with gr.Accordion("📚 Subject Tutor", open=False):
|
| 131 |
subj = gr.Radio(
|
|
@@ -145,38 +180,30 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
| 145 |
label="Language Options"
|
| 146 |
)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
to help learners explore **Science, ICT, English, and more** —
|
| 157 |
-
in a smart and friendly way! 🌟
|
| 158 |
-
"""
|
| 159 |
-
)
|
| 160 |
|
| 161 |
-
with gr.Column(
|
| 162 |
-
context_display = gr.Markdown("📘 **You are in General Mode.** Ask EduAI anything
|
| 163 |
chatbot = gr.Chatbot(
|
| 164 |
-
label="
|
| 165 |
height=450,
|
| 166 |
-
render_markdown=True,
|
| 167 |
bubble_full_width=False,
|
| 168 |
-
|
| 169 |
-
{"left": "$$", "right": "$$", "display": True},
|
| 170 |
-
{"left": "\\[", "right": "\\]", "display": True}
|
| 171 |
-
]
|
| 172 |
)
|
| 173 |
msg = gr.Textbox(
|
| 174 |
-
|
| 175 |
-
|
| 176 |
)
|
| 177 |
send = gr.Button("✨ Send Message")
|
| 178 |
|
| 179 |
-
#
|
| 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)
|
|
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
| 6 |
+
# 🌍 Web search
|
| 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 |
if data.get("AbstractText"):
|
| 14 |
return data["AbstractText"]
|
| 15 |
elif data.get("RelatedTopics"):
|
|
|
|
| 36 |
|
| 37 |
memory = load_memory()
|
| 38 |
|
| 39 |
+
# 💬 Chat with AI
|
| 40 |
def chat_with_model(message, history, context):
|
| 41 |
if not isinstance(history, list):
|
| 42 |
history = []
|
| 43 |
|
|
|
|
| 44 |
if message.lower().startswith("search "):
|
| 45 |
query = message[7:]
|
| 46 |
+
result = search_web(query)
|
| 47 |
+
history.append((message, f"🔎 Here's what I found online:\n\n{result}"))
|
| 48 |
save_memory(history)
|
| 49 |
return history, history
|
| 50 |
|
|
|
|
| 51 |
conversation = [
|
| 52 |
{"role": "system", "content": (
|
| 53 |
+
"You are EduAI, a multilingual educational assistant created by a Sri Lankan student named Wafa Fazly. "
|
| 54 |
+
"Explain step-by-step like a teacher. Use Markdown and LaTeX for math. "
|
| 55 |
+
"Be friendly, clear, and structured."
|
|
|
|
| 56 |
)}
|
| 57 |
]
|
| 58 |
|
| 59 |
+
for u, b in history[-5:]:
|
| 60 |
+
conversation.append({"role": "user", "content": u})
|
| 61 |
+
conversation.append({"role": "assistant", "content": b})
|
| 62 |
|
| 63 |
conversation.append({"role": "user", "content": message})
|
| 64 |
|
|
|
|
| 65 |
try:
|
| 66 |
response = requests.post(
|
| 67 |
"https://router.huggingface.co/v1/chat/completions",
|
|
|
|
| 74 |
"messages": conversation
|
| 75 |
}
|
| 76 |
)
|
|
|
|
| 77 |
data = response.json()
|
| 78 |
reply = data["choices"][0]["message"]["content"]
|
| 79 |
|
|
|
|
| 80 |
reply = reply.replace("Step", "\n\n**Step")
|
| 81 |
reply = reply.replace(":", ":**")
|
| 82 |
reply = reply.replace("\\[", "\n\n\\[")
|
| 83 |
reply = reply.replace("\\]", "\\]\n\n")
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
history.append((message, reply))
|
| 86 |
save_memory(history)
|
| 87 |
return history, history
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
+
history.append((message, "😅 EduAI is having trouble connecting. Try again later!"))
|
|
|
|
| 91 |
return history, history
|
| 92 |
|
| 93 |
+
# 📘 Context update
|
| 94 |
def update_context(choice):
|
| 95 |
if not choice:
|
| 96 |
+
return "📘 **You are in General Mode.** Ask EduAI anything!"
|
| 97 |
+
return f"📘 **You selected {choice} mode.**"
|
| 98 |
|
| 99 |
+
# 🧹 Clear memory
|
| 100 |
def clear_memory():
|
| 101 |
if os.path.exists(MEMORY_FILE):
|
| 102 |
os.remove(MEMORY_FILE)
|
| 103 |
+
return [], "🧹 Memory cleared! Start fresh."
|
| 104 |
+
|
| 105 |
+
# 🎨 Custom CSS for modern look
|
| 106 |
+
custom_css = """
|
| 107 |
+
body {
|
| 108 |
+
background: linear-gradient(135deg, #f0e7ff 0%, #e8e0ff 50%, #f3e9ff 100%);
|
| 109 |
+
font-family: 'Inter', sans-serif;
|
| 110 |
+
}
|
| 111 |
+
.gradio-container {
|
| 112 |
+
background: transparent !important;
|
| 113 |
+
}
|
| 114 |
+
#eduai-main {
|
| 115 |
+
display: flex;
|
| 116 |
+
flex-direction: row;
|
| 117 |
+
gap: 20px;
|
| 118 |
+
margin-top: 15px;
|
| 119 |
+
}
|
| 120 |
+
.sidebar {
|
| 121 |
+
background: #ffffffdd;
|
| 122 |
+
border-radius: 20px;
|
| 123 |
+
padding: 18px;
|
| 124 |
+
box-shadow: 0 4px 20px rgba(100, 0, 200, 0.1);
|
| 125 |
+
width: 270px;
|
| 126 |
+
}
|
| 127 |
+
.chat-area {
|
| 128 |
+
background: #ffffff;
|
| 129 |
+
border-radius: 20px;
|
| 130 |
+
flex-grow: 1;
|
| 131 |
+
box-shadow: 0 4px 25px rgba(120, 0, 180, 0.15);
|
| 132 |
+
display: flex;
|
| 133 |
+
flex-direction: column;
|
| 134 |
+
padding: 15px 20px 20px 20px;
|
| 135 |
+
}
|
| 136 |
+
.chat-header {
|
| 137 |
+
text-align: center;
|
| 138 |
+
font-size: 1.6em;
|
| 139 |
+
font-weight: 700;
|
| 140 |
+
color: #5b21b6;
|
| 141 |
+
margin-bottom: 10px;
|
| 142 |
+
}
|
| 143 |
+
textarea, input, button {
|
| 144 |
+
border-radius: 12px !important;
|
| 145 |
+
}
|
| 146 |
+
button {
|
| 147 |
+
background: linear-gradient(135deg, #7c3aed, #9333ea) !important;
|
| 148 |
+
color: white !important;
|
| 149 |
+
border: none !important;
|
| 150 |
+
transition: 0.2s ease-in-out;
|
| 151 |
+
}
|
| 152 |
+
button:hover {
|
| 153 |
+
transform: scale(1.05);
|
| 154 |
+
}
|
| 155 |
+
"""
|
| 156 |
+
|
| 157 |
+
# 🚀 Interface
|
| 158 |
+
with gr.Blocks(css=custom_css) as iface:
|
| 159 |
+
gr.Markdown("<div class='chat-header'>🎓 EduAI — Your Smart Study Companion</div>")
|
| 160 |
+
|
| 161 |
+
with gr.Row(elem_id="eduai-main"):
|
| 162 |
+
with gr.Column(elem_classes=["sidebar"]):
|
| 163 |
+
gr.Markdown("### 🧭 Main Menu")
|
| 164 |
|
| 165 |
with gr.Accordion("📚 Subject Tutor", open=False):
|
| 166 |
subj = gr.Radio(
|
|
|
|
| 180 |
label="Language Options"
|
| 181 |
)
|
| 182 |
|
| 183 |
+
gr.Markdown("---")
|
| 184 |
+
clear_btn = gr.Button("🧹 Clear Memory")
|
| 185 |
+
|
| 186 |
+
gr.Markdown(
|
| 187 |
+
"👩🎓 **About EduAI** \n"
|
| 188 |
+
"Created by **Wafa Fazly** — a passionate Sri Lankan student 🇱🇰 \n"
|
| 189 |
+
"EduAI helps learners explore **Science, ICT, English**, and more 🌟"
|
| 190 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
with gr.Column(elem_classes=["chat-area"]):
|
| 193 |
+
context_display = gr.Markdown("📘 **You are in General Mode.** Ask EduAI anything!")
|
| 194 |
chatbot = gr.Chatbot(
|
| 195 |
+
label="",
|
| 196 |
height=450,
|
|
|
|
| 197 |
bubble_full_width=False,
|
| 198 |
+
render_markdown=True
|
|
|
|
|
|
|
|
|
|
| 199 |
)
|
| 200 |
msg = gr.Textbox(
|
| 201 |
+
placeholder="💬 Type your question here...",
|
| 202 |
+
show_label=False
|
| 203 |
)
|
| 204 |
send = gr.Button("✨ Send Message")
|
| 205 |
|
| 206 |
+
# ⚙️ Events
|
| 207 |
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 208 |
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 209 |
lang.change(update_context, inputs=lang, outputs=context_display)
|