Upload app.py
Browse files
app.py
CHANGED
@@ -693,52 +693,18 @@ def _rows_to_table(rows: list[dict]) -> list[list[str]]:
|
|
693 |
])
|
694 |
return table
|
695 |
|
696 |
-
def _dropdown_choices(rows: list[dict]) -> list[tuple[str, int]]:
|
697 |
-
# Friendly labels mapped to ID values
|
698 |
-
choices = []
|
699 |
-
for r in rows:
|
700 |
-
label = f'#{r["id"]} — {r["label"]} — {_truncate(r["question"], 60)}'
|
701 |
-
choices.append((label, r["id"]))
|
702 |
-
return choices
|
703 |
|
704 |
# ===== Gradio callbacks for examples =====
|
705 |
-
def ui_see_more(selector, rows, filter_label):
|
706 |
-
"""Append a chunk of examples to the browser."""
|
707 |
-
chunk = selector.next_batch(k=6, filter_label=filter_label)
|
708 |
-
rows = (rows or []) + chunk
|
709 |
-
return (
|
710 |
-
rows, # rows_state
|
711 |
-
gr.update(value=_rows_to_table(rows)), # examples_df
|
712 |
-
gr.update(choices=_dropdown_choices(rows), value=None), # row_picker
|
713 |
-
)
|
714 |
-
|
715 |
-
def ui_reset_examples():
|
716 |
-
"""Reset per-session selector and clear the browser."""
|
717 |
-
sel = new_selector()
|
718 |
-
rows: list[dict] = []
|
719 |
-
return (
|
720 |
-
sel, # selector_state
|
721 |
-
rows, # rows_state
|
722 |
-
gr.update(value=_rows_to_table(rows)), # examples_df
|
723 |
-
gr.update(choices=[], value=None), # row_picker
|
724 |
-
)
|
725 |
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
if r["id"] == selected_id:
|
732 |
-
return r["question"], r["solution"]
|
733 |
-
return gr.update(), gr.update()
|
734 |
-
|
735 |
-
def ui_surprise(selector, filter_label):
|
736 |
-
"""Pick one example and push it straight to inputs."""
|
737 |
r = selector.surprise(filter_label=filter_label)
|
738 |
if not r:
|
739 |
-
|
740 |
-
|
741 |
-
return r["question"], r["solution"]
|
742 |
|
743 |
|
744 |
# ---------------- UI: add CSV-driven examples ----------------
|
@@ -751,7 +717,6 @@ with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
|
751 |
|
752 |
# Per-session state
|
753 |
selector_state = gr.State(new_selector())
|
754 |
-
rows_state = gr.State([]) # list[dict] rows currently in the browser
|
755 |
|
756 |
with gr.Row():
|
757 |
# -------- Left: inputs --------
|
@@ -780,46 +745,18 @@ with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
|
780 |
# -------- Curated starter examples (static) --------
|
781 |
gr.Examples(
|
782 |
examples=[
|
783 |
-
|
784 |
-
"2x + 5 = 13\n2x = 13 - 5\n2x = 8\nx = 4"],
|
785 |
-
["John has three apples and Mary has seven, how many apples do they have together?",
|
786 |
"They have 7 + 3 = 11 apples."],
|
|
|
|
|
787 |
["What is 15% of 200?",
|
788 |
"15% = 15/100 = 0.15\n0.15 × 200 = 30"],
|
|
|
|
|
789 |
],
|
790 |
inputs=[question_input, solution_input],
|
791 |
)
|
792 |
-
|
793 |
-
# -------- Dynamic browser (CSV) --------
|
794 |
-
with gr.Accordion("Browse more examples", open=False):
|
795 |
-
with gr.Row():
|
796 |
-
filter_dd = gr.Dropdown(
|
797 |
-
label="Filter",
|
798 |
-
choices=[
|
799 |
-
("Any", "any"),
|
800 |
-
("Correct only", "correct"),
|
801 |
-
("Conceptual error only", "conceptual_error"),
|
802 |
-
("Computational error only", "computational_error"),
|
803 |
-
],
|
804 |
-
value="any",
|
805 |
-
allow_custom_value=False,
|
806 |
-
)
|
807 |
-
see_more_btn = gr.Button("See more")
|
808 |
-
reset_list_btn = gr.Button("Reset list")
|
809 |
-
|
810 |
-
examples_df = gr.Dataframe(
|
811 |
-
headers=["ID", "Label", "Question", "Solution"],
|
812 |
-
value=[],
|
813 |
-
interactive=False,
|
814 |
-
row_count=(0, "dynamic"),
|
815 |
-
col_count=4,
|
816 |
-
wrap=True,
|
817 |
-
height=260,
|
818 |
-
label="Examples",
|
819 |
-
)
|
820 |
-
with gr.Row():
|
821 |
-
row_picker = gr.Dropdown(label="Select example to load", choices=[], value=None, scale=2)
|
822 |
-
load_btn = gr.Button("Load to editor", scale=1)
|
823 |
|
824 |
# ---------- Wiring ----------
|
825 |
# Main classify (streaming)
|
@@ -831,37 +768,14 @@ with gr.Blocks(title="Math Solution Classifier", theme=gr.themes.Soft()) as app:
|
|
831 |
concurrency_limit=1,
|
832 |
)
|
833 |
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
# See more → appends rows to the browser
|
843 |
-
see_more_btn.click(
|
844 |
-
fn=ui_see_more,
|
845 |
-
inputs=[selector_state, rows_state, filter_dd],
|
846 |
-
outputs=[rows_state, examples_df, row_picker],
|
847 |
-
queue=False,
|
848 |
-
)
|
849 |
-
|
850 |
-
# Reset list → new selector + clear table
|
851 |
-
reset_list_btn.click(
|
852 |
-
fn=ui_reset_examples,
|
853 |
-
inputs=None,
|
854 |
-
outputs=[selector_state, rows_state, examples_df, row_picker],
|
855 |
-
queue=False,
|
856 |
-
)
|
857 |
|
858 |
-
# Load selected row → fills main inputs
|
859 |
-
load_btn.click(
|
860 |
-
fn=ui_load_selected,
|
861 |
-
inputs=[rows_state, row_picker],
|
862 |
-
outputs=[question_input, solution_input],
|
863 |
-
queue=False,
|
864 |
-
)
|
865 |
|
866 |
# Enable queue for streaming
|
867 |
app.queue()
|
|
|
693 |
])
|
694 |
return table
|
695 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
696 |
|
697 |
# ===== Gradio callbacks for examples =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
698 |
|
699 |
+
# ---- replace ui_surprise with this ----
|
700 |
+
def ui_surprise(selector, filter_label="any"):
|
701 |
+
"""Pick one example and push it straight to inputs; persist selector state."""
|
702 |
+
if selector is None or not POOL:
|
703 |
+
return selector, gr.update(), gr.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
704 |
r = selector.surprise(filter_label=filter_label)
|
705 |
if not r:
|
706 |
+
return selector, gr.update(), gr.update()
|
707 |
+
return selector, r["question"], r["solution"]
|
|
|
708 |
|
709 |
|
710 |
# ---------------- UI: add CSV-driven examples ----------------
|
|
|
717 |
|
718 |
# Per-session state
|
719 |
selector_state = gr.State(new_selector())
|
|
|
720 |
|
721 |
with gr.Row():
|
722 |
# -------- Left: inputs --------
|
|
|
745 |
# -------- Curated starter examples (static) --------
|
746 |
gr.Examples(
|
747 |
examples=[
|
748 |
+
["John has three apples and Mary has seven, how many apples do they have together?",
|
|
|
|
|
749 |
"They have 7 + 3 = 11 apples."],
|
750 |
+
["A tank holds 60 liters of fuel. A generator uses fuel at a rate of 5 liters per hour. After running for 9 hours, how many liters are still in the tank?",
|
751 |
+
"The generator uses 5 L/h × 9 h = 45 L of fuel in 9 hours.\n Then, there remain 60 L + 45 L = 105 L in the tank.\n Final answer: 105 L"],
|
752 |
["What is 15% of 200?",
|
753 |
"15% = 15/100 = 0.15\n0.15 × 200 = 30"],
|
754 |
+
["A 24-meter rope is cut into 6 equal pieces. A climber uses 2 of those pieces. How many meters of rope are still unused?",
|
755 |
+
"The length of each piece is 24 / 6 = 4 m.\n The climber uses 2 × 4 m = 8 m of rope.\n There are 24 m − 8 m = 16 m of rope still unused."]
|
756 |
],
|
757 |
inputs=[question_input, solution_input],
|
758 |
)
|
759 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
760 |
|
761 |
# ---------- Wiring ----------
|
762 |
# Main classify (streaming)
|
|
|
768 |
concurrency_limit=1,
|
769 |
)
|
770 |
|
771 |
+
# ---- and replace the Surprise button wiring with this ----
|
772 |
+
surprise_btn.click(
|
773 |
+
fn=ui_surprise,
|
774 |
+
inputs=[selector_state], # no filter_dd anymore
|
775 |
+
outputs=[selector_state, question_input, solution_input], # persist selector state
|
776 |
+
queue=True,
|
777 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
778 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
779 |
|
780 |
# Enable queue for streaming
|
781 |
app.queue()
|