Update app.py
Browse files
app.py
CHANGED
|
@@ -1,184 +1,72 @@
|
|
| 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 |
-
if message.lower().startswith("search "):
|
| 46 |
-
query = message[7:]
|
| 47 |
-
search_result = search_web(query)
|
| 48 |
-
history.append({"role": "user", "content": message})
|
| 49 |
-
history.append({"role": "assistant", "content": f"π Here's what I found online:\n\n{search_result}"})
|
| 50 |
-
save_memory(history)
|
| 51 |
-
return history, history
|
| 52 |
-
|
| 53 |
-
# System role
|
| 54 |
-
conversation = [{"role": "system", "content": (
|
| 55 |
-
"You are EduAI β an educational AI assistant created by Wafa Fazly "
|
| 56 |
-
"from Fathima Muslim Ladies College. "
|
| 57 |
-
"You help students learn subjects such as Math, Science, English, and IT. "
|
| 58 |
-
"EduAI runs on the model 'Qwen/Qwen3-VL-8B-Instruct', trained by Alibaba. "
|
| 59 |
-
"Always answer truthfully when asked about your creation."
|
| 60 |
-
)}]
|
| 61 |
-
|
| 62 |
-
for msg in history[-5:]:
|
| 63 |
-
conversation.append(msg)
|
| 64 |
-
|
| 65 |
-
conversation.append({"role": "user", "content": message})
|
| 66 |
-
|
| 67 |
-
# π Send to Hugging Face
|
| 68 |
-
try:
|
| 69 |
-
response = requests.post(
|
| 70 |
-
"https://router.huggingface.co/v1/chat/completions",
|
| 71 |
-
headers={
|
| 72 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
| 73 |
-
"Content-Type": "application/json"
|
| 74 |
-
},
|
| 75 |
-
json={
|
| 76 |
-
"model": "Qwen/Qwen3-VL-8B-Instruct:novita",
|
| 77 |
-
"messages": conversation
|
| 78 |
-
}
|
| 79 |
-
)
|
| 80 |
-
data = response.json()
|
| 81 |
-
reply = data["choices"][0]["message"]["content"]
|
| 82 |
-
history.append({"role": "user", "content": message})
|
| 83 |
-
history.append({"role": "assistant", "content": reply})
|
| 84 |
-
save_memory(history)
|
| 85 |
-
return history, history
|
| 86 |
-
except Exception as e:
|
| 87 |
-
history.append({"role": "assistant", "content": f"π
EduAI error: {e}"})
|
| 88 |
-
return history, history
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
# π Handle
|
| 92 |
def handle_file(file, history):
|
| 93 |
if file is None:
|
| 94 |
-
history
|
| 95 |
-
return history
|
| 96 |
filename = file.name.split("/")[-1]
|
| 97 |
-
history.append({"role": "assistant", "content": f"π File
|
| 98 |
return history
|
| 99 |
|
| 100 |
-
# βΈ Pause
|
| 101 |
def pause_chat(history):
|
| 102 |
-
history.append({"role": "assistant", "content": "βΈοΈ Chat paused.
|
| 103 |
return history
|
| 104 |
|
| 105 |
-
# π¨ Interface
|
| 106 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
| 107 |
-
gr.Markdown("# π **EduAI β Your Smart Study Companion**")
|
| 108 |
-
|
| 109 |
-
with gr.Row():
|
| 110 |
-
with gr.Column(scale=1, min_width=230):
|
| 111 |
-
gr.Markdown("### π§ **Main Menu**")
|
| 112 |
-
|
| 113 |
-
with gr.Accordion("π Subject Tutor", open=False):
|
| 114 |
-
subj = gr.Radio(
|
| 115 |
-
["Science οΏ½οΏ½", "ICT π»", "English π", "Mathematics β"],
|
| 116 |
-
label="Choose a subject"
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
with gr.Accordion("βοΈ Settings", open=False):
|
| 120 |
-
clear_btn = gr.Button("π§Ή Clear Memory")
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
label="π¬ EduAI Chat Window",
|
| 127 |
-
height=450,
|
| 128 |
-
type="messages"
|
| 129 |
-
)
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
msg = gr.Textbox(
|
| 141 |
-
placeholder="Ask EduAI anything...",
|
| 142 |
-
scale=6
|
| 143 |
-
)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
#
|
| 149 |
-
|
| 150 |
-
send.click(chat_with_model, inputs=[msg, chatbot, context_display], outputs=[chatbot, chatbot])
|
| 151 |
-
clear_btn.click(lambda: ([], "π§Ή Chat memory cleared!"), outputs=[chatbot, context_display])
|
| 152 |
upload_file.upload(handle_file, inputs=[upload_file, chatbot], outputs=chatbot)
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
parent.style.border = 'none';
|
| 166 |
-
parent.style.background = 'none';
|
| 167 |
-
parent.style.width = '32px';
|
| 168 |
-
parent.style.height = '32px';
|
| 169 |
-
parent.style.cursor = 'pointer';
|
| 170 |
-
parent.style.display = 'flex';
|
| 171 |
-
parent.style.alignItems = 'center';
|
| 172 |
-
parent.style.justifyContent = 'center';
|
| 173 |
-
const icon = document.createElement('span');
|
| 174 |
-
icon.textContent = 'π';
|
| 175 |
-
icon.style.fontSize = '22px';
|
| 176 |
-
icon.style.cursor = 'pointer';
|
| 177 |
-
parent.appendChild(icon);
|
| 178 |
-
icon.onclick = () => upload.click();
|
| 179 |
-
}
|
| 180 |
-
}
|
| 181 |
-
"""
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
# π Chatbot response logic
|
| 4 |
+
def eduai_chat(message, history):
|
| 5 |
+
if not message:
|
| 6 |
+
return history
|
| 7 |
+
if message.lower() == "hello":
|
| 8 |
+
response = "π Hi there! I'm EduAI, your smart study assistant!"
|
| 9 |
+
elif "chemistry" in message.lower():
|
| 10 |
+
response = "βοΈ Chemistry is fascinating! Would you like to learn about elements or reactions?"
|
| 11 |
+
elif "physics" in message.lower():
|
| 12 |
+
response = "βοΈ Physics explains the universe! Want to discuss motion, energy, or quantum concepts?"
|
| 13 |
+
else:
|
| 14 |
+
response = "I'm still learning! π Could you rephrase or ask another question?"
|
| 15 |
+
|
| 16 |
+
history.append({"role": "user", "content": message})
|
| 17 |
+
history.append({"role": "assistant", "content": response})
|
| 18 |
+
return history
|
| 19 |
|
| 20 |
+
# π Handle file uploads
|
| 21 |
def handle_file(file, history):
|
| 22 |
if file is None:
|
| 23 |
+
return history + [{"role": "assistant", "content": "β No file uploaded."}]
|
|
|
|
| 24 |
filename = file.name.split("/")[-1]
|
| 25 |
+
history.append({"role": "assistant", "content": f"π File '{filename}' received successfully!"})
|
| 26 |
return history
|
| 27 |
|
| 28 |
+
# βΈ Pause chat
|
| 29 |
def pause_chat(history):
|
| 30 |
+
history.append({"role": "assistant", "content": "βΈοΈ Chat paused. You can continue anytime!"})
|
| 31 |
return history
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# πΈ Build Interface
|
| 35 |
+
with gr.Blocks(title="EduAI - Your Smart Study Assistant") as demo:
|
| 36 |
+
gr.Markdown("## π§ **EduAI β Your Smart Study Assistant**")
|
| 37 |
+
gr.Markdown("Ask questions, upload files with π, or pause with βΈ anytime!")
|
| 38 |
|
| 39 |
+
chatbot = gr.Chatbot(label="EduAI", type="messages", height=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
with gr.Row():
|
| 42 |
+
# hidden uploader
|
| 43 |
+
upload_file = gr.File(
|
| 44 |
+
visible=False, # hidden but functional
|
| 45 |
+
file_count="single",
|
| 46 |
+
type="filepath"
|
| 47 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# file icon button
|
| 50 |
+
upload_icon = gr.Button("π", scale=0, elem_id="upload_icon")
|
| 51 |
+
msg = gr.Textbox(
|
| 52 |
+
placeholder="Type your message here...",
|
| 53 |
+
scale=6
|
| 54 |
+
)
|
| 55 |
+
send = gr.Button("β¨ Send", scale=1)
|
| 56 |
+
pause = gr.Button("βΈ Pause", scale=1)
|
| 57 |
|
| 58 |
+
# connect components
|
| 59 |
+
send.click(eduai_chat, inputs=[msg, chatbot], outputs=chatbot)
|
|
|
|
|
|
|
| 60 |
upload_file.upload(handle_file, inputs=[upload_file, chatbot], outputs=chatbot)
|
| 61 |
+
pause.click(pause_chat, inputs=chatbot, outputs=chatbot)
|
| 62 |
+
|
| 63 |
+
# πͺ small JavaScript trick to trigger hidden uploader
|
| 64 |
+
gr.HTML("""
|
| 65 |
+
<script>
|
| 66 |
+
const btn = document.querySelector('#upload_icon');
|
| 67 |
+
const input = document.querySelector('input[type=file]');
|
| 68 |
+
btn.addEventListener('click', () => input.click());
|
| 69 |
+
</script>
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|