Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -958,19 +958,24 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
|
|
| 958 |
mod_name = gr.Textbox(label="Student Name", placeholder="Your full name")
|
| 959 |
mod_class = gr.Textbox(label="Class", placeholder="e.g. 5A")
|
| 960 |
|
| 961 |
-
mod_level_state
|
| 962 |
-
mod_timer_state
|
| 963 |
-
mod_pcount_state
|
| 964 |
-
mod_passages_state
|
|
|
|
| 965 |
|
| 966 |
mod_load_btn = gr.Button("๐ Load My Level", variant="primary")
|
| 967 |
mod_level_display = gr.Markdown()
|
| 968 |
-
|
| 969 |
mod_passages_display = gr.Markdown()
|
| 970 |
|
|
|
|
|
|
|
|
|
|
| 971 |
def mod_load(name, klass):
|
| 972 |
if not name.strip() or not klass.strip():
|
| 973 |
-
return "โ ๏ธ Enter name and class first.", "", "", 2, 0, 0, []
|
|
|
|
| 974 |
level = get_student_level(name, klass, "moderate")
|
| 975 |
passages = passages_for_level(level, klass)
|
| 976 |
pcount = len(passages)
|
|
@@ -979,63 +984,86 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
|
|
| 979 |
f"Passages: **{pcount}** "
|
| 980 |
f"Max stars: **{max_s:.1f} โญ**")
|
| 981 |
if pcount == 0:
|
| 982 |
-
return level_md, "โ ๏ธ No passages found.", "", level, 0, 0, []
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
return level_md,
|
|
|
|
| 988 |
|
| 989 |
mod_load_btn.click(
|
| 990 |
fn=mod_load,
|
| 991 |
inputs=[mod_name, mod_class],
|
| 992 |
-
outputs=[mod_level_display,
|
| 993 |
-
mod_level_state, mod_timer_state, mod_pcount_state,
|
|
|
|
| 994 |
)
|
| 995 |
|
| 996 |
-
mod_show_btn = gr.Button("๐๏ธ Show Passages (
|
| 997 |
|
| 998 |
def mod_show_passages(ts, pcount, passages):
|
| 999 |
-
"""First click starts
|
| 1000 |
if pcount == 0:
|
| 1001 |
-
return gr.update(), "โ ๏ธ Load your level first.", 0
|
| 1002 |
|
| 1003 |
now = time.time()
|
| 1004 |
-
|
| 1005 |
-
# First click: start the clock
|
| 1006 |
if ts == 0:
|
| 1007 |
-
ts = now
|
| 1008 |
|
| 1009 |
elapsed = now - ts
|
| 1010 |
-
|
| 1011 |
visible = []
|
| 1012 |
-
status_parts = []
|
| 1013 |
for i in range(pcount):
|
| 1014 |
-
|
| 1015 |
-
remaining = deadline - elapsed
|
| 1016 |
-
if remaining > 0:
|
| 1017 |
visible.append((i, passages[i]))
|
| 1018 |
-
m = int(remaining) // 60
|
| 1019 |
-
s = int(remaining) % 60
|
| 1020 |
-
status_parts.append(f"Passage {i+1}: {m}m {s:02d}s left")
|
| 1021 |
-
else:
|
| 1022 |
-
status_parts.append(f"Passage {i+1}: HIDDEN โ
")
|
| 1023 |
|
| 1024 |
if not visible:
|
| 1025 |
-
|
| 1026 |
-
|
|
|
|
| 1027 |
|
| 1028 |
parts = []
|
| 1029 |
for idx, p in visible:
|
| 1030 |
parts.append(f"### Passage {idx + 1}\n{p}")
|
| 1031 |
ptext = "\n\n---\n\n".join(parts)
|
| 1032 |
-
|
| 1033 |
-
return ptext,
|
| 1034 |
|
| 1035 |
mod_show_btn.click(
|
| 1036 |
fn=mod_show_passages,
|
| 1037 |
inputs=[mod_timer_state, mod_pcount_state, mod_passages_state],
|
| 1038 |
-
outputs=[mod_passages_display,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1039 |
)
|
| 1040 |
|
| 1041 |
mod_answer = gr.Textbox(label="โ๏ธ Type the English passage(s) here (no copy-paste!)",
|
|
@@ -1045,37 +1073,34 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
|
|
| 1045 |
mod_status = gr.Markdown()
|
| 1046 |
|
| 1047 |
def mod_grade(name, klass, answer, level, ts, pcount):
|
| 1048 |
-
|
| 1049 |
-
|
|
|
|
|
|
|
| 1050 |
elapsed = time.time() - ts
|
| 1051 |
secs_allowed = pcount * 60
|
| 1052 |
if elapsed < secs_allowed:
|
| 1053 |
remaining = int(secs_allowed - elapsed)
|
| 1054 |
return (f"โ ๏ธ Still {remaining} seconds left! "
|
| 1055 |
f"Wait for all passages to disappear, then type from memory."),\
|
| 1056 |
-
"", gr.update(), gr.update(), gr.update(), level,
|
| 1057 |
-
# Must have clicked Show at least once (ts > 0)
|
| 1058 |
-
if ts == 0:
|
| 1059 |
-
return "โ ๏ธ You haven't read the passages yet! Click ๐๏ธ Show Passages first.",\
|
| 1060 |
-
"", gr.update(), gr.update(), gr.update(), level, 0, pcount
|
| 1061 |
|
| 1062 |
report, new_level, status = grade_passage_mode(name, klass, answer, level, "moderate")
|
| 1063 |
new_passages = passages_for_level(new_level, klass)
|
| 1064 |
new_pcount = len(new_passages)
|
| 1065 |
-
ptext = format_passages_display(new_passages)
|
| 1066 |
max_s = compute_max_stars_moderate(new_level)
|
| 1067 |
level_md = (f"### ๐ฎ Your Level: **{new_level}** "
|
| 1068 |
f"Passages: **{new_pcount}** "
|
| 1069 |
f"Max stars: **{max_s:.1f} โญ**")
|
| 1070 |
-
|
| 1071 |
f"Click Load to start!")
|
| 1072 |
-
return report, status, level_md,
|
| 1073 |
|
| 1074 |
mod_submit_btn.click(
|
| 1075 |
fn=mod_grade,
|
| 1076 |
inputs=[mod_name, mod_class, mod_answer, mod_level_state,
|
| 1077 |
mod_timer_state, mod_pcount_state],
|
| 1078 |
-
outputs=[mod_report, mod_status, mod_level_display,
|
| 1079 |
mod_passages_display, mod_level_state, mod_timer_state, mod_pcount_state]
|
| 1080 |
)
|
| 1081 |
|
|
|
|
| 958 |
mod_name = gr.Textbox(label="Student Name", placeholder="Your full name")
|
| 959 |
mod_class = gr.Textbox(label="Class", placeholder="e.g. 5A")
|
| 960 |
|
| 961 |
+
mod_level_state = gr.State(value=2)
|
| 962 |
+
mod_timer_state = gr.State(value=0)
|
| 963 |
+
mod_pcount_state = gr.State(value=0)
|
| 964 |
+
mod_passages_state = gr.State(value=[])
|
| 965 |
+
mod_last_visible_state = gr.State(value=-1) # tracks visible count to avoid redundant updates
|
| 966 |
|
| 967 |
mod_load_btn = gr.Button("๐ Load My Level", variant="primary")
|
| 968 |
mod_level_display = gr.Markdown()
|
| 969 |
+
mod_info_display = gr.Markdown()
|
| 970 |
mod_passages_display = gr.Markdown()
|
| 971 |
|
| 972 |
+
# Silent timer โ only outputs to passages_display and states, no timer text
|
| 973 |
+
mod_timer_component = gr.Timer(1, active=False)
|
| 974 |
+
|
| 975 |
def mod_load(name, klass):
|
| 976 |
if not name.strip() or not klass.strip():
|
| 977 |
+
return ("โ ๏ธ Enter name and class first.", "", "", 2, 0, 0, [], -1,
|
| 978 |
+
gr.Timer(1, active=False))
|
| 979 |
level = get_student_level(name, klass, "moderate")
|
| 980 |
passages = passages_for_level(level, klass)
|
| 981 |
pcount = len(passages)
|
|
|
|
| 984 |
f"Passages: **{pcount}** "
|
| 985 |
f"Max stars: **{max_s:.1f} โญ**")
|
| 986 |
if pcount == 0:
|
| 987 |
+
return (level_md, "โ ๏ธ No passages found.", "", level, 0, 0, [], -1,
|
| 988 |
+
gr.Timer(1, active=False))
|
| 989 |
+
info_md = (f"๐ **{pcount} passage(s) ready โ 1 minute per passage.** "
|
| 990 |
+
f"Click ๐๏ธ **Show Passages** to start reading! "
|
| 991 |
+
f"The clock starts when you click.")
|
| 992 |
+
return (level_md, info_md, "", level, 0, pcount, passages, -1,
|
| 993 |
+
gr.Timer(1, active=False))
|
| 994 |
|
| 995 |
mod_load_btn.click(
|
| 996 |
fn=mod_load,
|
| 997 |
inputs=[mod_name, mod_class],
|
| 998 |
+
outputs=[mod_level_display, mod_info_display, mod_passages_display,
|
| 999 |
+
mod_level_state, mod_timer_state, mod_pcount_state,
|
| 1000 |
+
mod_passages_state, mod_last_visible_state, mod_timer_component]
|
| 1001 |
)
|
| 1002 |
|
| 1003 |
+
mod_show_btn = gr.Button("๐๏ธ Show Passages (clock starts on first click!)")
|
| 1004 |
|
| 1005 |
def mod_show_passages(ts, pcount, passages):
|
| 1006 |
+
"""First click starts clock and shows passages. Also activates the silent timer."""
|
| 1007 |
if pcount == 0:
|
| 1008 |
+
return gr.update(), "โ ๏ธ Load your level first.", 0, -1, gr.Timer(1, active=False)
|
| 1009 |
|
| 1010 |
now = time.time()
|
|
|
|
|
|
|
| 1011 |
if ts == 0:
|
| 1012 |
+
ts = now # first click starts the clock
|
| 1013 |
|
| 1014 |
elapsed = now - ts
|
|
|
|
| 1015 |
visible = []
|
|
|
|
| 1016 |
for i in range(pcount):
|
| 1017 |
+
if elapsed < (i + 1) * 60:
|
|
|
|
|
|
|
| 1018 |
visible.append((i, passages[i]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1019 |
|
| 1020 |
if not visible:
|
| 1021 |
+
return ("๐ *All passages hidden โ time is up! Type what you remember.*",
|
| 1022 |
+
"โฐ **All passages hidden! Now type from memory!**",
|
| 1023 |
+
ts, 0, gr.Timer(1, active=False))
|
| 1024 |
|
| 1025 |
parts = []
|
| 1026 |
for idx, p in visible:
|
| 1027 |
parts.append(f"### Passage {idx + 1}\n{p}")
|
| 1028 |
ptext = "\n\n---\n\n".join(parts)
|
| 1029 |
+
info_md = f"โฑ๏ธ **Reading... {len(visible)} of {pcount} passage(s) visible.** Click ๐๏ธ again to refresh."
|
| 1030 |
+
return ptext, info_md, ts, len(visible), gr.Timer(1, active=True)
|
| 1031 |
|
| 1032 |
mod_show_btn.click(
|
| 1033 |
fn=mod_show_passages,
|
| 1034 |
inputs=[mod_timer_state, mod_pcount_state, mod_passages_state],
|
| 1035 |
+
outputs=[mod_passages_display, mod_info_display, mod_timer_state,
|
| 1036 |
+
mod_last_visible_state, mod_timer_component]
|
| 1037 |
+
)
|
| 1038 |
+
|
| 1039 |
+
def mod_silent_tick(ts, pcount, passages, last_visible):
|
| 1040 |
+
"""Ticks every second. ONLY updates passages_display when a passage expires."""
|
| 1041 |
+
if ts == 0 or pcount == 0:
|
| 1042 |
+
return gr.update(), gr.Timer(1, active=False), last_visible, gr.update()
|
| 1043 |
+
|
| 1044 |
+
elapsed = time.time() - ts
|
| 1045 |
+
visible_count = sum(1 for i in range(pcount) if elapsed < (i + 1) * 60)
|
| 1046 |
+
|
| 1047 |
+
# Nothing changed โ return without touching any visible component
|
| 1048 |
+
if visible_count == last_visible:
|
| 1049 |
+
return gr.update(), gr.Timer(1, active=True), last_visible, gr.update()
|
| 1050 |
+
|
| 1051 |
+
# Something changed โ rebuild passage display
|
| 1052 |
+
if visible_count == 0:
|
| 1053 |
+
return ("๐ *All passages hidden โ time is up! Type what you remember.*",
|
| 1054 |
+
gr.Timer(1, active=False), 0,
|
| 1055 |
+
"โฐ **All passages hidden! Now type from memory!**")
|
| 1056 |
+
|
| 1057 |
+
visible = [(i, passages[i]) for i in range(pcount) if elapsed < (i + 1) * 60]
|
| 1058 |
+
parts = [f"### Passage {idx + 1}\n{p}" for idx, p in visible]
|
| 1059 |
+
ptext = "\n\n---\n\n".join(parts)
|
| 1060 |
+
info_md = f"โฑ๏ธ **Reading... {visible_count} of {pcount} passage(s) visible.**"
|
| 1061 |
+
return ptext, gr.Timer(1, active=True), visible_count, info_md
|
| 1062 |
+
|
| 1063 |
+
mod_timer_component.tick(
|
| 1064 |
+
fn=mod_silent_tick,
|
| 1065 |
+
inputs=[mod_timer_state, mod_pcount_state, mod_passages_state, mod_last_visible_state],
|
| 1066 |
+
outputs=[mod_passages_display, mod_timer_component, mod_last_visible_state, mod_info_display]
|
| 1067 |
)
|
| 1068 |
|
| 1069 |
mod_answer = gr.Textbox(label="โ๏ธ Type the English passage(s) here (no copy-paste!)",
|
|
|
|
| 1073 |
mod_status = gr.Markdown()
|
| 1074 |
|
| 1075 |
def mod_grade(name, klass, answer, level, ts, pcount):
|
| 1076 |
+
if ts == 0:
|
| 1077 |
+
return "โ ๏ธ You haven't read the passages yet! Click ๐๏ธ Show Passages first.",\
|
| 1078 |
+
"", gr.update(), gr.update(), gr.update(), level, 0, pcount
|
| 1079 |
+
if pcount > 0:
|
| 1080 |
elapsed = time.time() - ts
|
| 1081 |
secs_allowed = pcount * 60
|
| 1082 |
if elapsed < secs_allowed:
|
| 1083 |
remaining = int(secs_allowed - elapsed)
|
| 1084 |
return (f"โ ๏ธ Still {remaining} seconds left! "
|
| 1085 |
f"Wait for all passages to disappear, then type from memory."),\
|
| 1086 |
+
"", gr.update(), gr.update(), gr.update(), level, ts, pcount
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1087 |
|
| 1088 |
report, new_level, status = grade_passage_mode(name, klass, answer, level, "moderate")
|
| 1089 |
new_passages = passages_for_level(new_level, klass)
|
| 1090 |
new_pcount = len(new_passages)
|
|
|
|
| 1091 |
max_s = compute_max_stars_moderate(new_level)
|
| 1092 |
level_md = (f"### ๐ฎ Your Level: **{new_level}** "
|
| 1093 |
f"Passages: **{new_pcount}** "
|
| 1094 |
f"Max stars: **{max_s:.1f} โญ**")
|
| 1095 |
+
info_md = (f"๐ **Next round: {new_pcount} passage(s) ร 1 min each.** "
|
| 1096 |
f"Click Load to start!")
|
| 1097 |
+
return report, status, level_md, info_md, "", new_level, 0, 0
|
| 1098 |
|
| 1099 |
mod_submit_btn.click(
|
| 1100 |
fn=mod_grade,
|
| 1101 |
inputs=[mod_name, mod_class, mod_answer, mod_level_state,
|
| 1102 |
mod_timer_state, mod_pcount_state],
|
| 1103 |
+
outputs=[mod_report, mod_status, mod_level_display, mod_info_display,
|
| 1104 |
mod_passages_display, mod_level_state, mod_timer_state, mod_pcount_state]
|
| 1105 |
)
|
| 1106 |
|