Upload app.py
Browse files
app.py
CHANGED
@@ -357,34 +357,28 @@ def analyze_solution(question: str, solution: str):
|
|
357 |
|
358 |
return final_verdict
|
359 |
|
360 |
-
|
361 |
-
|
362 |
-
def classify_solution_stream(question: str, solution: str, progress=gr.Progress()):
|
363 |
-
# always yield EXACTLY THREE outputs (classification, explanation, status)
|
364 |
if not question.strip() or not solution.strip():
|
365 |
yield "Please fill in both fields", "", "⚠️ Provide a question and a solution."
|
366 |
return
|
367 |
|
368 |
-
# lazy init if needed
|
369 |
if not models_ready():
|
370 |
yield "⏳ Loading models…", "", "Booting models…"
|
371 |
msg = load_model()
|
372 |
-
progress(0.1, desc="Loading models")
|
373 |
if not models_ready():
|
374 |
yield "Models not loaded", "", f"❌ {msg}"
|
375 |
return
|
376 |
|
377 |
try:
|
378 |
# Stage 1 — conceptual
|
379 |
-
progress(0.2, desc="Stage 1: Conceptual check")
|
380 |
yield "⏳ Working…", "Starting conceptual check…", "🔎 **Stage 1:** running classifier…"
|
381 |
conceptual = run_conceptual_check(question, solution, classifier_model, classifier_tokenizer)
|
382 |
conf = conceptual['probabilities'][conceptual['prediction']]
|
383 |
-
yield "⏳ Working…", f"Stage 1:
|
384 |
f"✅ **Stage 1 done** — prediction: **{conceptual['prediction']}** (p={conf:.2%})."
|
385 |
|
386 |
# Stage 2 — computational
|
387 |
-
progress(0.6, desc="Stage 2: Computational check")
|
388 |
yield "⏳ Working…", "Running computational check…", "🧮 **Stage 2:** extracting & evaluating equations…"
|
389 |
computational = run_computational_check(solution, gemma_model, gemma_tokenizer)
|
390 |
|
@@ -407,24 +401,22 @@ def classify_solution_stream(question: str, solution: str, progress=gr.Progress(
|
|
407 |
explanation = "All calculations are correct, but there appears to be a conceptual error in the logic or setup of the solution."
|
408 |
status = "🟨 **Final:** conceptual issue."
|
409 |
|
410 |
-
progress(1.0, desc="Done")
|
411 |
yield classification, explanation, status
|
412 |
|
413 |
except Exception as e:
|
414 |
logger.exception("inference failed")
|
415 |
yield "Runtime error", f"{type(e).__name__}: {e}", "❌ Exception during inference."
|
416 |
|
417 |
-
|
418 |
-
# Create Gradio interface (streaming UI)
|
419 |
with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
420 |
gr.Markdown("# 🧮 Math Solution Classifier")
|
421 |
gr.Markdown(
|
422 |
"Classify math solutions as **correct**, **conceptually flawed**, or **computationally flawed**. "
|
423 |
-
"
|
424 |
)
|
425 |
|
426 |
with gr.Row():
|
427 |
-
#
|
428 |
with gr.Column(scale=1):
|
429 |
question_input = gr.Textbox(
|
430 |
label="Math Question",
|
@@ -440,13 +432,13 @@ with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
|
440 |
classify_btn = gr.Button("Classify Solution", variant="primary")
|
441 |
clear_btn = gr.Button("Clear")
|
442 |
|
443 |
-
#
|
444 |
with gr.Column(scale=1):
|
445 |
classification_output = gr.Textbox(label="Classification", interactive=False)
|
446 |
explanation_output = gr.Textbox(label="Explanation", interactive=False, lines=6)
|
447 |
status_output = gr.Markdown(value="*(idle)*") # live stage updates
|
448 |
|
449 |
-
#
|
450 |
gr.Examples(
|
451 |
examples=[
|
452 |
["Solve for x: 2x + 5 = 13",
|
@@ -459,13 +451,13 @@ with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
|
459 |
inputs=[question_input, solution_input],
|
460 |
)
|
461 |
|
462 |
-
#
|
463 |
classify_btn.click(
|
464 |
-
fn=classify_solution_stream, #
|
465 |
inputs=[question_input, solution_input],
|
466 |
outputs=[classification_output, explanation_output, status_output],
|
467 |
-
show_progress=
|
468 |
-
concurrency_limit=1, # <-
|
469 |
)
|
470 |
|
471 |
clear_btn.click(
|
@@ -481,10 +473,11 @@ with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
|
481 |
queue=False,
|
482 |
)
|
483 |
|
484 |
-
#
|
485 |
app.queue()
|
486 |
|
487 |
if __name__ == "__main__":
|
488 |
app.launch()
|
489 |
|
490 |
|
|
|
|
357 |
|
358 |
return final_verdict
|
359 |
|
360 |
+
def classify_solution_stream(question: str, solution: str):
|
361 |
+
# always yield exactly 3 outputs: (classification, explanation, status)
|
|
|
|
|
362 |
if not question.strip() or not solution.strip():
|
363 |
yield "Please fill in both fields", "", "⚠️ Provide a question and a solution."
|
364 |
return
|
365 |
|
|
|
366 |
if not models_ready():
|
367 |
yield "⏳ Loading models…", "", "Booting models…"
|
368 |
msg = load_model()
|
|
|
369 |
if not models_ready():
|
370 |
yield "Models not loaded", "", f"❌ {msg}"
|
371 |
return
|
372 |
|
373 |
try:
|
374 |
# Stage 1 — conceptual
|
|
|
375 |
yield "⏳ Working…", "Starting conceptual check…", "🔎 **Stage 1:** running classifier…"
|
376 |
conceptual = run_conceptual_check(question, solution, classifier_model, classifier_tokenizer)
|
377 |
conf = conceptual['probabilities'][conceptual['prediction']]
|
378 |
+
yield "⏳ Working…", f"Stage 1: **{conceptual['prediction']}** (p={conf:.2%}). Now checking calculations…", \
|
379 |
f"✅ **Stage 1 done** — prediction: **{conceptual['prediction']}** (p={conf:.2%})."
|
380 |
|
381 |
# Stage 2 — computational
|
|
|
382 |
yield "⏳ Working…", "Running computational check…", "🧮 **Stage 2:** extracting & evaluating equations…"
|
383 |
computational = run_computational_check(solution, gemma_model, gemma_tokenizer)
|
384 |
|
|
|
401 |
explanation = "All calculations are correct, but there appears to be a conceptual error in the logic or setup of the solution."
|
402 |
status = "🟨 **Final:** conceptual issue."
|
403 |
|
|
|
404 |
yield classification, explanation, status
|
405 |
|
406 |
except Exception as e:
|
407 |
logger.exception("inference failed")
|
408 |
yield "Runtime error", f"{type(e).__name__}: {e}", "❌ Exception during inference."
|
409 |
|
410 |
+
# ---------------- UI: streaming, no progress bars ----------------
|
|
|
411 |
with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
412 |
gr.Markdown("# 🧮 Math Solution Classifier")
|
413 |
gr.Markdown(
|
414 |
"Classify math solutions as **correct**, **conceptually flawed**, or **computationally flawed**. "
|
415 |
+
"Live status updates appear below as the two-stage pipeline runs."
|
416 |
)
|
417 |
|
418 |
with gr.Row():
|
419 |
+
# -------- Left: inputs --------
|
420 |
with gr.Column(scale=1):
|
421 |
question_input = gr.Textbox(
|
422 |
label="Math Question",
|
|
|
432 |
classify_btn = gr.Button("Classify Solution", variant="primary")
|
433 |
clear_btn = gr.Button("Clear")
|
434 |
|
435 |
+
# -------- Right: outputs --------
|
436 |
with gr.Column(scale=1):
|
437 |
classification_output = gr.Textbox(label="Classification", interactive=False)
|
438 |
explanation_output = gr.Textbox(label="Explanation", interactive=False, lines=6)
|
439 |
status_output = gr.Markdown(value="*(idle)*") # live stage updates
|
440 |
|
441 |
+
# -------- Examples --------
|
442 |
gr.Examples(
|
443 |
examples=[
|
444 |
["Solve for x: 2x + 5 = 13",
|
|
|
451 |
inputs=[question_input, solution_input],
|
452 |
)
|
453 |
|
454 |
+
# -------- Wiring --------
|
455 |
classify_btn.click(
|
456 |
+
fn=classify_solution_stream, # <- generator that yields (classification, explanation, status)
|
457 |
inputs=[question_input, solution_input],
|
458 |
outputs=[classification_output, explanation_output, status_output],
|
459 |
+
show_progress=False, # <- no Gradio progress bars
|
460 |
+
concurrency_limit=1, # <- per-event limit (good for GPU)
|
461 |
)
|
462 |
|
463 |
clear_btn.click(
|
|
|
473 |
queue=False,
|
474 |
)
|
475 |
|
476 |
+
# enable queue for streaming (no deprecated args)
|
477 |
app.queue()
|
478 |
|
479 |
if __name__ == "__main__":
|
480 |
app.launch()
|
481 |
|
482 |
|
483 |
+
|