Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
# ThinkPal – Hugging Face Space (Gradio)
|
| 3 |
|
| 4 |
import os, json, uuid, re, unicodedata
|
| 5 |
-
from difflib import get_close_matches
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
|
|
@@ -161,16 +161,7 @@ Finally, structure the roadmap into the following sections:
|
|
| 161 |
4. Milestones (by Phase)
|
| 162 |
"""
|
| 163 |
|
| 164 |
-
def _compose_profile(student: dict) -> str:
|
| 165 |
-
parts = []
|
| 166 |
-
for k, v in student.items():
|
| 167 |
-
if isinstance(v, list):
|
| 168 |
-
v = ", ".join(v)
|
| 169 |
-
if v:
|
| 170 |
-
parts.append(f"{k}: {v}")
|
| 171 |
-
return " | ".join(parts) if parts else "No data"
|
| 172 |
def get_gemini_response(query: str, student_data: dict | None = None) -> str:
|
| 173 |
-
"""Sends query to Gemini API with optional student data (ThinkPal context)."""
|
| 174 |
try:
|
| 175 |
if not genai:
|
| 176 |
return f"(Simulated) {query[:400]}..."
|
|
@@ -179,7 +170,6 @@ def get_gemini_response(query: str, student_data: dict | None = None) -> str:
|
|
| 179 |
personalized_prompt = query
|
| 180 |
|
| 181 |
if student_data:
|
| 182 |
-
# Build profile dynamically
|
| 183 |
profile_parts = []
|
| 184 |
for key, label in [
|
| 185 |
("learning_style", "Learning Style"),
|
|
@@ -223,9 +213,7 @@ Formatting requirements:
|
|
| 223 |
except Exception as e:
|
| 224 |
return f"(Gemini error fallback) {str(e)[:160]}"
|
| 225 |
|
| 226 |
-
|
| 227 |
def generate_ai_insights(student_data: dict) -> str:
|
| 228 |
-
"""Generates AI-driven insights based on student data."""
|
| 229 |
if not student_data:
|
| 230 |
return "Student data not available for generating insights."
|
| 231 |
|
|
@@ -268,7 +256,6 @@ Formatting requirements:
|
|
| 268 |
|
| 269 |
return get_gemini_response(insights_prompt, student_data)
|
| 270 |
|
| 271 |
-
|
| 272 |
# -----------------------------
|
| 273 |
# Chat logic
|
| 274 |
# -----------------------------
|
|
@@ -292,8 +279,19 @@ def chat(student_id: str, message: str) -> tuple[str, str, str]:
|
|
| 292 |
# -----------------------------
|
| 293 |
# Add / Update helpers
|
| 294 |
# -----------------------------
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
new_id = add_student(data)
|
| 298 |
return (
|
| 299 |
f"🎉 Created {new_id}",
|
|
@@ -303,11 +301,16 @@ def create_student(**kwargs):
|
|
| 303 |
|
| 304 |
def load_student_to_form(student_id: str):
|
| 305 |
s = get_student(student_id)
|
| 306 |
-
if not s: return [""] *
|
| 307 |
return [", ".join(v) if isinstance(v, list) else v for v in s.values()]
|
| 308 |
|
| 309 |
-
def apply_update(student_id, *
|
| 310 |
-
updates = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
msg = update_student(student_id, updates)
|
| 312 |
return msg, json.dumps(get_student(student_id) or {}, ensure_ascii=False, indent=2)
|
| 313 |
|
|
@@ -341,10 +344,7 @@ with gr.Blocks(theme=THEME, css=CUSTOM_CSS) as demo:
|
|
| 341 |
ask_btn.click(fn=chat, inputs=[student_dd, user_msg], outputs=[roadmap_out, insights_out, chatbot_out])
|
| 342 |
|
| 343 |
with gr.Tab("➕ Add Student"):
|
| 344 |
-
fields = {k: gr.Textbox(label=k.replace("_", " ").title()) for k in
|
| 345 |
-
["learning_style","academic_progress","personality","interests","goals","level",
|
| 346 |
-
"preferred_methods","iq_level","eq_level","decision_making_style",
|
| 347 |
-
"motivation_level","preferred_study_environment","community_groups"]}
|
| 348 |
create_btn = gr.Button("Create Profile", variant="primary")
|
| 349 |
status_new = gr.Textbox(label="Status")
|
| 350 |
preview_new = gr.Textbox(label="Saved Profile", lines=10)
|
|
@@ -353,10 +353,7 @@ with gr.Blocks(theme=THEME, css=CUSTOM_CSS) as demo:
|
|
| 353 |
with gr.Tab("✏️ Update Student"):
|
| 354 |
target_id = gr.Dropdown(label="Student", choices=list_student_ids() or [])
|
| 355 |
load_btn = gr.Button("Load Profile")
|
| 356 |
-
upd_fields = {k: gr.Textbox(label=k.replace("_", " ").title()) for k in
|
| 357 |
-
["learning_style","academic_progress","personality","interests","goals","level",
|
| 358 |
-
"preferred_methods","iq_level","eq_level","decision_making_style",
|
| 359 |
-
"motivation_level","preferred_study_environment","community_groups"]}
|
| 360 |
save_btn = gr.Button("Save Changes", variant="primary")
|
| 361 |
status_upd = gr.Textbox(label="Status")
|
| 362 |
preview_upd = gr.Textbox(label="Updated Profile", lines=10)
|
|
@@ -364,4 +361,4 @@ with gr.Blocks(theme=THEME, css=CUSTOM_CSS) as demo:
|
|
| 364 |
save_btn.click(fn=apply_update, inputs=[target_id]+list(upd_fields.values()), outputs=[status_upd, preview_upd])
|
| 365 |
|
| 366 |
if __name__ == "__main__":
|
| 367 |
-
demo.launch()
|
|
|
|
| 2 |
# ThinkPal – Hugging Face Space (Gradio)
|
| 3 |
|
| 4 |
import os, json, uuid, re, unicodedata
|
| 5 |
+
from difflib import get_close_matches
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
|
|
|
|
| 161 |
4. Milestones (by Phase)
|
| 162 |
"""
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
def get_gemini_response(query: str, student_data: dict | None = None) -> str:
|
|
|
|
| 165 |
try:
|
| 166 |
if not genai:
|
| 167 |
return f"(Simulated) {query[:400]}..."
|
|
|
|
| 170 |
personalized_prompt = query
|
| 171 |
|
| 172 |
if student_data:
|
|
|
|
| 173 |
profile_parts = []
|
| 174 |
for key, label in [
|
| 175 |
("learning_style", "Learning Style"),
|
|
|
|
| 213 |
except Exception as e:
|
| 214 |
return f"(Gemini error fallback) {str(e)[:160]}"
|
| 215 |
|
|
|
|
| 216 |
def generate_ai_insights(student_data: dict) -> str:
|
|
|
|
| 217 |
if not student_data:
|
| 218 |
return "Student data not available for generating insights."
|
| 219 |
|
|
|
|
| 256 |
|
| 257 |
return get_gemini_response(insights_prompt, student_data)
|
| 258 |
|
|
|
|
| 259 |
# -----------------------------
|
| 260 |
# Chat logic
|
| 261 |
# -----------------------------
|
|
|
|
| 279 |
# -----------------------------
|
| 280 |
# Add / Update helpers
|
| 281 |
# -----------------------------
|
| 282 |
+
FIELDS = [
|
| 283 |
+
"learning_style","academic_progress","personality","interests","goals","level",
|
| 284 |
+
"preferred_methods","iq_level","eq_level","decision_making_style",
|
| 285 |
+
"motivation_level","preferred_study_environment","community_groups"
|
| 286 |
+
]
|
| 287 |
+
|
| 288 |
+
def create_student(*args):
|
| 289 |
+
data = {}
|
| 290 |
+
for k, v in zip(FIELDS, args):
|
| 291 |
+
if "methods" in k or "groups" in k:
|
| 292 |
+
data[k] = v.split(",") if v else []
|
| 293 |
+
else:
|
| 294 |
+
data[k] = v
|
| 295 |
new_id = add_student(data)
|
| 296 |
return (
|
| 297 |
f"🎉 Created {new_id}",
|
|
|
|
| 301 |
|
| 302 |
def load_student_to_form(student_id: str):
|
| 303 |
s = get_student(student_id)
|
| 304 |
+
if not s: return [""] * len(FIELDS)
|
| 305 |
return [", ".join(v) if isinstance(v, list) else v for v in s.values()]
|
| 306 |
|
| 307 |
+
def apply_update(student_id, *args):
|
| 308 |
+
updates = {}
|
| 309 |
+
for k, v in zip(FIELDS, args):
|
| 310 |
+
if "methods" in k or "groups" in k:
|
| 311 |
+
updates[k] = v.split(",") if v else []
|
| 312 |
+
else:
|
| 313 |
+
updates[k] = v
|
| 314 |
msg = update_student(student_id, updates)
|
| 315 |
return msg, json.dumps(get_student(student_id) or {}, ensure_ascii=False, indent=2)
|
| 316 |
|
|
|
|
| 344 |
ask_btn.click(fn=chat, inputs=[student_dd, user_msg], outputs=[roadmap_out, insights_out, chatbot_out])
|
| 345 |
|
| 346 |
with gr.Tab("➕ Add Student"):
|
| 347 |
+
fields = {k: gr.Textbox(label=k.replace("_", " ").title()) for k in FIELDS}
|
|
|
|
|
|
|
|
|
|
| 348 |
create_btn = gr.Button("Create Profile", variant="primary")
|
| 349 |
status_new = gr.Textbox(label="Status")
|
| 350 |
preview_new = gr.Textbox(label="Saved Profile", lines=10)
|
|
|
|
| 353 |
with gr.Tab("✏️ Update Student"):
|
| 354 |
target_id = gr.Dropdown(label="Student", choices=list_student_ids() or [])
|
| 355 |
load_btn = gr.Button("Load Profile")
|
| 356 |
+
upd_fields = {k: gr.Textbox(label=k.replace("_", " ").title()) for k in FIELDS}
|
|
|
|
|
|
|
|
|
|
| 357 |
save_btn = gr.Button("Save Changes", variant="primary")
|
| 358 |
status_upd = gr.Textbox(label="Status")
|
| 359 |
preview_upd = gr.Textbox(label="Updated Profile", lines=10)
|
|
|
|
| 361 |
save_btn.click(fn=apply_update, inputs=[target_id]+list(upd_fields.values()), outputs=[status_upd, preview_upd])
|
| 362 |
|
| 363 |
if __name__ == "__main__":
|
| 364 |
+
demo.launch()
|