Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -629,9 +629,8 @@ Read them carefully — **passages disappear one by one** as time runs out!
|
|
| 629 |
- **Passage 1** disappears after **1 minute**
|
| 630 |
- **Passage 2** disappears after **2 minutes**
|
| 631 |
- **Passage 3** disappears after **3 minutes**
|
| 632 |
-
- ... and so on
|
| 633 |
|
| 634 |
-
A **live countdown** shows how much time each passage has left.
|
| 635 |
Once all passages are hidden, type what you remember!
|
| 636 |
|
| 637 |
### Scoring
|
|
@@ -955,21 +954,20 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
|
|
| 955 |
mod_class = gr.Textbox(label="Class", placeholder="e.g. 5A")
|
| 956 |
|
| 957 |
mod_level_state = gr.State(value=2)
|
| 958 |
-
mod_timer_state = gr.State(value=0) # timestamp when Load was clicked
|
| 959 |
mod_pcount_state = gr.State(value=0) # total passages loaded
|
| 960 |
mod_passages_state = gr.State(value=[]) # list of passage texts
|
| 961 |
-
mod_last_visible_state = gr.State(value=-1) #
|
| 962 |
|
| 963 |
mod_load_btn = gr.Button("🚀 Load My Level & Passages", variant="primary")
|
| 964 |
mod_level_display = gr.Markdown()
|
| 965 |
-
mod_timer_display = gr.HTML()
|
| 966 |
mod_passages_display = gr.Markdown()
|
| 967 |
|
| 968 |
-
mod_timer_component = gr.Timer(1, active=False)
|
| 969 |
|
| 970 |
def mod_load(name, klass):
|
| 971 |
if not name.strip() or not klass.strip():
|
| 972 |
-
return ("⚠️ Enter name and class first.", "",
|
| 973 |
gr.Timer(1, active=False))
|
| 974 |
level = get_student_level(name, klass, "moderate")
|
| 975 |
passages = passages_for_level(level, klass)
|
|
@@ -977,79 +975,58 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
|
|
| 977 |
max_s = compute_max_stars_moderate(level)
|
| 978 |
level_md = (f"### 🎮 Your Level: **{level}** "
|
| 979 |
f"Passages: **{pcount}** "
|
| 980 |
-
f"Max stars: **{max_s:.1f} ⭐**"
|
|
|
|
| 981 |
if pcount == 0:
|
| 982 |
-
return (level_md, "⚠️ No passages found.",
|
| 983 |
gr.Timer(1, active=False))
|
| 984 |
-
ptext
|
| 985 |
-
timer_md = (f"<div style='font-size:1.1em;padding:8px 0'>"
|
| 986 |
-
f"⏱️ <b>Reading time: {pcount} minute(s) total "
|
| 987 |
-
f"({pcount} passage(s) × 1 min each).</b> "
|
| 988 |
-
f"Passages disappear one by one!</div>")
|
| 989 |
ts = time.time()
|
| 990 |
-
return (level_md,
|
| 991 |
gr.Timer(1, active=True))
|
| 992 |
|
| 993 |
mod_load_btn.click(
|
| 994 |
fn=mod_load,
|
| 995 |
inputs=[mod_name, mod_class],
|
| 996 |
-
outputs=[mod_level_display,
|
| 997 |
mod_level_state, mod_timer_state, mod_pcount_state,
|
| 998 |
mod_passages_state, mod_last_visible_state, mod_timer_component]
|
| 999 |
)
|
| 1000 |
|
| 1001 |
def mod_tick(ts, pcount, passages, last_visible):
|
| 1002 |
-
"""Called every second
|
| 1003 |
if ts == 0 or pcount == 0:
|
| 1004 |
-
return gr.update(), gr.
|
| 1005 |
|
| 1006 |
elapsed = time.time() - ts
|
| 1007 |
-
total_secs = pcount * 60
|
| 1008 |
|
| 1009 |
-
#
|
| 1010 |
visible_count = 0
|
| 1011 |
visible_passages = []
|
| 1012 |
-
status_parts = []
|
| 1013 |
for i in range(pcount):
|
| 1014 |
-
|
| 1015 |
-
if elapsed < deadline:
|
| 1016 |
visible_count += 1
|
| 1017 |
visible_passages.append((i, passages[i]))
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
|
| 1021 |
-
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
# Build timer text (always updates — uses gr.HTML for smooth rendering)
|
| 1026 |
if visible_count == 0:
|
| 1027 |
-
timer_md = "<div style='font-size:1.1em;padding:8px 0'>⏰ <b>All passages hidden! Now type from memory!</b></div>"
|
| 1028 |
ptext = "🙈 *All passages hidden — time is up! Type what you remember.*"
|
| 1029 |
-
return ptext,
|
| 1030 |
else:
|
| 1031 |
-
elapsed_int = int(elapsed)
|
| 1032 |
-
total_remaining = int(max(0, total_secs - elapsed))
|
| 1033 |
-
status_html = " | ".join(status_parts)
|
| 1034 |
-
timer_md = (f"<div style='font-size:1.1em;padding:8px 0'>"
|
| 1035 |
-
f"⏱️ <b>Elapsed: {elapsed_int // 60}m {elapsed_int % 60:02d}s</b> "
|
| 1036 |
-
f" Total remaining: <b>{total_remaining // 60}m {total_remaining % 60:02d}s</b>"
|
| 1037 |
-
f"<br>{status_html}</div>")
|
| 1038 |
-
|
| 1039 |
-
# Only rebuild passage display if a passage just disappeared
|
| 1040 |
-
if visible_count != last_visible:
|
| 1041 |
parts = []
|
| 1042 |
for idx, p in visible_passages:
|
| 1043 |
parts.append(f"### Passage {idx + 1}\n{p}")
|
| 1044 |
ptext = "\n\n---\n\n".join(parts)
|
| 1045 |
-
return ptext,
|
| 1046 |
-
else:
|
| 1047 |
-
return gr.update(), timer_md, gr.Timer(1, active=True), visible_count
|
| 1048 |
|
| 1049 |
mod_timer_component.tick(
|
| 1050 |
fn=mod_tick,
|
| 1051 |
inputs=[mod_timer_state, mod_pcount_state, mod_passages_state, mod_last_visible_state],
|
| 1052 |
-
outputs=[mod_passages_display,
|
| 1053 |
)
|
| 1054 |
|
| 1055 |
mod_answer = gr.Textbox(label="✍️ Type the English passage(s) here (no copy-paste!)",
|
|
@@ -1059,15 +1036,15 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
|
|
| 1059 |
mod_status = gr.Markdown()
|
| 1060 |
|
| 1061 |
def mod_grade(name, klass, answer, level, ts, pcount):
|
| 1062 |
-
# Server-side
|
| 1063 |
if ts > 0 and pcount > 0:
|
| 1064 |
elapsed = time.time() - ts
|
| 1065 |
secs_allowed = pcount * 60
|
| 1066 |
if elapsed < secs_allowed:
|
| 1067 |
remaining = int(secs_allowed - elapsed)
|
| 1068 |
-
return (f"⚠️
|
| 1069 |
-
f"
|
| 1070 |
-
"", gr.update(), gr.update(),
|
| 1071 |
|
| 1072 |
report, new_level, status = grade_passage_mode(name, klass, answer, level, "moderate")
|
| 1073 |
new_passages = passages_for_level(new_level, klass)
|
|
@@ -1076,18 +1053,15 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
|
|
| 1076 |
max_s = compute_max_stars_moderate(new_level)
|
| 1077 |
level_md = (f"### 🎮 Your Level: **{new_level}** "
|
| 1078 |
f"Passages: **{new_pcount}** "
|
| 1079 |
-
f"Max stars: **{max_s:.1f} ⭐**"
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
f"({new_pcount} passage(s) × 1 min each).</b> "
|
| 1083 |
-
f"Click Load to start!</div>")
|
| 1084 |
-
return report, status, level_md, timer_md, ptext, new_level, 0, 0
|
| 1085 |
|
| 1086 |
mod_submit_btn.click(
|
| 1087 |
fn=mod_grade,
|
| 1088 |
inputs=[mod_name, mod_class, mod_answer, mod_level_state,
|
| 1089 |
mod_timer_state, mod_pcount_state],
|
| 1090 |
-
outputs=[mod_report, mod_status, mod_level_display,
|
| 1091 |
mod_passages_display, mod_level_state, mod_timer_state, mod_pcount_state]
|
| 1092 |
)
|
| 1093 |
|
|
|
|
| 629 |
- **Passage 1** disappears after **1 minute**
|
| 630 |
- **Passage 2** disappears after **2 minutes**
|
| 631 |
- **Passage 3** disappears after **3 minutes**
|
| 632 |
+
- ... and so on
|
| 633 |
|
|
|
|
| 634 |
Once all passages are hidden, type what you remember!
|
| 635 |
|
| 636 |
### Scoring
|
|
|
|
| 954 |
mod_class = gr.Textbox(label="Class", placeholder="e.g. 5A")
|
| 955 |
|
| 956 |
mod_level_state = gr.State(value=2)
|
| 957 |
+
mod_timer_state = gr.State(value=0) # timestamp when Load was clicked
|
| 958 |
mod_pcount_state = gr.State(value=0) # total passages loaded
|
| 959 |
mod_passages_state = gr.State(value=[]) # list of passage texts
|
| 960 |
+
mod_last_visible_state = gr.State(value=-1) # passages visible on last tick
|
| 961 |
|
| 962 |
mod_load_btn = gr.Button("🚀 Load My Level & Passages", variant="primary")
|
| 963 |
mod_level_display = gr.Markdown()
|
|
|
|
| 964 |
mod_passages_display = gr.Markdown()
|
| 965 |
|
| 966 |
+
mod_timer_component = gr.Timer(1, active=False)
|
| 967 |
|
| 968 |
def mod_load(name, klass):
|
| 969 |
if not name.strip() or not klass.strip():
|
| 970 |
+
return ("⚠️ Enter name and class first.", "", 2, 0, 0, [], -1,
|
| 971 |
gr.Timer(1, active=False))
|
| 972 |
level = get_student_level(name, klass, "moderate")
|
| 973 |
passages = passages_for_level(level, klass)
|
|
|
|
| 975 |
max_s = compute_max_stars_moderate(level)
|
| 976 |
level_md = (f"### 🎮 Your Level: **{level}** "
|
| 977 |
f"Passages: **{pcount}** "
|
| 978 |
+
f"Max stars: **{max_s:.1f} ⭐** "
|
| 979 |
+
f"⏱️ 1 min per passage — they disappear one by one!")
|
| 980 |
if pcount == 0:
|
| 981 |
+
return (level_md, "⚠️ No passages found.", level, 0, 0, [], -1,
|
| 982 |
gr.Timer(1, active=False))
|
| 983 |
+
ptext = format_passages_display(passages)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 984 |
ts = time.time()
|
| 985 |
+
return (level_md, ptext, level, ts, pcount, passages, pcount,
|
| 986 |
gr.Timer(1, active=True))
|
| 987 |
|
| 988 |
mod_load_btn.click(
|
| 989 |
fn=mod_load,
|
| 990 |
inputs=[mod_name, mod_class],
|
| 991 |
+
outputs=[mod_level_display, mod_passages_display,
|
| 992 |
mod_level_state, mod_timer_state, mod_pcount_state,
|
| 993 |
mod_passages_state, mod_last_visible_state, mod_timer_component]
|
| 994 |
)
|
| 995 |
|
| 996 |
def mod_tick(ts, pcount, passages, last_visible):
|
| 997 |
+
"""Called every second. Only touches passages when one disappears."""
|
| 998 |
if ts == 0 or pcount == 0:
|
| 999 |
+
return gr.update(), gr.Timer(1, active=False), last_visible
|
| 1000 |
|
| 1001 |
elapsed = time.time() - ts
|
|
|
|
| 1002 |
|
| 1003 |
+
# How many passages still visible?
|
| 1004 |
visible_count = 0
|
| 1005 |
visible_passages = []
|
|
|
|
| 1006 |
for i in range(pcount):
|
| 1007 |
+
if elapsed < (i + 1) * 60:
|
|
|
|
| 1008 |
visible_count += 1
|
| 1009 |
visible_passages.append((i, passages[i]))
|
| 1010 |
+
|
| 1011 |
+
# Nothing changed — don't touch anything
|
| 1012 |
+
if visible_count == last_visible:
|
| 1013 |
+
return gr.update(), gr.Timer(1, active=True), last_visible
|
| 1014 |
+
|
| 1015 |
+
# Something changed — rebuild passage display
|
|
|
|
|
|
|
| 1016 |
if visible_count == 0:
|
|
|
|
| 1017 |
ptext = "🙈 *All passages hidden — time is up! Type what you remember.*"
|
| 1018 |
+
return ptext, gr.Timer(1, active=False), 0
|
| 1019 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1020 |
parts = []
|
| 1021 |
for idx, p in visible_passages:
|
| 1022 |
parts.append(f"### Passage {idx + 1}\n{p}")
|
| 1023 |
ptext = "\n\n---\n\n".join(parts)
|
| 1024 |
+
return ptext, gr.Timer(1, active=True), visible_count
|
|
|
|
|
|
|
| 1025 |
|
| 1026 |
mod_timer_component.tick(
|
| 1027 |
fn=mod_tick,
|
| 1028 |
inputs=[mod_timer_state, mod_pcount_state, mod_passages_state, mod_last_visible_state],
|
| 1029 |
+
outputs=[mod_passages_display, mod_timer_component, mod_last_visible_state]
|
| 1030 |
)
|
| 1031 |
|
| 1032 |
mod_answer = gr.Textbox(label="✍️ Type the English passage(s) here (no copy-paste!)",
|
|
|
|
| 1036 |
mod_status = gr.Markdown()
|
| 1037 |
|
| 1038 |
def mod_grade(name, klass, answer, level, ts, pcount):
|
| 1039 |
+
# Server-side enforcement: cannot submit until all passages hidden
|
| 1040 |
if ts > 0 and pcount > 0:
|
| 1041 |
elapsed = time.time() - ts
|
| 1042 |
secs_allowed = pcount * 60
|
| 1043 |
if elapsed < secs_allowed:
|
| 1044 |
remaining = int(secs_allowed - elapsed)
|
| 1045 |
+
return (f"⚠️ Still {remaining} seconds left! "
|
| 1046 |
+
f"Wait for all passages to disappear first."),\
|
| 1047 |
+
"", gr.update(), gr.update(), level, 0, 0
|
| 1048 |
|
| 1049 |
report, new_level, status = grade_passage_mode(name, klass, answer, level, "moderate")
|
| 1050 |
new_passages = passages_for_level(new_level, klass)
|
|
|
|
| 1053 |
max_s = compute_max_stars_moderate(new_level)
|
| 1054 |
level_md = (f"### 🎮 Your Level: **{new_level}** "
|
| 1055 |
f"Passages: **{new_pcount}** "
|
| 1056 |
+
f"Max stars: **{max_s:.1f} ⭐** "
|
| 1057 |
+
f"Click Load to start next round!")
|
| 1058 |
+
return report, status, level_md, ptext, new_level, 0, 0
|
|
|
|
|
|
|
|
|
|
| 1059 |
|
| 1060 |
mod_submit_btn.click(
|
| 1061 |
fn=mod_grade,
|
| 1062 |
inputs=[mod_name, mod_class, mod_answer, mod_level_state,
|
| 1063 |
mod_timer_state, mod_pcount_state],
|
| 1064 |
+
outputs=[mod_report, mod_status, mod_level_display,
|
| 1065 |
mod_passages_display, mod_level_state, mod_timer_state, mod_pcount_state]
|
| 1066 |
)
|
| 1067 |
|