Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,126 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return {}
|
| 11 |
-
with open(
|
| 12 |
return json.load(f)
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return
|
| 31 |
-
|
| 32 |
-
# ----------
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def generate_ai_insights(student_data):
|
| 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 |
-
|
| 64 |
-
|
|
|
|
| 65 |
if not student_data:
|
| 66 |
-
return "❌ Student not found."
|
| 67 |
|
| 68 |
-
if
|
| 69 |
-
|
| 70 |
-
elif
|
| 71 |
-
|
| 72 |
else:
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
gr.Markdown("# 🎓 ThinkPal – Personalized Learning Assistant")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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 |
-
|
| 108 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
# -----------------------------
|
| 7 |
+
# File for student profiles
|
| 8 |
+
# -----------------------------
|
| 9 |
+
STUDENTS_FILE = "student_profiles.json"
|
| 10 |
+
|
| 11 |
+
# -----------------------------
|
| 12 |
+
# Student data handling
|
| 13 |
+
# -----------------------------
|
| 14 |
+
def load_students():
|
| 15 |
+
"""Load all student profiles from JSON file."""
|
| 16 |
+
if not os.path.exists(STUDENTS_FILE):
|
| 17 |
return {}
|
| 18 |
+
with open(STUDENTS_FILE, "r", encoding="utf-8") as f:
|
| 19 |
return json.load(f)
|
| 20 |
|
| 21 |
+
def save_students(data):
|
| 22 |
+
"""Save all student profiles to JSON file."""
|
| 23 |
+
with open(STUDENTS_FILE, "w", encoding="utf-8") as f:
|
| 24 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 25 |
+
|
| 26 |
+
def get_student_data(student_id):
|
| 27 |
+
"""Retrieve a single student profile by ID."""
|
| 28 |
+
students = load_students()
|
| 29 |
+
return students.get(student_id)
|
| 30 |
+
|
| 31 |
+
def add_student_data(data):
|
| 32 |
+
"""Add a new student with unique random ID."""
|
| 33 |
+
students = load_students()
|
| 34 |
+
student_id = "student_" + str(uuid.uuid4())[:8] # unique random ID
|
| 35 |
+
students[student_id] = data
|
| 36 |
+
save_students(students)
|
| 37 |
+
return student_id
|
| 38 |
+
|
| 39 |
+
# -----------------------------
|
| 40 |
+
# Simulated AI responses (replace with Gemini later)
|
| 41 |
+
# -----------------------------
|
| 42 |
+
def get_gemini_response(prompt, student_data=None):
|
| 43 |
+
"""Simulated LLM response."""
|
| 44 |
+
if not student_data:
|
| 45 |
+
return f"(Simulated Response: {prompt})"
|
| 46 |
+
return f"(Simulated AI Response for {student_data.get('personality','N/A')} personality: {prompt})"
|
| 47 |
|
| 48 |
def generate_ai_insights(student_data):
|
| 49 |
+
"""Simulated AI insights."""
|
| 50 |
+
return f"(Insights: Learning style = {student_data.get('learning_style')}, Motivation = {student_data.get('motivation_level')})"
|
| 51 |
+
|
| 52 |
+
# -----------------------------
|
| 53 |
+
# FAQ system (Arabic + English)
|
| 54 |
+
# -----------------------------
|
| 55 |
+
faqs = {
|
| 56 |
+
"إيه هو ThinkPal؟": "ThinkPal منصة بتساعدك تعرف نفسك أكتر وتتعلم بالطريقة اللي تناسبك...",
|
| 57 |
+
"What is ThinkPal?": "ThinkPal is a platform that helps you know yourself better...",
|
| 58 |
+
"لو وقفت في نص الطريق؟": "مفيش مشكلة، تقدر ترجع في أي وقت وتكمل من نفس المكان.",
|
| 59 |
+
"What if I stop halfway?": "No problem! You can always continue later from where you left off."
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
def find_faq_answer(user_input):
|
| 63 |
+
"""Check if user_input matches any FAQ question."""
|
| 64 |
+
for q, a in faqs.items():
|
| 65 |
+
if user_input.strip().lower() in q.lower():
|
| 66 |
+
return a
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
# -----------------------------
|
| 70 |
+
# Chat interface logic
|
| 71 |
+
# -----------------------------
|
| 72 |
+
def thinkpal_interface(student_id, user_input):
|
| 73 |
+
"""Main chat interface logic."""
|
| 74 |
+
student_data = get_student_data(student_id)
|
| 75 |
+
roadmap_text, insights_text, chatbot_response_text = "", "", ""
|
| 76 |
+
|
| 77 |
if not student_data:
|
| 78 |
+
return ("❌ Student not found. Please add a profile first.", "", "")
|
| 79 |
|
| 80 |
+
if user_input.lower() == "roadmap":
|
| 81 |
+
roadmap_text = get_gemini_response("roadmap", student_data) or "Could not generate roadmap."
|
| 82 |
+
elif user_input.lower() == "insights":
|
| 83 |
+
insights_text = generate_ai_insights(student_data) or "Could not generate insights."
|
| 84 |
else:
|
| 85 |
+
faq_answer = find_faq_answer(user_input)
|
| 86 |
+
chatbot_response_text = faq_answer if faq_answer else get_gemini_response(user_input, student_data) or "Sorry, could not process request."
|
| 87 |
|
| 88 |
+
return roadmap_text, insights_text, chatbot_response_text
|
|
|
|
| 89 |
|
| 90 |
+
def add_new_student(data_json):
|
| 91 |
+
"""Add student from JSON string."""
|
| 92 |
+
try:
|
| 93 |
+
data = json.loads(data_json)
|
| 94 |
+
except json.JSONDecodeError:
|
| 95 |
+
return "❌ Invalid JSON. Please check your input."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
student_id = add_student_data(data)
|
| 98 |
+
return f"✅ Student added with ID: {student_id}"
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# -----------------------------
|
| 101 |
+
# Gradio UI
|
| 102 |
+
# -----------------------------
|
| 103 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 104 |
+
gr.Markdown("## 🎓 ThinkPal - Personalized Learning Assistant")
|
| 105 |
|
| 106 |
+
with gr.Tab("💬 Chat"):
|
| 107 |
+
with gr.Row():
|
| 108 |
+
student_id_input = gr.Textbox(label="Enter Student ID", placeholder="student_1")
|
| 109 |
+
user_input = gr.Textbox(label="Your Question", placeholder="Type roadmap, insights, or FAQ...")
|
| 110 |
+
submit_btn = gr.Button("Ask")
|
| 111 |
+
roadmap_output = gr.Textbox(label="Roadmap", lines=8)
|
| 112 |
+
insights_output = gr.Textbox(label="Insights", lines=6)
|
| 113 |
+
chatbot_output = gr.Textbox(label="Chatbot Response", lines=4)
|
| 114 |
+
submit_btn.click(thinkpal_interface, [student_id_input, user_input], [roadmap_output, insights_output, chatbot_output])
|
| 115 |
+
|
| 116 |
+
with gr.Tab("➕ Add Student"):
|
| 117 |
+
gr.Markdown("Paste student profile data as JSON to add a new student.")
|
| 118 |
+
new_student_data = gr.Textbox(label="Student Data (JSON)", lines=10,
|
| 119 |
+
placeholder='{"learning_style": "visual", "goals": "improve math"}')
|
| 120 |
+
add_btn = gr.Button("Add Student")
|
| 121 |
+
add_output = gr.Textbox(label="Result")
|
| 122 |
+
add_btn.click(add_new_student, inputs=[new_student_data], outputs=[add_output])
|
| 123 |
+
|
| 124 |
+
# For Hugging Face Spaces
|
| 125 |
+
if __name__ == "__main__":
|
| 126 |
+
demo.launch()
|