Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import requests
|
|
| 7 |
import tempfile
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
-
import pandas as pd
|
| 11 |
from huggingface_hub import HfApi, hf_hub_download
|
| 12 |
|
| 13 |
# ---------- CONFIG ----------
|
|
@@ -43,7 +42,6 @@ def call_groq_api(prompt, temperature=0.7, max_tokens=256):
|
|
| 43 |
data = resp.json()
|
| 44 |
return data["choices"][0]["message"]["content"]
|
| 45 |
|
| 46 |
-
# Dataset helpers
|
| 47 |
def read_remote_homework():
|
| 48 |
try:
|
| 49 |
local_path = hf_hub_download(repo_id=REPO_ID, filename=FILE_NAME, repo_type=REPO_TYPE, token=HF_WRITE_TOKEN)
|
|
@@ -77,7 +75,6 @@ def append_and_push_record(record: dict):
|
|
| 77 |
except Exception as e:
|
| 78 |
return False, f"Failed to push to dataset repo: {e}"
|
| 79 |
|
| 80 |
-
# Teacher helpers
|
| 81 |
def generate_prompt(subject, brief, num_questions=5):
|
| 82 |
return (
|
| 83 |
f"You are an experienced K-12 teacher. Create {num_questions} diverse homework questions "
|
|
@@ -93,7 +90,6 @@ def generate_homework(subject, brief, num_questions=5):
|
|
| 93 |
generated = call_groq_api(prompt, temperature=0.7, max_tokens=350)
|
| 94 |
return generated.strip()
|
| 95 |
|
| 96 |
-
# Student helpers
|
| 97 |
def parse_questions(text):
|
| 98 |
lines = text.strip().splitlines()
|
| 99 |
questions = []
|
|
@@ -109,7 +105,8 @@ def parse_questions(text):
|
|
| 109 |
def filter_homework(subject, grade):
|
| 110 |
records = read_remote_homework()
|
| 111 |
filtered = [r for r in records if r.get("subject") == subject and r.get("grade") == grade]
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
def get_homework_by_id(hw_id):
|
| 115 |
records = read_remote_homework()
|
|
@@ -128,12 +125,10 @@ def analyze_answers(questions, answers, time_taken_seconds):
|
|
| 128 |
result = f"Score: {score}/{len(questions)}."
|
| 129 |
return result, needs_attention
|
| 130 |
|
| 131 |
-
# UI Setup
|
| 132 |
with gr.Blocks() as demo:
|
| 133 |
gr.Markdown("# TeachMate AI")
|
| 134 |
|
| 135 |
with gr.Tabs():
|
| 136 |
-
# Teacher tab
|
| 137 |
with gr.TabItem("Teacher"):
|
| 138 |
with gr.Row():
|
| 139 |
with gr.Column(scale=1):
|
|
@@ -141,6 +136,7 @@ with gr.Blocks() as demo:
|
|
| 141 |
email_in = gr.Textbox(label="Teacher Email")
|
| 142 |
subject_in = gr.Dropdown(label="Subject", choices=["Mathematics","Science","English","History","Geography","Other"], value="Mathematics")
|
| 143 |
grade_in = gr.Dropdown(label="Grade", choices=[f"Grade {i}" for i in range(1,13)], value="Grade 5")
|
|
|
|
| 144 |
brief_in = gr.Textbox(label="Homework Brief (describe topic & grade)", lines=4)
|
| 145 |
gen_btn = gr.Button("Generate Homework", variant="primary")
|
| 146 |
|
|
@@ -150,21 +146,20 @@ with gr.Blocks() as demo:
|
|
| 150 |
status = gr.Textbox(label="Status / Logs", interactive=False)
|
| 151 |
results_out = gr.Textbox(label="Student Results & Attention Warnings", lines=10, interactive=False)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
def on_generate(name, email, subject, grade, brief):
|
| 156 |
if not brief.strip():
|
| 157 |
return "Please enter a brief"
|
| 158 |
questions = generate_homework(subject, brief)
|
| 159 |
return questions
|
| 160 |
|
| 161 |
-
def on_save(name, email, subject, grade, brief, questions):
|
| 162 |
rec = {
|
| 163 |
"id": str(uuid.uuid4()),
|
| 164 |
"teacher_name": name,
|
| 165 |
"teacher_email": email,
|
| 166 |
"subject": subject,
|
| 167 |
"grade": grade,
|
|
|
|
| 168 |
"brief": brief,
|
| 169 |
"final_questions": questions,
|
| 170 |
"created_at": datetime.now().isoformat(),
|
|
@@ -183,10 +178,9 @@ with gr.Blocks() as demo:
|
|
| 183 |
lines.append(line)
|
| 184 |
return "\n".join(lines)
|
| 185 |
|
| 186 |
-
gen_btn.click(on_generate, inputs=[name_in, email_in, subject_in, grade_in, brief_in], outputs=generated_out)
|
| 187 |
-
save_btn.click(on_save, inputs=[name_in, email_in, subject_in, grade_in, brief_in, generated_out], outputs=status)
|
| 188 |
|
| 189 |
-
# Student tab
|
| 190 |
with gr.TabItem("Student"):
|
| 191 |
with gr.Row():
|
| 192 |
with gr.Column(scale=1):
|
|
@@ -197,7 +191,8 @@ with gr.Blocks() as demo:
|
|
| 197 |
|
| 198 |
load_hw_btn = gr.Button("Load Homework")
|
| 199 |
homework_dd = gr.Dropdown(label="Select Homework", choices=[], interactive=True)
|
| 200 |
-
|
|
|
|
| 201 |
|
| 202 |
with gr.Column(scale=1):
|
| 203 |
questions_box = gr.Textbox(label="Questions", lines=15, interactive=False)
|
|
@@ -230,7 +225,6 @@ with gr.Blocks() as demo:
|
|
| 230 |
time_taken = time.time() - start
|
| 231 |
ans_list = answers.splitlines()
|
| 232 |
res, needs_attention = analyze_answers(questions, ans_list, time_taken)
|
| 233 |
-
# Append to global student_results on teacher tab
|
| 234 |
if not hasattr(demo, "student_results"):
|
| 235 |
demo.student_results = []
|
| 236 |
demo.student_results.append({
|
|
@@ -242,12 +236,16 @@ with gr.Blocks() as demo:
|
|
| 242 |
})
|
| 243 |
return res
|
| 244 |
|
|
|
|
| 245 |
student_subject.change(load_homework_list, inputs=[student_subject, student_grade], outputs=[homework_dd, homework_dd])
|
|
|
|
| 246 |
load_hw_btn.click(load_homework_list, inputs=[student_subject, student_grade], outputs=[homework_dd, homework_dd])
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
| 248 |
submit_btn.click(submit_answers, inputs=[current_questions, answers_box, start_time, student_name, student_grade], outputs=result_out)
|
| 249 |
|
| 250 |
-
# Rough Work tab
|
| 251 |
with gr.TabItem("Rough Work"):
|
| 252 |
rough_input = gr.Textbox(label="Enter your questions or doubts here", lines=10, interactive=True)
|
| 253 |
get_hint_btn = gr.Button("Get Hint")
|
|
@@ -260,8 +258,7 @@ with gr.Blocks() as demo:
|
|
| 260 |
|
| 261 |
get_hint_btn.click(generate_hint, inputs=rough_input, outputs=hint_output)
|
| 262 |
|
| 263 |
-
|
| 264 |
-
timer = gr.Timer(10) # Every 10 seconds
|
| 265 |
timer.tick(update_teacher_results, outputs=results_out)
|
| 266 |
|
| 267 |
if __name__ == "__main__":
|
|
|
|
| 7 |
import tempfile
|
| 8 |
|
| 9 |
import gradio as gr
|
|
|
|
| 10 |
from huggingface_hub import HfApi, hf_hub_download
|
| 11 |
|
| 12 |
# ---------- CONFIG ----------
|
|
|
|
| 42 |
data = resp.json()
|
| 43 |
return data["choices"][0]["message"]["content"]
|
| 44 |
|
|
|
|
| 45 |
def read_remote_homework():
|
| 46 |
try:
|
| 47 |
local_path = hf_hub_download(repo_id=REPO_ID, filename=FILE_NAME, repo_type=REPO_TYPE, token=HF_WRITE_TOKEN)
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
return False, f"Failed to push to dataset repo: {e}"
|
| 77 |
|
|
|
|
| 78 |
def generate_prompt(subject, brief, num_questions=5):
|
| 79 |
return (
|
| 80 |
f"You are an experienced K-12 teacher. Create {num_questions} diverse homework questions "
|
|
|
|
| 90 |
generated = call_groq_api(prompt, temperature=0.7, max_tokens=350)
|
| 91 |
return generated.strip()
|
| 92 |
|
|
|
|
| 93 |
def parse_questions(text):
|
| 94 |
lines = text.strip().splitlines()
|
| 95 |
questions = []
|
|
|
|
| 105 |
def filter_homework(subject, grade):
|
| 106 |
records = read_remote_homework()
|
| 107 |
filtered = [r for r in records if r.get("subject") == subject and r.get("grade") == grade]
|
| 108 |
+
# Return list of tuples (id, homework_name)
|
| 109 |
+
return [(r.get("id"), r.get("homework_name", "Unnamed Homework")) for r in filtered]
|
| 110 |
|
| 111 |
def get_homework_by_id(hw_id):
|
| 112 |
records = read_remote_homework()
|
|
|
|
| 125 |
result = f"Score: {score}/{len(questions)}."
|
| 126 |
return result, needs_attention
|
| 127 |
|
|
|
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
gr.Markdown("# TeachMate AI")
|
| 130 |
|
| 131 |
with gr.Tabs():
|
|
|
|
| 132 |
with gr.TabItem("Teacher"):
|
| 133 |
with gr.Row():
|
| 134 |
with gr.Column(scale=1):
|
|
|
|
| 136 |
email_in = gr.Textbox(label="Teacher Email")
|
| 137 |
subject_in = gr.Dropdown(label="Subject", choices=["Mathematics","Science","English","History","Geography","Other"], value="Mathematics")
|
| 138 |
grade_in = gr.Dropdown(label="Grade", choices=[f"Grade {i}" for i in range(1,13)], value="Grade 5")
|
| 139 |
+
homework_name_in = gr.Textbox(label="Homework Name (e.g. Fractions Practice)")
|
| 140 |
brief_in = gr.Textbox(label="Homework Brief (describe topic & grade)", lines=4)
|
| 141 |
gen_btn = gr.Button("Generate Homework", variant="primary")
|
| 142 |
|
|
|
|
| 146 |
status = gr.Textbox(label="Status / Logs", interactive=False)
|
| 147 |
results_out = gr.Textbox(label="Student Results & Attention Warnings", lines=10, interactive=False)
|
| 148 |
|
| 149 |
+
def on_generate(name, email, subject, grade, homework_name, brief):
|
|
|
|
|
|
|
| 150 |
if not brief.strip():
|
| 151 |
return "Please enter a brief"
|
| 152 |
questions = generate_homework(subject, brief)
|
| 153 |
return questions
|
| 154 |
|
| 155 |
+
def on_save(name, email, subject, grade, homework_name, brief, questions):
|
| 156 |
rec = {
|
| 157 |
"id": str(uuid.uuid4()),
|
| 158 |
"teacher_name": name,
|
| 159 |
"teacher_email": email,
|
| 160 |
"subject": subject,
|
| 161 |
"grade": grade,
|
| 162 |
+
"homework_name": homework_name,
|
| 163 |
"brief": brief,
|
| 164 |
"final_questions": questions,
|
| 165 |
"created_at": datetime.now().isoformat(),
|
|
|
|
| 178 |
lines.append(line)
|
| 179 |
return "\n".join(lines)
|
| 180 |
|
| 181 |
+
gen_btn.click(on_generate, inputs=[name_in, email_in, subject_in, grade_in, homework_name_in, brief_in], outputs=generated_out)
|
| 182 |
+
save_btn.click(on_save, inputs=[name_in, email_in, subject_in, grade_in, homework_name_in, brief_in, generated_out], outputs=status)
|
| 183 |
|
|
|
|
| 184 |
with gr.TabItem("Student"):
|
| 185 |
with gr.Row():
|
| 186 |
with gr.Column(scale=1):
|
|
|
|
| 191 |
|
| 192 |
load_hw_btn = gr.Button("Load Homework")
|
| 193 |
homework_dd = gr.Dropdown(label="Select Homework", choices=[], interactive=True)
|
| 194 |
+
# Remove manual Load Questions button since loading auto on selection
|
| 195 |
+
# load_questions_btn = gr.Button("Load Questions")
|
| 196 |
|
| 197 |
with gr.Column(scale=1):
|
| 198 |
questions_box = gr.Textbox(label="Questions", lines=15, interactive=False)
|
|
|
|
| 225 |
time_taken = time.time() - start
|
| 226 |
ans_list = answers.splitlines()
|
| 227 |
res, needs_attention = analyze_answers(questions, ans_list, time_taken)
|
|
|
|
| 228 |
if not hasattr(demo, "student_results"):
|
| 229 |
demo.student_results = []
|
| 230 |
demo.student_results.append({
|
|
|
|
| 236 |
})
|
| 237 |
return res
|
| 238 |
|
| 239 |
+
# Load homework list on subject or grade change
|
| 240 |
student_subject.change(load_homework_list, inputs=[student_subject, student_grade], outputs=[homework_dd, homework_dd])
|
| 241 |
+
student_grade.change(load_homework_list, inputs=[student_subject, student_grade], outputs=[homework_dd, homework_dd])
|
| 242 |
load_hw_btn.click(load_homework_list, inputs=[student_subject, student_grade], outputs=[homework_dd, homework_dd])
|
| 243 |
+
|
| 244 |
+
# Load questions automatically on homework selection
|
| 245 |
+
homework_dd.change(load_questions, inputs=homework_dd, outputs=[questions_box, result_out])
|
| 246 |
+
|
| 247 |
submit_btn.click(submit_answers, inputs=[current_questions, answers_box, start_time, student_name, student_grade], outputs=result_out)
|
| 248 |
|
|
|
|
| 249 |
with gr.TabItem("Rough Work"):
|
| 250 |
rough_input = gr.Textbox(label="Enter your questions or doubts here", lines=10, interactive=True)
|
| 251 |
get_hint_btn = gr.Button("Get Hint")
|
|
|
|
| 258 |
|
| 259 |
get_hint_btn.click(generate_hint, inputs=rough_input, outputs=hint_output)
|
| 260 |
|
| 261 |
+
timer = gr.Timer(10)
|
|
|
|
| 262 |
timer.tick(update_teacher_results, outputs=results_out)
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|