Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,142 +2,153 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return json.load(f)
|
| 15 |
-
return []
|
| 16 |
-
|
| 17 |
-
def save_memory(memory):
|
| 18 |
-
with open(MEMORY_FILE, "w") as f:
|
| 19 |
-
json.dump(memory, f)
|
| 20 |
-
|
| 21 |
-
memory = load_memory()
|
| 22 |
-
|
| 23 |
-
# π¬ Chat function with Hugging Face
|
| 24 |
-
def ai_response(message, history, context, file_input=None):
|
| 25 |
-
if not isinstance(history, list):
|
| 26 |
history = []
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
{"role":"system", "content":(
|
| 33 |
-
"You are EduAI, a multilingual educational AI assistant. "
|
| 34 |
-
"Explain answers like a professional tutor, use Markdown/LaTeX for math, "
|
| 35 |
-
"and keep answers neat and student-friendly."
|
| 36 |
-
)}
|
| 37 |
-
]
|
| 38 |
|
| 39 |
for past_user, past_bot in history[-5:]:
|
| 40 |
-
conversation.append({"role":"user","content":past_user})
|
| 41 |
-
conversation.append({"role":"assistant","content":past_bot})
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
try:
|
| 45 |
response = requests.post(
|
| 46 |
-
"https://
|
| 47 |
-
headers={
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
data = response.json()
|
| 51 |
reply = data["choices"][0]["message"]["content"]
|
| 52 |
history.append((message, reply))
|
| 53 |
-
|
| 54 |
-
return history, history
|
| 55 |
except Exception as e:
|
| 56 |
history.append((message, f"β Error contacting model: {e}"))
|
| 57 |
-
return history, history
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
quiz_html += "</div>"
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
return
|
| 84 |
-
|
| 85 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
custom_css = """
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
button.primary {background
|
| 96 |
-
|
| 97 |
-
input[type=checkbox] {accent-color:#a855f7;}
|
| 98 |
"""
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
gr.Markdown("<h1 style='text-align:center; color:#A855F7;'>π EduAI β Smart Study Companion</h1>")
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
-
# π Left Sidebar
|
| 106 |
with gr.Column(scale=1, min_width=250):
|
| 107 |
-
gr.Markdown("### π§
|
|
|
|
| 108 |
with gr.Accordion("π Study Tutor", open=False):
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
with gr.Accordion("π§ MCQ Generator", open=False):
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
with gr.Accordion("
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
with gr.Accordion("π‘ Study Adviser", open=False):
|
| 120 |
-
adviser_input = gr.Textbox(label="Ask for study advice")
|
| 121 |
adviser_btn = gr.Button("Get Advice", elem_classes="primary")
|
| 122 |
-
adviser_output = gr.HTML("<i>Advice will appear here...</i>")
|
| 123 |
|
| 124 |
-
with gr.Accordion("
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
context_display = gr.Markdown("π **General Mode.** Ask EduAI anything!")
|
| 130 |
-
chatbot = gr.Chatbot(label="EduAI Chat", elem_id="chatbot", height=450, type="messages")
|
| 131 |
user_input = gr.Textbox(label="Type your question:")
|
| 132 |
-
send_btn = gr.Button("Send
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
#
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
iface.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
+
import random
|
| 6 |
|
| 7 |
+
# Hugging Face API token
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
+
# =========================
|
| 11 |
+
# AI response via DeepSeek
|
| 12 |
+
# =========================
|
| 13 |
+
def ai_response(message, history=None):
|
| 14 |
+
if history is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
history = []
|
| 16 |
|
| 17 |
+
conversation = [{"role": "system", "content": (
|
| 18 |
+
"You are EduAI, a professional multilingual educational assistant. "
|
| 19 |
+
"Explain clearly with examples, use step-by-step for math, and Markdown/LaTeX for equations."
|
| 20 |
+
)}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
for past_user, past_bot in history[-5:]:
|
| 23 |
+
conversation.append({"role": "user", "content": past_user})
|
| 24 |
+
conversation.append({"role": "assistant", "content": past_bot})
|
| 25 |
+
|
| 26 |
+
conversation.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
try:
|
| 29 |
response = requests.post(
|
| 30 |
+
"https://api-inference.huggingface.co/v1/chat/completions",
|
| 31 |
+
headers={
|
| 32 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 33 |
+
"Content-Type": "application/json"
|
| 34 |
+
},
|
| 35 |
+
json={
|
| 36 |
+
"model": "deepseek-ai/DeepSeek-V3.2-Exp:novita",
|
| 37 |
+
"messages": conversation
|
| 38 |
+
}
|
| 39 |
)
|
| 40 |
data = response.json()
|
| 41 |
reply = data["choices"][0]["message"]["content"]
|
| 42 |
history.append((message, reply))
|
| 43 |
+
return history, reply
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
history.append((message, f"β Error contacting model: {e}"))
|
| 46 |
+
return history, history[-1][1]
|
| 47 |
+
|
| 48 |
+
# =========================
|
| 49 |
+
# MCQ Generator (AI-based)
|
| 50 |
+
# =========================
|
| 51 |
+
def generate_mcq(subject):
|
| 52 |
+
prompt = f"Create 3 multiple-choice questions with 4 options each and the correct answer for {subject}. Return as JSON like: [{{'question':'...','options':['..'],'answer':'..'}}, ...]"
|
| 53 |
+
history, reply = ai_response(prompt)
|
| 54 |
try:
|
| 55 |
+
mcqs = json.loads(reply)
|
| 56 |
+
except:
|
| 57 |
+
return f"β Error generating MCQs: {reply}"
|
| 58 |
+
|
| 59 |
+
quiz_html = "<div style='font-family:Poppins, sans-serif; color:#EEE;'>"
|
| 60 |
+
for i, q in enumerate(mcqs, 1):
|
| 61 |
+
quiz_html += f"<div style='margin-bottom:15px; padding:12px; border-radius:10px; background:#2a2a2a;'>"
|
| 62 |
+
quiz_html += f"<b style='color:#7C3AED;'>{i}. {q['question']}</b><br>"
|
| 63 |
+
for opt in q['options']:
|
| 64 |
+
quiz_html += f"<label style='display:block; margin:5px 0;'><input type='checkbox' value='{opt}' style='accent-color:#7C3AED;'> {opt}</label>"
|
| 65 |
+
quiz_html += f"<details style='color:#fff;'><summary>Show Answer</summary>Answer: {q['answer']}</details>"
|
| 66 |
quiz_html += "</div>"
|
| 67 |
+
quiz_html += "</div>"
|
| 68 |
+
return quiz_html
|
| 69 |
+
|
| 70 |
+
# =========================
|
| 71 |
+
# Study Adviser
|
| 72 |
+
# =========================
|
| 73 |
+
def study_adviser(question):
|
| 74 |
+
history, answer = ai_response(f"Give a professional study advice: {question}")
|
| 75 |
+
return answer
|
| 76 |
+
|
| 77 |
+
# =========================
|
| 78 |
+
# Study Planner
|
| 79 |
+
# =========================
|
| 80 |
+
planner_tasks = []
|
| 81 |
+
|
| 82 |
+
def add_task(task):
|
| 83 |
+
planner_tasks.append(task)
|
| 84 |
+
return "\n".join([f"- {t}" for t in planner_tasks])
|
| 85 |
+
|
| 86 |
+
def view_plan():
|
| 87 |
+
if not planner_tasks:
|
| 88 |
+
return "No tasks added yet."
|
| 89 |
+
return "\n".join([f"- {t}" for t in planner_tasks])
|
| 90 |
+
|
| 91 |
+
# =========================
|
| 92 |
+
# Gradio UI
|
| 93 |
+
# =========================
|
| 94 |
custom_css = """
|
| 95 |
+
body {background:#121212; color:#EEE; font-family:Poppins, sans-serif;}
|
| 96 |
+
button.primary {background:#7C3AED; color:white; border-radius:8px; padding:5px 12px;}
|
| 97 |
+
button.primary:hover {background:#6D28D9;}
|
| 98 |
+
.gradio-container {background:#121212;}
|
|
|
|
| 99 |
"""
|
| 100 |
|
| 101 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Dark()) as iface:
|
| 102 |
+
gr.Markdown("<h1 style='text-align:center; color:#7C3AED;'>π EduAI β Smart Study Companion</h1>")
|
|
|
|
| 103 |
|
| 104 |
with gr.Row():
|
|
|
|
| 105 |
with gr.Column(scale=1, min_width=250):
|
| 106 |
+
gr.Markdown("### π§ Main Menu")
|
| 107 |
+
|
| 108 |
with gr.Accordion("π Study Tutor", open=False):
|
| 109 |
+
subject_tutor = gr.Radio(["Math", "Science", "ICT", "English"], label="Select Subject")
|
| 110 |
+
tutor_input = gr.Textbox(label="Ask your question:")
|
| 111 |
+
tutor_output = gr.Textbox(label="Answer:")
|
| 112 |
+
tutor_btn = gr.Button("Get Answer", elem_classes="primary")
|
| 113 |
|
| 114 |
with gr.Accordion("π§ MCQ Generator", open=False):
|
| 115 |
+
mcq_subject = gr.Radio(["Math", "Science", "ICT", "English"], label="Select Subject")
|
| 116 |
+
mcq_output = gr.HTML("<i>Click 'Generate Quiz' to start!</i>")
|
| 117 |
+
mcq_btn = gr.Button("Generate Quiz", elem_classes="primary")
|
| 118 |
|
| 119 |
+
with gr.Accordion("π Study Adviser", open=False):
|
| 120 |
+
adviser_input = gr.Textbox(label="Ask for study advice:")
|
| 121 |
+
adviser_output = gr.Textbox(label="Advice:")
|
|
|
|
|
|
|
| 122 |
adviser_btn = gr.Button("Get Advice", elem_classes="primary")
|
|
|
|
| 123 |
|
| 124 |
+
with gr.Accordion("π Study Planner", open=False):
|
| 125 |
+
task_input = gr.Textbox(label="Add a Task:")
|
| 126 |
+
add_task_btn = gr.Button("Add Task", elem_classes="primary")
|
| 127 |
+
view_plan_btn = gr.Button("View Plan", elem_classes="primary")
|
| 128 |
+
planner_output = gr.Textbox(label="Planner Tasks:")
|
| 129 |
|
| 130 |
+
with gr.Column(scale=4):
|
| 131 |
+
chatbot_display = gr.Chatbot(label="π¬ EduAI Chat", height=480, type="messages")
|
|
|
|
|
|
|
| 132 |
user_input = gr.Textbox(label="Type your question:")
|
| 133 |
+
send_btn = gr.Button("Send", elem_classes="primary")
|
| 134 |
+
|
| 135 |
+
# =========================
|
| 136 |
+
# Event Bindings
|
| 137 |
+
# =========================
|
| 138 |
+
# Study Tutor
|
| 139 |
+
tutor_btn.click(ai_response, inputs=[tutor_input, gr.State(value=[])], outputs=[gr.State(value=[]), tutor_output])
|
| 140 |
+
|
| 141 |
+
# MCQ Generator
|
| 142 |
+
mcq_btn.click(generate_mcq, inputs=[mcq_subject], outputs=[mcq_output])
|
| 143 |
+
|
| 144 |
+
# Study Adviser
|
| 145 |
+
adviser_btn.click(study_adviser, inputs=[adviser_input], outputs=[adviser_output])
|
| 146 |
+
|
| 147 |
+
# Study Planner
|
| 148 |
+
add_task_btn.click(add_task, inputs=[task_input], outputs=[planner_output])
|
| 149 |
+
view_plan_btn.click(view_plan, outputs=[planner_output])
|
| 150 |
+
|
| 151 |
+
# Chatbot
|
| 152 |
+
send_btn.click(ai_response, inputs=[user_input, gr.State(value=[])], outputs=[gr.State(value=[]), chatbot_display])
|
| 153 |
|
| 154 |
iface.launch()
|