Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
# ๐ Web search function
|
| 7 |
def search_web(query):
|
|
@@ -15,7 +16,7 @@ def search_web(query):
|
|
| 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:
|
|
@@ -48,11 +49,11 @@ def chat_with_model(message, history, context, file_input=None):
|
|
| 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,
|
| 52 |
|
| 53 |
# ๐ If file is uploaded
|
| 54 |
if file_input:
|
| 55 |
-
file_name = file_input.name
|
| 56 |
message += f"\n\n๐ (User uploaded a file named '{file_name}')"
|
| 57 |
|
| 58 |
# ๐ง Build conversation
|
|
@@ -80,7 +81,7 @@ def chat_with_model(message, history, context, file_input=None):
|
|
| 80 |
"Content-Type": "application/json"
|
| 81 |
},
|
| 82 |
json={
|
| 83 |
-
"model": "deepseek-ai/DeepSeek-
|
| 84 |
"messages": conversation
|
| 85 |
}
|
| 86 |
)
|
|
@@ -89,23 +90,18 @@ def chat_with_model(message, history, context, file_input=None):
|
|
| 89 |
reply = data["choices"][0]["message"]["content"]
|
| 90 |
|
| 91 |
# ๐งฎ Clean up math formatting
|
| 92 |
-
reply =
|
| 93 |
-
reply = reply.replace(":", ":**")
|
| 94 |
reply = reply.replace("\\[", "\n\n\\[")
|
| 95 |
reply = reply.replace("\\]", "\\]\n\n")
|
| 96 |
|
| 97 |
-
# โ
Add Markdown + LaTeX support
|
| 98 |
-
if "\\" in reply or "log_" in reply or "^" in reply:
|
| 99 |
-
reply = f"{reply}"
|
| 100 |
-
|
| 101 |
history.append((message, reply))
|
| 102 |
save_memory(history)
|
| 103 |
-
return history,
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
print("Error:", e)
|
| 107 |
history.append((message, "๐
EduAI is having trouble connecting right now. Please try again later!"))
|
| 108 |
-
return history,
|
| 109 |
|
| 110 |
# ๐ Sidebar context update
|
| 111 |
def update_context(choice):
|
|
@@ -119,6 +115,76 @@ def clear_memory():
|
|
| 119 |
os.remove(MEMORY_FILE)
|
| 120 |
return [], "๐งน Chat memory cleared! Start fresh."
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
# ๐จ Gradio Interface
|
| 123 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
| 124 |
gr.Markdown("# ๐ **EduAI โ Your Smart Study Companion**")
|
|
@@ -145,6 +211,12 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
| 145 |
label="Language Options"
|
| 146 |
)
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
with gr.Accordion("โ๏ธ Settings", open=False):
|
| 149 |
clear_btn = gr.Button("๐งน Clear Memory")
|
| 150 |
|
|
@@ -171,7 +243,9 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
|
| 171 |
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 172 |
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 173 |
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 174 |
-
send.click(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot,
|
|
|
|
| 175 |
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
|
|
|
| 176 |
|
| 177 |
-
iface.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
# ๐ Web search function
|
| 8 |
def search_web(query):
|
|
|
|
| 16 |
return data["AbstractText"]
|
| 17 |
elif data.get("RelatedTopics"):
|
| 18 |
topics = [t.get("Text", "") for t in data["RelatedTopics"] if "Text" in t]
|
| 19 |
+
return " ".join(topics[:3])
|
| 20 |
else:
|
| 21 |
return "No useful information found."
|
| 22 |
except Exception as e:
|
|
|
|
| 49 |
search_result = search_web(query)
|
| 50 |
history.append((message, f"๐ Here's what I found online:\n\n{search_result}"))
|
| 51 |
save_memory(history)
|
| 52 |
+
return history, ""
|
| 53 |
|
| 54 |
# ๐ If file is uploaded
|
| 55 |
if file_input:
|
| 56 |
+
file_name = os.path.basename(file_input.name)
|
| 57 |
message += f"\n\n๐ (User uploaded a file named '{file_name}')"
|
| 58 |
|
| 59 |
# ๐ง Build conversation
|
|
|
|
| 81 |
"Content-Type": "application/json"
|
| 82 |
},
|
| 83 |
json={
|
| 84 |
+
"model": "deepseek-ai/DeepSeek-V2.5",
|
| 85 |
"messages": conversation
|
| 86 |
}
|
| 87 |
)
|
|
|
|
| 90 |
reply = data["choices"][0]["message"]["content"]
|
| 91 |
|
| 92 |
# ๐งฎ Clean up math formatting
|
| 93 |
+
reply = re.sub(r"\bStep (\d+):", r"\n\n**Step \1:**", reply)
|
|
|
|
| 94 |
reply = reply.replace("\\[", "\n\n\\[")
|
| 95 |
reply = reply.replace("\\]", "\\]\n\n")
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
history.append((message, reply))
|
| 98 |
save_memory(history)
|
| 99 |
+
return history, ""
|
| 100 |
|
| 101 |
except Exception as e:
|
| 102 |
print("Error:", e)
|
| 103 |
history.append((message, "๐
EduAI is having trouble connecting right now. Please try again later!"))
|
| 104 |
+
return history, ""
|
| 105 |
|
| 106 |
# ๐ Sidebar context update
|
| 107 |
def update_context(choice):
|
|
|
|
| 115 |
os.remove(MEMORY_FILE)
|
| 116 |
return [], "๐งน Chat memory cleared! Start fresh."
|
| 117 |
|
| 118 |
+
# ๐งฉ Dynamic MCQ Quiz Generator
|
| 119 |
+
def generate_dynamic_quiz(topic):
|
| 120 |
+
try:
|
| 121 |
+
if not topic.strip():
|
| 122 |
+
return [gr.Markdown("โ ๏ธ Please enter a topic to generate the quiz.")]
|
| 123 |
+
|
| 124 |
+
prompt = f"""
|
| 125 |
+
Create 5 multiple-choice questions about "{topic}".
|
| 126 |
+
Each question should have 4 options (A, B, C, D) and only one correct answer.
|
| 127 |
+
Format the response in JSON like this:
|
| 128 |
+
[
|
| 129 |
+
{{
|
| 130 |
+
"question": "...",
|
| 131 |
+
"options": ["...", "...", "...", "..."],
|
| 132 |
+
"answer": "..."
|
| 133 |
+
}},
|
| 134 |
+
...
|
| 135 |
+
]
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
response = requests.post(
|
| 139 |
+
"https://router.huggingface.co/v1/chat/completions",
|
| 140 |
+
headers={
|
| 141 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 142 |
+
"Content-Type": "application/json"
|
| 143 |
+
},
|
| 144 |
+
json={
|
| 145 |
+
"model": "deepseek-ai/DeepSeek-V2.5",
|
| 146 |
+
"messages": [{"role": "user", "content": prompt}]
|
| 147 |
+
}
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
data = response.json()
|
| 151 |
+
raw_text = data["choices"][0]["message"]["content"]
|
| 152 |
+
|
| 153 |
+
# ๐งฉ Try parsing JSON
|
| 154 |
+
try:
|
| 155 |
+
quiz_data = json.loads(raw_text)
|
| 156 |
+
except:
|
| 157 |
+
quiz_data = []
|
| 158 |
+
for block in raw_text.split("\n\n"):
|
| 159 |
+
if "?" in block:
|
| 160 |
+
parts = block.split("\n")
|
| 161 |
+
q = parts[0]
|
| 162 |
+
opts = [p.strip("- ").strip() for p in parts[1:5] if p.strip()]
|
| 163 |
+
if len(opts) == 4:
|
| 164 |
+
quiz_data.append({
|
| 165 |
+
"question": q,
|
| 166 |
+
"options": opts,
|
| 167 |
+
"answer": ""
|
| 168 |
+
})
|
| 169 |
+
|
| 170 |
+
# ๐ฏ Build UI dynamically
|
| 171 |
+
quiz_elements = []
|
| 172 |
+
for q in quiz_data:
|
| 173 |
+
quiz_elements.append(gr.Markdown(f"### โ {q['question']}"))
|
| 174 |
+
quiz_elements.append(gr.CheckboxGroup(
|
| 175 |
+
choices=q["options"],
|
| 176 |
+
label="Select your answer:",
|
| 177 |
+
interactive=True
|
| 178 |
+
))
|
| 179 |
+
|
| 180 |
+
if not quiz_elements:
|
| 181 |
+
return [gr.Markdown("๐
Couldn't generate a proper quiz. Try another topic!")]
|
| 182 |
+
|
| 183 |
+
return quiz_elements
|
| 184 |
+
|
| 185 |
+
except Exception as e:
|
| 186 |
+
return [gr.Markdown(f"Error generating quiz: {e}")]
|
| 187 |
+
|
| 188 |
# ๐จ Gradio Interface
|
| 189 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
| 190 |
gr.Markdown("# ๐ **EduAI โ Your Smart Study Companion**")
|
|
|
|
| 211 |
label="Language Options"
|
| 212 |
)
|
| 213 |
|
| 214 |
+
# ๐งฉ MCQ Quiz Generator
|
| 215 |
+
with gr.Accordion("๐งฉ MCQ Quiz Generator", open=False):
|
| 216 |
+
quiz_topic = gr.Textbox(label="Enter a topic (e.g., Photosynthesis, AI, Newton's Laws):")
|
| 217 |
+
generate_quiz_btn = gr.Button("Generate Quiz ๐ง ")
|
| 218 |
+
quiz_output = gr.Column()
|
| 219 |
+
|
| 220 |
with gr.Accordion("โ๏ธ Settings", open=False):
|
| 221 |
clear_btn = gr.Button("๐งน Clear Memory")
|
| 222 |
|
|
|
|
| 243 |
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 244 |
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 245 |
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 246 |
+
send.click(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, msg])
|
| 247 |
+
msg.submit(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, msg])
|
| 248 |
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
| 249 |
+
generate_quiz_btn.click(generate_dynamic_quiz, inputs=quiz_topic, outputs=quiz_output)
|
| 250 |
|
| 251 |
+
iface.launch()
|