Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,115 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
DATA_FILE = "student_profiles.json"
|
| 6 |
+
|
| 7 |
+
# ---------- Data Storage ----------
|
| 8 |
+
def load_student_data():
|
| 9 |
+
if not os.path.exists(DATA_FILE):
|
| 10 |
+
return {}
|
| 11 |
+
with open(DATA_FILE, "r", encoding="utf-8") as f:
|
| 12 |
+
return json.load(f)
|
| 13 |
+
|
| 14 |
+
def save_student_data(data):
|
| 15 |
+
with open(DATA_FILE, "w", encoding="utf-8") as f:
|
| 16 |
+
json.dump(data, f, indent=4, ensure_ascii=False)
|
| 17 |
+
|
| 18 |
+
def generate_new_student_id():
|
| 19 |
+
data = load_student_data()
|
| 20 |
+
if not data:
|
| 21 |
+
return "student_1"
|
| 22 |
+
numbers = [int(k.split("_")[1]) for k in data if k.startswith("student_")]
|
| 23 |
+
return f"student_{max(numbers) + 1}"
|
| 24 |
+
|
| 25 |
+
def add_student_data(new_data):
|
| 26 |
+
data = load_student_data()
|
| 27 |
+
new_id = generate_new_student_id()
|
| 28 |
+
data[new_id] = new_data
|
| 29 |
+
save_student_data(data)
|
| 30 |
+
return new_id, new_data
|
| 31 |
+
|
| 32 |
+
# ---------- Chat & Roadmap Simulation ----------
|
| 33 |
+
def get_gemini_response(prompt, student_data):
|
| 34 |
+
# Replace with real LLM call later
|
| 35 |
+
return f"(Simulated AI Response based on {student_data['personality']} profile: {prompt})"
|
| 36 |
+
|
| 37 |
+
def generate_ai_insights(student_data):
|
| 38 |
+
return f"(Insights: Your learning style is {student_data['learning_style']}, motivation is {student_data['motivation_level']})"
|
| 39 |
+
|
| 40 |
+
# ---------- Gradio Interface ----------
|
| 41 |
+
def create_profile(learning_style, academic_progress, personality, interests, goals,
|
| 42 |
+
level, preferred_methods, iq_level, eq_level, decision_style,
|
| 43 |
+
motivation_level, study_environment, community_groups):
|
| 44 |
+
new_data = {
|
| 45 |
+
"learning_style": learning_style,
|
| 46 |
+
"academic_progress": academic_progress,
|
| 47 |
+
"personality": personality,
|
| 48 |
+
"interests": interests,
|
| 49 |
+
"goals": goals,
|
| 50 |
+
"level": level,
|
| 51 |
+
"preferred_methods": [m.strip() for m in preferred_methods.split(",") if m.strip()],
|
| 52 |
+
"iq_level": iq_level,
|
| 53 |
+
"eq_level": eq_level,
|
| 54 |
+
"decision_making_style": decision_style,
|
| 55 |
+
"motivation_level": motivation_level,
|
| 56 |
+
"preferred_study_environment": study_environment,
|
| 57 |
+
"community_groups": [g.strip() for g in community_groups.split(",") if g.strip()]
|
| 58 |
+
}
|
| 59 |
+
new_id, saved_data = add_student_data(new_data)
|
| 60 |
+
return f"✅ New student created with ID: {new_id}", json.dumps(saved_data, indent=2, ensure_ascii=False), list(load_student_data().keys())
|
| 61 |
+
|
| 62 |
+
def chat(student_id, message):
|
| 63 |
+
data = load_student_data()
|
| 64 |
+
student_data = data.get(student_id)
|
| 65 |
+
if not student_data:
|
| 66 |
+
return "❌ Student not found."
|
| 67 |
+
|
| 68 |
+
if message.lower() == "roadmap":
|
| 69 |
+
return get_gemini_response("roadmap", student_data)
|
| 70 |
+
elif message.lower() == "insights":
|
| 71 |
+
return generate_ai_insights(student_data)
|
| 72 |
+
else:
|
| 73 |
+
return get_gemini_response(message, student_data)
|
| 74 |
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("# 🎓 ThinkPal – Personalized Learning Assistant")
|
| 77 |
+
|
| 78 |
+
with gr.Tab("➕ Add Student"):
|
| 79 |
+
with gr.Row():
|
| 80 |
+
learning_style = gr.Textbox(label="Learning Style")
|
| 81 |
+
academic_progress = gr.Textbox(label="Academic Progress")
|
| 82 |
+
personality = gr.Textbox(label="Personality")
|
| 83 |
+
interests = gr.Textbox(label="Interests")
|
| 84 |
+
goals = gr.Textbox(label="Goals")
|
| 85 |
+
level = gr.Textbox(label="Level")
|
| 86 |
+
preferred_methods = gr.Textbox(label="Preferred Methods (comma-separated)")
|
| 87 |
+
iq_level = gr.Textbox(label="IQ Level")
|
| 88 |
+
eq_level = gr.Textbox(label="EQ Level")
|
| 89 |
+
decision_style = gr.Textbox(label="Decision Style")
|
| 90 |
+
motivation_level = gr.Textbox(label="Motivation Level")
|
| 91 |
+
study_environment = gr.Textbox(label="Study Environment")
|
| 92 |
+
community_groups = gr.Textbox(label="Community Groups (comma-separated)")
|
| 93 |
+
|
| 94 |
+
create_btn = gr.Button("Save Profile")
|
| 95 |
+
status = gr.Textbox(label="Status")
|
| 96 |
+
preview = gr.Textbox(label="Profile Preview", lines=8)
|
| 97 |
+
student_list = gr.Dropdown(label="Student IDs", choices=list(load_student_data().keys()), value=None)
|
| 98 |
+
|
| 99 |
+
create_btn.click(
|
| 100 |
+
fn=create_profile,
|
| 101 |
+
inputs=[learning_style, academic_progress, personality, interests, goals,
|
| 102 |
+
level, preferred_methods, iq_level, eq_level, decision_style,
|
| 103 |
+
motivation_level, study_environment, community_groups],
|
| 104 |
+
outputs=[status, preview, student_list]
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
with gr.Tab("💬 Chat"):
|
| 108 |
+
student_select = gr.Dropdown(label="Choose Student", choices=list(load_student_data().keys()))
|
| 109 |
+
chat_in = gr.Textbox(label="Message (type 'roadmap' or 'insights')")
|
| 110 |
+
chat_out = gr.Textbox(label="Chatbot Response", lines=6)
|
| 111 |
+
chat_btn = gr.Button("Send")
|
| 112 |
|
| 113 |
+
chat_btn.click(fn=chat, inputs=[student_select, chat_in], outputs=chat_out)
|
| 114 |
|
| 115 |
+
demo.launch()
|
|
|