Rajarshi255 commited on
Commit
bb30714
ยท
verified ยท
1 Parent(s): 2902721

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -40
app.py CHANGED
@@ -623,16 +623,11 @@ MODERATE_RULES = """
623
  ## ๐Ÿ“‹ Moderate Mode โ€” Full Rules
624
 
625
  You'll see **English passages** with Bengali translations below each one.
626
- Read them, then **the passage disappears after a time limit** โ€” type it back from memory!
627
 
628
  ### โฑ๏ธ Timer
629
- | Level | Passage visible for |
630
- |---|---|
631
- | Level 0 | 1 minute |
632
- | Level 1 | 2 minutes |
633
- | Level 2 | 3 minutes |
634
- | Level 3 | 4 minutes |
635
- | Level N | (N+1) minutes |
636
 
637
  After time is up the passage is **hidden** โ€” type what you remember!
638
 
@@ -663,6 +658,7 @@ After time is up the passage is **hidden** โ€” type what you remember!
663
  - Capitalisation and spacing **do not matter** โ€” the app ignores them
664
  - Gibberish or mindless repetition โ†’ **โˆ’1 โญ**
665
  - **Copy-paste is disabled**
 
666
  """
667
 
668
  EASY_RULES = """
@@ -890,6 +886,7 @@ def format_passages_display(passages: list[str]) -> str:
890
  # ==================== JAVASCRIPT ====================
891
  block_paste_js = """
892
  function() {
 
893
  function blockPaste(id) {
894
  const el = document.querySelector(id + " textarea");
895
  if (el) {
@@ -902,6 +899,58 @@ function() {
902
  blockPaste("#std-summary-input");
903
  blockPaste("#mod-answer-input");
904
  blockPaste("#easy-answer-input");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905
  }
906
  """
907
 
@@ -955,9 +1004,10 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
955
  mod_name = gr.Textbox(label="Student Name", placeholder="Your full name")
956
  mod_class = gr.Textbox(label="Class", placeholder="e.g. 5A")
957
 
958
- mod_level_state = gr.State(value=2)
959
- mod_timer_state = gr.State(value=0)
960
- mod_visible_state = gr.State(value=True)
 
961
 
962
  mod_load_btn = gr.Button("๐Ÿš€ Load My Level & Passages", variant="primary")
963
  mod_level_display = gr.Markdown()
@@ -966,46 +1016,40 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
966
 
967
  def mod_load(name, klass):
968
  if not name.strip() or not klass.strip():
969
- return "โš ๏ธ Enter name and class first.", "", "", 2, 0, True
970
  level = get_student_level(name, klass, "moderate")
971
  passages = passages_for_level(level, klass)
 
972
  ptext = format_passages_display(passages)
973
  max_s = compute_max_stars_moderate(level)
974
- mins = level + 1
975
  level_md = (f"### ๐ŸŽฎ Your Level: **{level}**    "
976
- f"Passages: **{len(passages)}**    "
977
  f"Max stars: **{max_s:.1f} โญ**")
978
- timer_md = f"โฑ๏ธ **You have {mins} minute(s) to read. The passage will disappear after that!**"
979
  ts = time.time()
980
- return level_md, timer_md, ptext, level, ts, True
981
 
982
  mod_load_btn.click(
983
  fn=mod_load,
984
  inputs=[mod_name, mod_class],
985
  outputs=[mod_level_display, mod_timer_display, mod_passages_display,
986
- mod_level_state, mod_timer_state, mod_visible_state]
987
  )
988
 
989
- mod_check_btn = gr.Button("๐Ÿ” Check Timer / Hide Passage")
 
990
 
991
- def mod_check_timer(level, ts, visible):
 
992
  if ts == 0:
993
- return gr.update(), "โš ๏ธ Load passages first.", visible
994
- elapsed = time.time() - ts
995
- secs_allowed = (level + 1) * 60
996
- remaining = max(0.0, secs_allowed - elapsed)
997
- if remaining > 0:
998
- mins_left = int(remaining // 60)
999
- secs_left = int(remaining % 60)
1000
- timer_md = f"โฑ๏ธ **Time remaining: {mins_left}m {secs_left}s**"
1001
- return gr.update(), timer_md, True
1002
- else:
1003
- timer_md = "โฐ **Time's up! Passage hidden. Now type from memory!**"
1004
- return gr.update(value="๐Ÿ™ˆ *Passage hidden โ€” time is up! Type what you remember.*"), timer_md, False
1005
-
1006
- mod_check_btn.click(
1007
- fn=mod_check_timer,
1008
- inputs=[mod_level_state, mod_timer_state, mod_visible_state],
1009
  outputs=[mod_passages_display, mod_timer_display, mod_visible_state]
1010
  )
1011
 
@@ -1015,21 +1059,32 @@ with gr.Blocks(title="Summary & Passage Grader", theme=gr.themes.Soft()) as demo
1015
  mod_report = gr.Markdown()
1016
  mod_status = gr.Markdown()
1017
 
1018
- def mod_grade(name, klass, answer, level):
 
 
 
 
 
 
 
 
 
 
1019
  report, new_level, status = grade_passage_mode(name, klass, answer, level, "moderate")
1020
  passages = passages_for_level(new_level, klass)
 
1021
  ptext = format_passages_display(passages)
1022
  max_s = compute_max_stars_moderate(new_level)
1023
- mins = new_level + 1
1024
  level_md = (f"### ๐ŸŽฎ Your Level: **{new_level}**    "
1025
- f"Passages: **{len(passages)}**    "
1026
  f"Max stars: **{max_s:.1f} โญ**")
1027
- timer_md = f"โฑ๏ธ **Next round: you'll have {mins} minute(s) to read.**"
1028
  return report, status, level_md, timer_md, ptext, new_level, 0, True
1029
 
1030
  mod_submit_btn.click(
1031
  fn=mod_grade,
1032
- inputs=[mod_name, mod_class, mod_answer, mod_level_state],
1033
  outputs=[mod_report, mod_status, mod_level_display, mod_timer_display,
1034
  mod_passages_display, mod_level_state, mod_timer_state, mod_visible_state]
1035
  )
 
623
  ## ๐Ÿ“‹ Moderate Mode โ€” Full Rules
624
 
625
  You'll see **English passages** with Bengali translations below each one.
626
+ Read them, then **the passage disappears automatically** โ€” type it back from memory!
627
 
628
  ### โฑ๏ธ Timer
629
+ - **1 minute per passage** (e.g. 3 passages = 3 minutes)
630
+ - The passage **disappears automatically** when time is up โ€” no button needed!
 
 
 
 
 
631
 
632
  After time is up the passage is **hidden** โ€” type what you remember!
633
 
 
658
  - Capitalisation and spacing **do not matter** โ€” the app ignores them
659
  - Gibberish or mindless repetition โ†’ **โˆ’1 โญ**
660
  - **Copy-paste is disabled**
661
+ - **You cannot submit until the timer expires** โ€” read first, then type from memory!
662
  """
663
 
664
  EASY_RULES = """
 
886
  # ==================== JAVASCRIPT ====================
887
  block_paste_js = """
888
  function() {
889
+ /* Block paste on all answer fields */
890
  function blockPaste(id) {
891
  const el = document.querySelector(id + " textarea");
892
  if (el) {
 
899
  blockPaste("#std-summary-input");
900
  blockPaste("#mod-answer-input");
901
  blockPaste("#easy-answer-input");
902
+
903
+ /* Auto-timer for Moderate Mode */
904
+ var modTimerEnd = null;
905
+ var modTimerInterval = null;
906
+ var modHidden = false;
907
+
908
+ /* Watch for the Load button output โ€” detect when passages appear */
909
+ var observer = new MutationObserver(function() {
910
+ /* Find the timer display โ€” look for the "โฑ๏ธ" text that contains minute info */
911
+ var allMd = document.querySelectorAll('.prose');
912
+ for (var i = 0; i < allMd.length; i++) {
913
+ var txt = allMd[i].textContent || '';
914
+ var match = txt.match(/You have (\\d+) minute/);
915
+ if (match && !modHidden) {
916
+ var mins = parseInt(match[1]);
917
+ if (modTimerEnd === null || txt.indexOf('Next round') === -1) {
918
+ modTimerEnd = Date.now() + mins * 60 * 1000;
919
+ modHidden = false;
920
+ if (modTimerInterval) clearInterval(modTimerInterval);
921
+ modTimerInterval = setInterval(checkModTimer, 1000);
922
+ }
923
+ break;
924
+ }
925
+ }
926
+ });
927
+ observer.observe(document.body, { childList: true, subtree: true, characterData: true });
928
+
929
+ function checkModTimer() {
930
+ if (!modTimerEnd || modHidden) return;
931
+ var remaining = Math.max(0, modTimerEnd - Date.now());
932
+ if (remaining <= 0) {
933
+ modHidden = true;
934
+ modTimerEnd = null;
935
+ if (modTimerInterval) { clearInterval(modTimerInterval); modTimerInterval = null; }
936
+ /* Click the hidden autohide button to trigger server-side hide */
937
+ var btn = document.querySelector('#mod-autohide-btn');
938
+ if (btn) btn.click();
939
+ }
940
+ }
941
+
942
+ /* Reset timer state when Load button is clicked */
943
+ var loadBtns = document.querySelectorAll('button');
944
+ loadBtns.forEach(function(btn) {
945
+ if (btn.textContent.indexOf('Load My Level') !== -1 &&
946
+ btn.closest('.tabitem') &&
947
+ btn.closest('.tabitem').querySelector('#mod-answer-input')) {
948
+ btn.addEventListener('click', function() {
949
+ modTimerEnd = null;
950
+ modHidden = false;
951
+ });
952
+ }
953
+ });
954
  }
955
  """
956
 
 
1004
  mod_name = gr.Textbox(label="Student Name", placeholder="Your full name")
1005
  mod_class = gr.Textbox(label="Class", placeholder="e.g. 5A")
1006
 
1007
+ mod_level_state = gr.State(value=2)
1008
+ mod_timer_state = gr.State(value=0)
1009
+ mod_visible_state = gr.State(value=True)
1010
+ mod_pcount_state = gr.State(value=0) # number of passages loaded
1011
 
1012
  mod_load_btn = gr.Button("๐Ÿš€ Load My Level & Passages", variant="primary")
1013
  mod_level_display = gr.Markdown()
 
1016
 
1017
  def mod_load(name, klass):
1018
  if not name.strip() or not klass.strip():
1019
+ return "โš ๏ธ Enter name and class first.", "", "", 2, 0, True, 0
1020
  level = get_student_level(name, klass, "moderate")
1021
  passages = passages_for_level(level, klass)
1022
+ pcount = len(passages)
1023
  ptext = format_passages_display(passages)
1024
  max_s = compute_max_stars_moderate(level)
1025
+ mins = pcount # 1 minute per passage
1026
  level_md = (f"### ๐ŸŽฎ Your Level: **{level}** &nbsp;&nbsp; "
1027
+ f"Passages: **{pcount}** &nbsp;&nbsp; "
1028
  f"Max stars: **{max_s:.1f} โญ**")
1029
+ timer_md = f"โฑ๏ธ **You have {mins} minute(s) to read ({pcount} passage(s) ร— 1 min each). The passage will disappear automatically!**"
1030
  ts = time.time()
1031
+ return level_md, timer_md, ptext, level, ts, True, pcount
1032
 
1033
  mod_load_btn.click(
1034
  fn=mod_load,
1035
  inputs=[mod_name, mod_class],
1036
  outputs=[mod_level_display, mod_timer_display, mod_passages_display,
1037
+ mod_level_state, mod_timer_state, mod_visible_state, mod_pcount_state]
1038
  )
1039
 
1040
+ # Hidden button triggered by JS auto-timer to hide passage server-side
1041
+ mod_autohide_btn = gr.Button("autohide", visible=False, elem_id="mod-autohide-btn")
1042
 
1043
+ def mod_autohide(ts, pcount):
1044
+ """Called automatically by JS when timer expires."""
1045
  if ts == 0:
1046
+ return gr.update(), "โš ๏ธ Load passages first.", False
1047
+ timer_md = "โฐ **Time's up! Passage hidden. Now type from memory!**"
1048
+ return gr.update(value="๐Ÿ™ˆ *Passage hidden โ€” time is up! Type what you remember.*"), timer_md, False
1049
+
1050
+ mod_autohide_btn.click(
1051
+ fn=mod_autohide,
1052
+ inputs=[mod_timer_state, mod_pcount_state],
 
 
 
 
 
 
 
 
 
1053
  outputs=[mod_passages_display, mod_timer_display, mod_visible_state]
1054
  )
1055
 
 
1059
  mod_report = gr.Markdown()
1060
  mod_status = gr.Markdown()
1061
 
1062
+ def mod_grade(name, klass, answer, level, ts, pcount):
1063
+ # Server-side timer enforcement
1064
+ if ts > 0 and pcount > 0:
1065
+ elapsed = time.time() - ts
1066
+ secs_allowed = pcount * 60
1067
+ if elapsed < secs_allowed:
1068
+ remaining = int(secs_allowed - elapsed)
1069
+ return (f"โš ๏ธ You still have {remaining} seconds left! Read the passage first, "
1070
+ f"then type from memory after it disappears."),\
1071
+ "", gr.update(), gr.update(), gr.update(), level, ts, True
1072
+
1073
  report, new_level, status = grade_passage_mode(name, klass, answer, level, "moderate")
1074
  passages = passages_for_level(new_level, klass)
1075
+ new_pcount = len(passages)
1076
  ptext = format_passages_display(passages)
1077
  max_s = compute_max_stars_moderate(new_level)
1078
+ mins = new_pcount
1079
  level_md = (f"### ๐ŸŽฎ Your Level: **{new_level}** &nbsp;&nbsp; "
1080
+ f"Passages: **{new_pcount}** &nbsp;&nbsp; "
1081
  f"Max stars: **{max_s:.1f} โญ**")
1082
+ timer_md = f"โฑ๏ธ **Next round: you'll have {mins} minute(s) to read ({new_pcount} passage(s) ร— 1 min each).**"
1083
  return report, status, level_md, timer_md, ptext, new_level, 0, True
1084
 
1085
  mod_submit_btn.click(
1086
  fn=mod_grade,
1087
+ inputs=[mod_name, mod_class, mod_answer, mod_level_state, mod_timer_state, mod_pcount_state],
1088
  outputs=[mod_report, mod_status, mod_level_display, mod_timer_display,
1089
  mod_passages_display, mod_level_state, mod_timer_state, mod_visible_state]
1090
  )