Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -77,18 +77,18 @@ def append_and_push_record(record: dict):
|
|
| 77 |
return False, f"Failed to push to dataset repo: {e}"
|
| 78 |
|
| 79 |
# Teacher helpers
|
| 80 |
-
def generate_prompt(subject, brief, num_questions=5
|
| 81 |
return (
|
| 82 |
f"You are an experienced K-12 teacher. Create {num_questions} diverse homework questions "
|
| 83 |
-
f"for a {subject} class
|
| 84 |
f"Mix question types (short answer, problem solving, multiple choice). "
|
| 85 |
f"Be concise and teacher-friendly.\n\n"
|
| 86 |
f"Brief: {brief}\n\n"
|
| 87 |
f"Output format:\n1) Question text\n2) Question text\n..."
|
| 88 |
)
|
| 89 |
|
| 90 |
-
def generate_homework(subject, brief, num_questions=5
|
| 91 |
-
prompt = generate_prompt(subject, brief, num_questions
|
| 92 |
generated = call_groq_api(prompt, temperature=0.7, max_tokens=350)
|
| 93 |
return generated.strip()
|
| 94 |
|
|
@@ -99,8 +99,7 @@ def parse_questions(text):
|
|
| 99 |
for line in lines:
|
| 100 |
if line.strip() == "":
|
| 101 |
continue
|
| 102 |
-
|
| 103 |
-
if len(line) > 1 and line[0].isdigit() and (line[1] == ')' or (len(line) > 2 and line[2] == ')')):
|
| 104 |
parts = line.split(")", 1)
|
| 105 |
if len(parts) > 1:
|
| 106 |
questions.append(parts[1].strip())
|
|
@@ -109,8 +108,7 @@ 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 |
-
return [{"label": r.get("homework_name", "Unnamed Homework"), "value": r.get("id")} for r in filtered]
|
| 114 |
|
| 115 |
def get_homework_by_id(hw_id):
|
| 116 |
records = read_remote_homework()
|
|
@@ -129,12 +127,10 @@ def analyze_answers(questions, answers, time_taken_seconds):
|
|
| 129 |
result = f"Score: {score}/{len(questions)}."
|
| 130 |
return result, needs_attention
|
| 131 |
|
| 132 |
-
# UI Setup
|
| 133 |
with gr.Blocks() as demo:
|
| 134 |
gr.Markdown("# TeachMate AI")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
demo.student_results = []
|
| 138 |
|
| 139 |
with gr.Tabs():
|
| 140 |
# Teacher tab
|
|
@@ -145,7 +141,7 @@ with gr.Blocks() as demo:
|
|
| 145 |
email_in = gr.Textbox(label="Teacher Email")
|
| 146 |
subject_in = gr.Dropdown(label="Subject", choices=["Mathematics","Science","English","History","Geography","Other"], value="Mathematics")
|
| 147 |
grade_in = gr.Dropdown(label="Grade", choices=[f"Grade {i}" for i in range(1,13)], value="Grade 5")
|
| 148 |
-
complexity_in = gr.Dropdown(label="Complexity
|
| 149 |
homework_name_in = gr.Textbox(label="Homework Name (e.g. Fractions Practice)")
|
| 150 |
|
| 151 |
brief_in = gr.Textbox(label="Homework Brief (describe topic & grade)", lines=4)
|
|
@@ -156,15 +152,19 @@ with gr.Blocks() as demo:
|
|
| 156 |
save_btn = gr.Button("Save to Dataset", variant="secondary")
|
| 157 |
status = gr.Textbox(label="Status / Logs", interactive=False)
|
| 158 |
results_out = gr.Textbox(label="Student Results & Attention Warnings", lines=10, interactive=False)
|
|
|
|
| 159 |
|
| 160 |
def on_generate(name, email, subject, grade, complexity, homework_name, brief):
|
| 161 |
if not brief.strip():
|
| 162 |
return "Please enter a brief"
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
def on_save(name, email, subject, grade, complexity, homework_name, questions):
|
| 166 |
if not homework_name.strip():
|
| 167 |
-
return "Please
|
| 168 |
rec = {
|
| 169 |
"id": str(uuid.uuid4()),
|
| 170 |
"teacher_name": name,
|
|
@@ -181,10 +181,10 @@ with gr.Blocks() as demo:
|
|
| 181 |
return msg
|
| 182 |
|
| 183 |
def update_teacher_results():
|
| 184 |
-
if not
|
| 185 |
return "No student submissions yet."
|
| 186 |
lines = []
|
| 187 |
-
for r in
|
| 188 |
line = (f"Student: {r['student_name']} | Grade: {r['student_grade']} | "
|
| 189 |
f"Score: {r['score']}/{r['total_questions']} | "
|
| 190 |
f"Attention: {'YES' if r['needs_attention'] else 'NO'}")
|
|
@@ -193,15 +193,7 @@ with gr.Blocks() as demo:
|
|
| 193 |
|
| 194 |
gen_btn.click(on_generate, inputs=[name_in, email_in, subject_in, grade_in, complexity_in, homework_name_in, brief_in], outputs=generated_out)
|
| 195 |
save_btn.click(on_save, inputs=[name_in, email_in, subject_in, grade_in, complexity_in, homework_name_in, generated_out], outputs=status)
|
| 196 |
-
|
| 197 |
-
# Timer to update student results every 10 seconds
|
| 198 |
-
def periodic_update():
|
| 199 |
-
return update_teacher_results()
|
| 200 |
-
# Instead of gr.Timer (not supported), we use an invisible button that triggers update every 10 seconds
|
| 201 |
-
timer_btn = gr.Button(visible=False)
|
| 202 |
-
timer_btn.click(periodic_update, outputs=results_out)
|
| 203 |
-
# Auto-click timer button every 10 seconds with JS
|
| 204 |
-
demo.load(lambda: timer_btn.click(periodic_update, outputs=results_out), outputs=results_out)
|
| 205 |
|
| 206 |
# Student tab
|
| 207 |
with gr.TabItem("Student"):
|
|
@@ -212,7 +204,7 @@ with gr.Blocks() as demo:
|
|
| 212 |
student_email = gr.Textbox(label="Student Email")
|
| 213 |
student_subject = gr.Dropdown(label="Subject", choices=["Mathematics","Science","English","History","Geography","Other"], value="Mathematics")
|
| 214 |
|
| 215 |
-
homework_dd = gr.Dropdown(label="Select Homework", choices=[], interactive=True
|
| 216 |
|
| 217 |
with gr.Column(scale=1):
|
| 218 |
questions_box = gr.Textbox(label="Questions", lines=15, interactive=False)
|
|
@@ -226,18 +218,18 @@ with gr.Blocks() as demo:
|
|
| 226 |
def load_homework_list(subject, grade):
|
| 227 |
options = filter_homework(subject, grade)
|
| 228 |
if not options:
|
| 229 |
-
return
|
| 230 |
-
return gr.update(
|
| 231 |
|
| 232 |
def load_questions(hw_id):
|
| 233 |
hw = get_homework_by_id(hw_id)
|
| 234 |
if not hw:
|
| 235 |
-
return "", ""
|
| 236 |
questions_text = hw.get("final_questions", "")
|
| 237 |
questions = parse_questions(questions_text)
|
| 238 |
current_questions.set(questions)
|
| 239 |
start_time.set(time.time())
|
| 240 |
-
return questions_text, ""
|
| 241 |
|
| 242 |
def submit_answers(questions, answers, start, student_name_val, student_grade_val):
|
| 243 |
if start is None:
|
|
@@ -245,8 +237,8 @@ with gr.Blocks() as demo:
|
|
| 245 |
time_taken = time.time() - start
|
| 246 |
ans_list = answers.splitlines()
|
| 247 |
res, needs_attention = analyze_answers(questions, ans_list, time_taken)
|
| 248 |
-
#
|
| 249 |
-
|
| 250 |
"student_name": student_name_val,
|
| 251 |
"student_grade": student_grade_val,
|
| 252 |
"score": sum(1 for a in ans_list if a.strip()),
|
|
@@ -255,9 +247,12 @@ with gr.Blocks() as demo:
|
|
| 255 |
})
|
| 256 |
return res
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
| 261 |
submit_btn.click(submit_answers, inputs=[current_questions, answers_box, start_time, student_name, student_grade], outputs=result_out)
|
| 262 |
|
| 263 |
# Rough Work tab
|
|
|
|
| 77 |
return False, f"Failed to push to dataset repo: {e}"
|
| 78 |
|
| 79 |
# Teacher helpers
|
| 80 |
+
def generate_prompt(subject, brief, num_questions=5):
|
| 81 |
return (
|
| 82 |
f"You are an experienced K-12 teacher. Create {num_questions} diverse homework questions "
|
| 83 |
+
f"for a {subject} class based on the brief below. Include a short instruction line at the top. "
|
| 84 |
f"Mix question types (short answer, problem solving, multiple choice). "
|
| 85 |
f"Be concise and teacher-friendly.\n\n"
|
| 86 |
f"Brief: {brief}\n\n"
|
| 87 |
f"Output format:\n1) Question text\n2) Question text\n..."
|
| 88 |
)
|
| 89 |
|
| 90 |
+
def generate_homework(subject, brief, num_questions=5):
|
| 91 |
+
prompt = generate_prompt(subject, brief, num_questions)
|
| 92 |
generated = call_groq_api(prompt, temperature=0.7, max_tokens=350)
|
| 93 |
return generated.strip()
|
| 94 |
|
|
|
|
| 99 |
for line in lines:
|
| 100 |
if line.strip() == "":
|
| 101 |
continue
|
| 102 |
+
if line[0].isdigit() and (line[1] == ')' or line[2] == ')'):
|
|
|
|
| 103 |
parts = line.split(")", 1)
|
| 104 |
if len(parts) > 1:
|
| 105 |
questions.append(parts[1].strip())
|
|
|
|
| 108 |
def filter_homework(subject, grade):
|
| 109 |
records = read_remote_homework()
|
| 110 |
filtered = [r for r in records if r.get("subject") == subject and r.get("grade") == grade]
|
| 111 |
+
return [(r.get("id"), r.get("homework_name") or r.get("brief")[:60] + "...") for r in filtered]
|
|
|
|
| 112 |
|
| 113 |
def get_homework_by_id(hw_id):
|
| 114 |
records = read_remote_homework()
|
|
|
|
| 127 |
result = f"Score: {score}/{len(questions)}."
|
| 128 |
return result, needs_attention
|
| 129 |
|
|
|
|
| 130 |
with gr.Blocks() as demo:
|
| 131 |
gr.Markdown("# TeachMate AI")
|
| 132 |
|
| 133 |
+
student_results = []
|
|
|
|
| 134 |
|
| 135 |
with gr.Tabs():
|
| 136 |
# Teacher tab
|
|
|
|
| 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 |
+
complexity_in = gr.Dropdown(label="Complexity", choices=["Easy", "Medium", "Complex"], value="Medium")
|
| 145 |
homework_name_in = gr.Textbox(label="Homework Name (e.g. Fractions Practice)")
|
| 146 |
|
| 147 |
brief_in = gr.Textbox(label="Homework Brief (describe topic & grade)", lines=4)
|
|
|
|
| 152 |
save_btn = gr.Button("Save to Dataset", variant="secondary")
|
| 153 |
status = gr.Textbox(label="Status / Logs", interactive=False)
|
| 154 |
results_out = gr.Textbox(label="Student Results & Attention Warnings", lines=10, interactive=False)
|
| 155 |
+
refresh_btn = gr.Button("Refresh Student Results")
|
| 156 |
|
| 157 |
def on_generate(name, email, subject, grade, complexity, homework_name, brief):
|
| 158 |
if not brief.strip():
|
| 159 |
return "Please enter a brief"
|
| 160 |
+
# We can optionally incorporate complexity into prompt
|
| 161 |
+
prompt = f"{brief}\nDifficulty level: {complexity}"
|
| 162 |
+
questions = generate_homework(subject, prompt)
|
| 163 |
+
return questions
|
| 164 |
|
| 165 |
def on_save(name, email, subject, grade, complexity, homework_name, questions):
|
| 166 |
if not homework_name.strip():
|
| 167 |
+
return "Please provide a homework name before saving."
|
| 168 |
rec = {
|
| 169 |
"id": str(uuid.uuid4()),
|
| 170 |
"teacher_name": name,
|
|
|
|
| 181 |
return msg
|
| 182 |
|
| 183 |
def update_teacher_results():
|
| 184 |
+
if not student_results:
|
| 185 |
return "No student submissions yet."
|
| 186 |
lines = []
|
| 187 |
+
for r in student_results:
|
| 188 |
line = (f"Student: {r['student_name']} | Grade: {r['student_grade']} | "
|
| 189 |
f"Score: {r['score']}/{r['total_questions']} | "
|
| 190 |
f"Attention: {'YES' if r['needs_attention'] else 'NO'}")
|
|
|
|
| 193 |
|
| 194 |
gen_btn.click(on_generate, inputs=[name_in, email_in, subject_in, grade_in, complexity_in, homework_name_in, brief_in], outputs=generated_out)
|
| 195 |
save_btn.click(on_save, inputs=[name_in, email_in, subject_in, grade_in, complexity_in, homework_name_in, generated_out], outputs=status)
|
| 196 |
+
refresh_btn.click(update_teacher_results, outputs=results_out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
# Student tab
|
| 199 |
with gr.TabItem("Student"):
|
|
|
|
| 204 |
student_email = gr.Textbox(label="Student Email")
|
| 205 |
student_subject = gr.Dropdown(label="Subject", choices=["Mathematics","Science","English","History","Geography","Other"], value="Mathematics")
|
| 206 |
|
| 207 |
+
homework_dd = gr.Dropdown(label="Select Homework", choices=[], interactive=True)
|
| 208 |
|
| 209 |
with gr.Column(scale=1):
|
| 210 |
questions_box = gr.Textbox(label="Questions", lines=15, interactive=False)
|
|
|
|
| 218 |
def load_homework_list(subject, grade):
|
| 219 |
options = filter_homework(subject, grade)
|
| 220 |
if not options:
|
| 221 |
+
return [], gr.update(interactive=False)
|
| 222 |
+
return options, gr.update(interactive=True)
|
| 223 |
|
| 224 |
def load_questions(hw_id):
|
| 225 |
hw = get_homework_by_id(hw_id)
|
| 226 |
if not hw:
|
| 227 |
+
return "", ""
|
| 228 |
questions_text = hw.get("final_questions", "")
|
| 229 |
questions = parse_questions(questions_text)
|
| 230 |
current_questions.set(questions)
|
| 231 |
start_time.set(time.time())
|
| 232 |
+
return questions_text, ""
|
| 233 |
|
| 234 |
def submit_answers(questions, answers, start, student_name_val, student_grade_val):
|
| 235 |
if start is None:
|
|
|
|
| 237 |
time_taken = time.time() - start
|
| 238 |
ans_list = answers.splitlines()
|
| 239 |
res, needs_attention = analyze_answers(questions, ans_list, time_taken)
|
| 240 |
+
# Update global student_results
|
| 241 |
+
student_results.append({
|
| 242 |
"student_name": student_name_val,
|
| 243 |
"student_grade": student_grade_val,
|
| 244 |
"score": sum(1 for a in ans_list if a.strip()),
|
|
|
|
| 247 |
})
|
| 248 |
return res
|
| 249 |
|
| 250 |
+
# Auto load homework dropdown on subject or grade change
|
| 251 |
+
student_subject.change(load_homework_list, inputs=[student_subject, student_grade], outputs=[homework_dd, homework_dd])
|
| 252 |
+
student_grade.change(load_homework_list, inputs=[student_subject, student_grade], outputs=[homework_dd, homework_dd])
|
| 253 |
+
|
| 254 |
+
homework_dd.change(load_questions, inputs=homework_dd, outputs=[questions_box, result_out])
|
| 255 |
+
|
| 256 |
submit_btn.click(submit_answers, inputs=[current_questions, answers_box, start_time, student_name, student_grade], outputs=result_out)
|
| 257 |
|
| 258 |
# Rough Work tab
|