Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
|
|
|
|
| 6 |
# Hugging Face Token
|
|
|
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
if not HF_TOKEN:
|
| 9 |
print("Error: HF_TOKEN not set.")
|
| 10 |
else:
|
| 11 |
print("HF_TOKEN loaded successfully.")
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
| 14 |
MEMORY_FILE = "memory.json"
|
| 15 |
|
| 16 |
def load_memory():
|
|
@@ -23,7 +38,9 @@ def save_memory(memory):
|
|
| 23 |
with open(MEMORY_FILE, "w") as f:
|
| 24 |
json.dump(memory, f)
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
| 27 |
def search_web(query):
|
| 28 |
try:
|
| 29 |
url = "https://api.duckduckgo.com/"
|
|
@@ -33,14 +50,16 @@ def search_web(query):
|
|
| 33 |
if data.get("AbstractText"):
|
| 34 |
return data["AbstractText"]
|
| 35 |
elif data.get("RelatedTopics"):
|
| 36 |
-
topics = [t.get("Text", "") for t in data
|
| 37 |
return " ".join(topics[:3])
|
| 38 |
else:
|
| 39 |
return "No useful information found."
|
| 40 |
except Exception as e:
|
| 41 |
return f"Search error: {e}"
|
| 42 |
|
| 43 |
-
#
|
|
|
|
|
|
|
| 44 |
def chat_with_model(message, history, context, file_input=None):
|
| 45 |
if history is None:
|
| 46 |
history = []
|
|
@@ -69,13 +88,15 @@ def chat_with_model(message, history, context, file_input=None):
|
|
| 69 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 70 |
"Content-Type": "application/json"
|
| 71 |
},
|
| 72 |
-
json={
|
|
|
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
if response.status_code != 200:
|
| 75 |
raise Exception(f"API Error: {response.status_code} - {response.text}")
|
| 76 |
|
| 77 |
reply = response.json()["choices"][0]["message"]["content"]
|
| 78 |
-
|
| 79 |
# Format math nicely
|
| 80 |
reply = reply.replace("Step", "\n\n**Step").replace(":", ":**").replace("\\[", "\n\n\\[").replace("\\]", "\\]\n\n")
|
| 81 |
|
|
@@ -90,22 +111,28 @@ def chat_with_model(message, history, context, file_input=None):
|
|
| 90 |
history.append({"role": "assistant", "content": error_msg})
|
| 91 |
return history, history
|
| 92 |
|
|
|
|
| 93 |
# Sidebar context update
|
|
|
|
| 94 |
def update_context(choice):
|
| 95 |
-
if
|
| 96 |
return "๐ **You are in General Mode.** Ask EduAI anything about your studies!"
|
| 97 |
return f"๐ **You selected {choice} mode.** Ask anything related to this topic!"
|
| 98 |
|
|
|
|
| 99 |
# Clear memory
|
|
|
|
| 100 |
def clear_memory():
|
| 101 |
if os.path.exists(MEMORY_FILE):
|
| 102 |
os.remove(MEMORY_FILE)
|
| 103 |
return [], "๐งน Chat memory cleared! Start fresh."
|
| 104 |
|
| 105 |
-
#
|
|
|
|
|
|
|
| 106 |
custom_css = """
|
| 107 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
|
| 108 |
-
body { font-family:
|
| 109 |
.header-title { font-size:36px; text-align:center; margin-bottom:20px; color:#007BFF; font-weight:700; text-shadow:0 0 10px rgba(0,123,255,0.5); }
|
| 110 |
.sidebar { background:#2a2a2a; padding:20px; border-radius:12px; box-shadow:0 4px 12px rgba(0,0,0,0.5); border:1px solid #333; }
|
| 111 |
.menu-title { font-size:20px; font-weight:500; margin-bottom:15px; color:#007BFF; }
|
|
@@ -116,7 +143,7 @@ body { font-family: 'Roboto', sans-serif; background:#1e1e1e; color:#e0e0e0; min
|
|
| 116 |
.context-box { background:#2a2a2a; padding:15px; border-radius:10px; box-shadow:0 2px 6px rgba(0,0,0,0.3); margin-bottom:15px; border:1px solid #333; color:#e0e0e0; }
|
| 117 |
.chatbox .message.user { background:#007BFF; color:#fff; border-radius:20px; padding:12px 18px; margin:8px 0; }
|
| 118 |
.chatbox .message.bot { background:#333; color:#e0e0e0; border-radius:20px; padding:12px 18px; margin:8px 0; }
|
| 119 |
-
.input-row { display:flex; gap:10px; }
|
| 120 |
.chat-input { flex:1; border-radius:12px; padding:12px; border:1px solid #555; background:#2a2a2a; color:#e0e0e0; }
|
| 121 |
.chat-input:focus { border-color:#007BFF; outline:none; }
|
| 122 |
.btn-send { background:#7c3aed; color:#fff; border-radius:12px; padding:12px 18px; border:none; cursor:pointer; transition:0.3s; }
|
|
@@ -129,7 +156,9 @@ body { font-family: 'Roboto', sans-serif; background:#1e1e1e; color:#e0e0e0; min
|
|
| 129 |
.radio label { color:#e0e0e0; margin:8px 0; }
|
| 130 |
"""
|
| 131 |
|
| 132 |
-
#
|
|
|
|
|
|
|
| 133 |
with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as iface:
|
| 134 |
gr.Markdown("# ๐ **EduAI โ Professional Study Companion**", elem_classes="header-title")
|
| 135 |
|
|
@@ -139,20 +168,65 @@ with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as iface:
|
|
| 139 |
gr.Markdown("### ๐งญ **Main Menu**", elem_classes="menu-title")
|
| 140 |
|
| 141 |
with gr.Accordion("๐ Subject Tutor", open=False):
|
| 142 |
-
subj = gr.Radio(
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
with gr.Accordion("๐ Study Planner", open=False):
|
| 145 |
-
planner = gr.Radio(
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
with gr.Accordion("๐ Languages", open=False):
|
| 148 |
-
lang = gr.Radio(
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
with gr.Accordion("โ๏ธ Settings", open=False):
|
| 151 |
clear_btn = gr.Button("๐งน Clear Memory", elem_classes="btn-clear")
|
| 152 |
|
| 153 |
with gr.Accordion("๐ฉโ๐ About", open=False):
|
| 154 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
# Main
|
| 157 |
with gr.Column(scale=4, elem_classes="main-chat"):
|
| 158 |
-
context_display = gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Perfect, Cherry! ๐ Hereโs a **complete, ready-to-run Gradio AI agent** with:
|
| 2 |
+
|
| 3 |
+
* โ
`type="messages"` properly implemented (no more incompatible data errors)
|
| 4 |
+
* โ
Modern **dark theme** UI with rounded chat bubbles
|
| 5 |
+
* โ
**Sidebar** with subjects, planner, languages, settings, and About section
|
| 6 |
+
* โ
**Floating send button + file upload**
|
| 7 |
+
* โ
**Context box** showing current mode
|
| 8 |
+
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
```python
|
| 12 |
import os
|
| 13 |
import gradio as gr
|
| 14 |
import requests
|
| 15 |
import json
|
| 16 |
|
| 17 |
+
# =======================
|
| 18 |
# Hugging Face Token
|
| 19 |
+
# =======================
|
| 20 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 21 |
if not HF_TOKEN:
|
| 22 |
print("Error: HF_TOKEN not set.")
|
| 23 |
else:
|
| 24 |
print("HF_TOKEN loaded successfully.")
|
| 25 |
|
| 26 |
+
# =======================
|
| 27 |
+
# Memory
|
| 28 |
+
# =======================
|
| 29 |
MEMORY_FILE = "memory.json"
|
| 30 |
|
| 31 |
def load_memory():
|
|
|
|
| 38 |
with open(MEMORY_FILE, "w") as f:
|
| 39 |
json.dump(memory, f)
|
| 40 |
|
| 41 |
+
# =======================
|
| 42 |
+
# Web search function
|
| 43 |
+
# =======================
|
| 44 |
def search_web(query):
|
| 45 |
try:
|
| 46 |
url = "https://api.duckduckgo.com/"
|
|
|
|
| 50 |
if data.get("AbstractText"):
|
| 51 |
return data["AbstractText"]
|
| 52 |
elif data.get("RelatedTopics"):
|
| 53 |
+
topics = [t.get("Text", "") for t in data.get("RelatedTopics", []) if "Text" in t]
|
| 54 |
return " ".join(topics[:3])
|
| 55 |
else:
|
| 56 |
return "No useful information found."
|
| 57 |
except Exception as e:
|
| 58 |
return f"Search error: {e}"
|
| 59 |
|
| 60 |
+
# =======================
|
| 61 |
+
# Chat function
|
| 62 |
+
# =======================
|
| 63 |
def chat_with_model(message, history, context, file_input=None):
|
| 64 |
if history is None:
|
| 65 |
history = []
|
|
|
|
| 88 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 89 |
"Content-Type": "application/json"
|
| 90 |
},
|
| 91 |
+
json={
|
| 92 |
+
"model": "deepseek-ai/DeepSeek-V3.2-Exp:novita",
|
| 93 |
+
"messages": conversation
|
| 94 |
+
}
|
| 95 |
)
|
| 96 |
if response.status_code != 200:
|
| 97 |
raise Exception(f"API Error: {response.status_code} - {response.text}")
|
| 98 |
|
| 99 |
reply = response.json()["choices"][0]["message"]["content"]
|
|
|
|
| 100 |
# Format math nicely
|
| 101 |
reply = reply.replace("Step", "\n\n**Step").replace(":", ":**").replace("\\[", "\n\n\\[").replace("\\]", "\\]\n\n")
|
| 102 |
|
|
|
|
| 111 |
history.append({"role": "assistant", "content": error_msg})
|
| 112 |
return history, history
|
| 113 |
|
| 114 |
+
# =======================
|
| 115 |
# Sidebar context update
|
| 116 |
+
# =======================
|
| 117 |
def update_context(choice):
|
| 118 |
+
if choice is None:
|
| 119 |
return "๐ **You are in General Mode.** Ask EduAI anything about your studies!"
|
| 120 |
return f"๐ **You selected {choice} mode.** Ask anything related to this topic!"
|
| 121 |
|
| 122 |
+
# =======================
|
| 123 |
# Clear memory
|
| 124 |
+
# =======================
|
| 125 |
def clear_memory():
|
| 126 |
if os.path.exists(MEMORY_FILE):
|
| 127 |
os.remove(MEMORY_FILE)
|
| 128 |
return [], "๐งน Chat memory cleared! Start fresh."
|
| 129 |
|
| 130 |
+
# =======================
|
| 131 |
+
# Custom CSS
|
| 132 |
+
# =======================
|
| 133 |
custom_css = """
|
| 134 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
|
| 135 |
+
body { font-family:'Roboto', sans-serif; background:#1e1e1e; color:#e0e0e0; min-height:100vh; }
|
| 136 |
.header-title { font-size:36px; text-align:center; margin-bottom:20px; color:#007BFF; font-weight:700; text-shadow:0 0 10px rgba(0,123,255,0.5); }
|
| 137 |
.sidebar { background:#2a2a2a; padding:20px; border-radius:12px; box-shadow:0 4px 12px rgba(0,0,0,0.5); border:1px solid #333; }
|
| 138 |
.menu-title { font-size:20px; font-weight:500; margin-bottom:15px; color:#007BFF; }
|
|
|
|
| 143 |
.context-box { background:#2a2a2a; padding:15px; border-radius:10px; box-shadow:0 2px 6px rgba(0,0,0,0.3); margin-bottom:15px; border:1px solid #333; color:#e0e0e0; }
|
| 144 |
.chatbox .message.user { background:#007BFF; color:#fff; border-radius:20px; padding:12px 18px; margin:8px 0; }
|
| 145 |
.chatbox .message.bot { background:#333; color:#e0e0e0; border-radius:20px; padding:12px 18px; margin:8px 0; }
|
| 146 |
+
.input-row { display:flex; gap:10px; margin-top:10px; }
|
| 147 |
.chat-input { flex:1; border-radius:12px; padding:12px; border:1px solid #555; background:#2a2a2a; color:#e0e0e0; }
|
| 148 |
.chat-input:focus { border-color:#007BFF; outline:none; }
|
| 149 |
.btn-send { background:#7c3aed; color:#fff; border-radius:12px; padding:12px 18px; border:none; cursor:pointer; transition:0.3s; }
|
|
|
|
| 156 |
.radio label { color:#e0e0e0; margin:8px 0; }
|
| 157 |
"""
|
| 158 |
|
| 159 |
+
# =======================
|
| 160 |
+
# Gradio UI
|
| 161 |
+
# =======================
|
| 162 |
with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as iface:
|
| 163 |
gr.Markdown("# ๐ **EduAI โ Professional Study Companion**", elem_classes="header-title")
|
| 164 |
|
|
|
|
| 168 |
gr.Markdown("### ๐งญ **Main Menu**", elem_classes="menu-title")
|
| 169 |
|
| 170 |
with gr.Accordion("๐ Subject Tutor", open=False):
|
| 171 |
+
subj = gr.Radio(
|
| 172 |
+
["Science ๐งช", "ICT ๐ป", "English ๐", "Mathematics โ"],
|
| 173 |
+
label="Choose a subject", type="index"
|
| 174 |
+
)
|
| 175 |
|
| 176 |
with gr.Accordion("๐ Study Planner", open=False):
|
| 177 |
+
planner = gr.Radio(
|
| 178 |
+
["View Plan ๐
", "Add Task โ๏ธ", "Study Tips ๐ก"],
|
| 179 |
+
label="Planner Options", type="index"
|
| 180 |
+
)
|
| 181 |
|
| 182 |
with gr.Accordion("๐ Languages", open=False):
|
| 183 |
+
lang = gr.Radio(
|
| 184 |
+
["Learn Sinhala ๐ฑ๐ฐ","Learn Tamil ๐ฎ๐ณ","Learn English ๐ฌ๐ง","Learn Spanish ๐ช๐ธ"],
|
| 185 |
+
label="Language Options", type="index"
|
| 186 |
+
)
|
| 187 |
|
| 188 |
with gr.Accordion("โ๏ธ Settings", open=False):
|
| 189 |
clear_btn = gr.Button("๐งน Clear Memory", elem_classes="btn-clear")
|
| 190 |
|
| 191 |
with gr.Accordion("๐ฉโ๐ About", open=False):
|
| 192 |
+
gr.Markdown(
|
| 193 |
+
"EduAI โ developed by **Wafa Fazly** using a pre-trained AI model. Helps learners understand **Science, ICT, English, and more**.",
|
| 194 |
+
elem_classes="about-text"
|
| 195 |
+
)
|
| 196 |
|
| 197 |
+
# Main Chat Area
|
| 198 |
with gr.Column(scale=4, elem_classes="main-chat"):
|
| 199 |
+
context_display = gr.Markdown(
|
| 200 |
+
"๐ **You are in General Mode.** Ask EduAI anything about your studies!",
|
| 201 |
+
elem_classes="context-box"
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
chatbot = gr.Chatbot(
|
| 205 |
+
label="EduAI Chat",
|
| 206 |
+
height=500,
|
| 207 |
+
render_markdown=True,
|
| 208 |
+
type="messages",
|
| 209 |
+
latex_delimiters=[{"left": "$$", "right": "$$", "display": True},
|
| 210 |
+
{"left": "\\[", "right": "\\]", "display": True}]
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# Input row
|
| 214 |
+
with gr.Row(elem_classes="input-row"):
|
| 215 |
+
msg = gr.Textbox(label="Ask EduAI:", placeholder="Type your question here...", elem_classes="chat-input", show_label=False)
|
| 216 |
+
file_input = gr.File(label="๐", file_types=[".pdf", ".docx", ".png", ".jpg"], elem_classes="file-upload", show_label=False)
|
| 217 |
+
send_btn = gr.Button("โ๏ธ", elem_classes="btn-send")
|
| 218 |
+
|
| 219 |
+
# =======================
|
| 220 |
+
# Event handlers
|
| 221 |
+
# =======================
|
| 222 |
+
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 223 |
+
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 224 |
+
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 225 |
+
send_btn.click(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, chatbot])
|
| 226 |
+
msg.submit(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, chatbot])
|
| 227 |
+
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
| 228 |
+
|
| 229 |
+
# Launch the app
|
| 230 |
+
iface.launch()
|
| 231 |
+
|
| 232 |
+
```
|