Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,17 +20,18 @@ passages_df = pd.read_csv("passage.csv")
|
|
| 20 |
# --- 状態変数 ---
|
| 21 |
used_passages = set()
|
| 22 |
current_user_id = None
|
| 23 |
-
|
| 24 |
action_log = [] # ページ操作ログ
|
| 25 |
|
| 26 |
|
| 27 |
-
def
|
| 28 |
prompt = f"""
|
| 29 |
-
Rewrite the following passage so it fits about {
|
| 30 |
-
-
|
| 31 |
-
-
|
| 32 |
-
-
|
| 33 |
-
|
|
|
|
| 34 |
{text}
|
| 35 |
"""
|
| 36 |
resp = client.chat.completions.create(
|
|
@@ -63,7 +64,7 @@ def get_new_passage_random():
|
|
| 63 |
|
| 64 |
used_passages.add(pid)
|
| 65 |
|
| 66 |
-
return pid, row["text"], row.get("
|
| 67 |
|
| 68 |
|
| 69 |
def save_log(entry):
|
|
@@ -85,9 +86,9 @@ def save_log(entry):
|
|
| 85 |
# コールバック
|
| 86 |
# ============================
|
| 87 |
|
| 88 |
-
def start_test(student_id,
|
| 89 |
"""開始:ジャンル選択なし版"""
|
| 90 |
-
global current_user_id,
|
| 91 |
used_passages = set()
|
| 92 |
action_log = []
|
| 93 |
|
|
@@ -102,9 +103,9 @@ def start_test(student_id, lexile_input):
|
|
| 102 |
)
|
| 103 |
|
| 104 |
current_user_id = str(student_id).strip()
|
| 105 |
-
|
| 106 |
|
| 107 |
-
pid, text,
|
| 108 |
if text is None:
|
| 109 |
return (
|
| 110 |
"教材が見つかりません", "", json.dumps([]), 0, "",
|
|
@@ -114,7 +115,7 @@ def start_test(student_id, lexile_input):
|
|
| 114 |
gr.update(interactive=False, visible=False)
|
| 115 |
)
|
| 116 |
|
| 117 |
-
rewritten =
|
| 118 |
pages = split_pages(rewritten)
|
| 119 |
start_time = (datetime.utcnow() + timedelta(hours=9)).isoformat()
|
| 120 |
total = len(pages)
|
|
@@ -137,15 +138,15 @@ def start_test(student_id, lexile_input):
|
|
| 137 |
start_time,
|
| 138 |
total,
|
| 139 |
pid,
|
| 140 |
-
|
| 141 |
-
|
| 142 |
prev_upd,
|
| 143 |
next_upd,
|
| 144 |
finish_upd
|
| 145 |
)
|
| 146 |
|
| 147 |
|
| 148 |
-
def next_page(pages_json, current_page, total_pages, pid,
|
| 149 |
pages = json.loads(pages_json)
|
| 150 |
if not pages:
|
| 151 |
# 安全措置
|
|
@@ -230,15 +231,15 @@ def prev_page(pages_json, current_page, total_pages):
|
|
| 230 |
)
|
| 231 |
|
| 232 |
|
| 233 |
-
def finish_or_retire(pages_json, current_page, pid,
|
| 234 |
pages = json.loads(pages_json)
|
| 235 |
now = (datetime.utcnow() + timedelta(hours=9)).isoformat()
|
| 236 |
|
| 237 |
entry = {
|
| 238 |
"user_id": current_user_id,
|
| 239 |
-
"
|
| 240 |
"passage_id": pid,
|
| 241 |
-
"
|
| 242 |
"first_page_displayed": start_time,
|
| 243 |
"finished_time": now,
|
| 244 |
"actions": json.dumps(action_log, ensure_ascii=False),
|
|
@@ -247,7 +248,7 @@ def finish_or_retire(pages_json, current_page, pid, orig_lex, start_time, action
|
|
| 247 |
save_log(entry)
|
| 248 |
|
| 249 |
# 新しい教材取得
|
| 250 |
-
new_pid, new_text,
|
| 251 |
if new_text is None:
|
| 252 |
return (
|
| 253 |
"教材がありません", "", json.dumps([]), 0, "",
|
|
@@ -257,7 +258,7 @@ def finish_or_retire(pages_json, current_page, pid, orig_lex, start_time, action
|
|
| 257 |
gr.update(interactive=False, visible=False)
|
| 258 |
)
|
| 259 |
|
| 260 |
-
rewritten = rewrite_to_lexile(new_text,
|
| 261 |
new_pages = split_pages(rewritten)
|
| 262 |
new_start = (datetime.utcnow() + timedelta(hours=9)).isoformat()
|
| 263 |
total = len(new_pages)
|
|
@@ -281,8 +282,8 @@ def finish_or_retire(pages_json, current_page, pid, orig_lex, start_time, action
|
|
| 281 |
new_start,
|
| 282 |
total,
|
| 283 |
new_pid,
|
| 284 |
-
|
| 285 |
-
|
| 286 |
prev_upd,
|
| 287 |
next_upd,
|
| 288 |
finish_upd
|
|
@@ -310,7 +311,7 @@ with gr.Blocks() as demo:
|
|
| 310 |
hidden_start_time = gr.Textbox(visible=False)
|
| 311 |
hidden_total_pages = gr.Number(visible=False)
|
| 312 |
hidden_passage_id = gr.Textbox(visible=False)
|
| 313 |
-
|
| 314 |
hidden_assigned_lex = gr.Textbox(visible=False)
|
| 315 |
|
| 316 |
with gr.Row():
|
|
@@ -324,12 +325,12 @@ with gr.Blocks() as demo:
|
|
| 324 |
# Start
|
| 325 |
start_btn.click(
|
| 326 |
fn=start_test,
|
| 327 |
-
inputs=[student_id_input,
|
| 328 |
outputs=[
|
| 329 |
text_display, page_display,
|
| 330 |
hidden_pages, hidden_page_index,
|
| 331 |
hidden_start_time, hidden_total_pages,
|
| 332 |
-
hidden_passage_id,
|
| 333 |
prev_btn, next_btn, finish_btn
|
| 334 |
]
|
| 335 |
)
|
|
@@ -340,7 +341,7 @@ with gr.Blocks() as demo:
|
|
| 340 |
inputs=[
|
| 341 |
hidden_pages, hidden_page_index,
|
| 342 |
hidden_total_pages, hidden_passage_id,
|
| 343 |
-
|
| 344 |
],
|
| 345 |
outputs=[
|
| 346 |
text_display, page_display,
|
|
@@ -368,7 +369,7 @@ with gr.Blocks() as demo:
|
|
| 368 |
text_display, page_display,
|
| 369 |
hidden_pages, hidden_page_index,
|
| 370 |
hidden_start_time, hidden_total_pages, hidden_passage_id,
|
| 371 |
-
|
| 372 |
prev_btn, next_btn, finish_btn
|
| 373 |
]
|
| 374 |
)
|
|
@@ -380,15 +381,15 @@ with gr.Blocks() as demo:
|
|
| 380 |
),
|
| 381 |
inputs=[
|
| 382 |
hidden_pages, hidden_page_index,
|
| 383 |
-
hidden_passage_id,
|
| 384 |
hidden_start_time
|
| 385 |
],
|
| 386 |
outputs=[
|
| 387 |
text_display, page_display,
|
| 388 |
hidden_pages, hidden_page_index,
|
| 389 |
hidden_start_time, hidden_total_pages,
|
| 390 |
-
hidden_passage_id,
|
| 391 |
-
|
| 392 |
]
|
| 393 |
)
|
| 394 |
|
|
|
|
| 20 |
# --- 状態変数 ---
|
| 21 |
used_passages = set()
|
| 22 |
current_user_id = None
|
| 23 |
+
current_level = None
|
| 24 |
action_log = [] # ページ操作ログ
|
| 25 |
|
| 26 |
|
| 27 |
+
def rewrite_level(text, target_level): # 変更点
|
| 28 |
prompt = f"""
|
| 29 |
+
Rewrite the following passage so it fits about {target_level} Flesch Reading Ease Score
|
| 30 |
+
- Preserve the original meaning faithfully.
|
| 31 |
+
- Do not add new information or remove essential information.
|
| 32 |
+
- Output only the rewritten passage. Do not include explanations.
|
| 33 |
+
|
| 34 |
+
|
| 35 |
{text}
|
| 36 |
"""
|
| 37 |
resp = client.chat.completions.create(
|
|
|
|
| 64 |
|
| 65 |
used_passages.add(pid)
|
| 66 |
|
| 67 |
+
return pid, row["text"], row.get("original_level", None)
|
| 68 |
|
| 69 |
|
| 70 |
def save_log(entry):
|
|
|
|
| 86 |
# コールバック
|
| 87 |
# ============================
|
| 88 |
|
| 89 |
+
def start_test(student_id, level_input):
|
| 90 |
"""開始:ジャンル選択なし版"""
|
| 91 |
+
global current_user_id, current_level, used_passages, action_log # 変更点
|
| 92 |
used_passages = set()
|
| 93 |
action_log = []
|
| 94 |
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
current_user_id = str(student_id).strip()
|
| 106 |
+
current_level = int(level_input) # 変更点
|
| 107 |
|
| 108 |
+
pid, text, orig_lev = get_new_passage_random()
|
| 109 |
if text is None:
|
| 110 |
return (
|
| 111 |
"教材が見つかりません", "", json.dumps([]), 0, "",
|
|
|
|
| 115 |
gr.update(interactive=False, visible=False)
|
| 116 |
)
|
| 117 |
|
| 118 |
+
rewritten = rewrite_level(text, current_level) # 変更点
|
| 119 |
pages = split_pages(rewritten)
|
| 120 |
start_time = (datetime.utcnow() + timedelta(hours=9)).isoformat()
|
| 121 |
total = len(pages)
|
|
|
|
| 138 |
start_time,
|
| 139 |
total,
|
| 140 |
pid,
|
| 141 |
+
orig_lev,
|
| 142 |
+
current_level,
|
| 143 |
prev_upd,
|
| 144 |
next_upd,
|
| 145 |
finish_upd
|
| 146 |
)
|
| 147 |
|
| 148 |
|
| 149 |
+
def next_page(pages_json, current_page, total_pages, pid, orig_lev, start_time):
|
| 150 |
pages = json.loads(pages_json)
|
| 151 |
if not pages:
|
| 152 |
# 安全措置
|
|
|
|
| 231 |
)
|
| 232 |
|
| 233 |
|
| 234 |
+
def finish_or_retire(pages_json, current_page, pid, orig_lev, start_time, action):
|
| 235 |
pages = json.loads(pages_json)
|
| 236 |
now = (datetime.utcnow() + timedelta(hours=9)).isoformat()
|
| 237 |
|
| 238 |
entry = {
|
| 239 |
"user_id": current_user_id,
|
| 240 |
+
"assigned_level": current_level, # 変更点
|
| 241 |
"passage_id": pid,
|
| 242 |
+
"original_level": orig_lev,
|
| 243 |
"first_page_displayed": start_time,
|
| 244 |
"finished_time": now,
|
| 245 |
"actions": json.dumps(action_log, ensure_ascii=False),
|
|
|
|
| 248 |
save_log(entry)
|
| 249 |
|
| 250 |
# 新しい教材取得
|
| 251 |
+
new_pid, new_text, new_orig_lev = get_new_passage_random()
|
| 252 |
if new_text is None:
|
| 253 |
return (
|
| 254 |
"教材がありません", "", json.dumps([]), 0, "",
|
|
|
|
| 258 |
gr.update(interactive=False, visible=False)
|
| 259 |
)
|
| 260 |
|
| 261 |
+
rewritten = rewrite_to_lexile(new_text, current_level) # 変更点
|
| 262 |
new_pages = split_pages(rewritten)
|
| 263 |
new_start = (datetime.utcnow() + timedelta(hours=9)).isoformat()
|
| 264 |
total = len(new_pages)
|
|
|
|
| 282 |
new_start,
|
| 283 |
total,
|
| 284 |
new_pid,
|
| 285 |
+
new_orig_lev,
|
| 286 |
+
current_level, # 変更点
|
| 287 |
prev_upd,
|
| 288 |
next_upd,
|
| 289 |
finish_upd
|
|
|
|
| 311 |
hidden_start_time = gr.Textbox(visible=False)
|
| 312 |
hidden_total_pages = gr.Number(visible=False)
|
| 313 |
hidden_passage_id = gr.Textbox(visible=False)
|
| 314 |
+
hidden_orig_lev = gr.Textbox(visible=False)
|
| 315 |
hidden_assigned_lex = gr.Textbox(visible=False)
|
| 316 |
|
| 317 |
with gr.Row():
|
|
|
|
| 325 |
# Start
|
| 326 |
start_btn.click(
|
| 327 |
fn=start_test,
|
| 328 |
+
inputs=[student_id_input, level_input],
|
| 329 |
outputs=[
|
| 330 |
text_display, page_display,
|
| 331 |
hidden_pages, hidden_page_index,
|
| 332 |
hidden_start_time, hidden_total_pages,
|
| 333 |
+
hidden_passage_id, hidden_orig_lev, hidden_assigned_lev,
|
| 334 |
prev_btn, next_btn, finish_btn
|
| 335 |
]
|
| 336 |
)
|
|
|
|
| 341 |
inputs=[
|
| 342 |
hidden_pages, hidden_page_index,
|
| 343 |
hidden_total_pages, hidden_passage_id,
|
| 344 |
+
hidden_orig_lev, hidden_start_time
|
| 345 |
],
|
| 346 |
outputs=[
|
| 347 |
text_display, page_display,
|
|
|
|
| 369 |
text_display, page_display,
|
| 370 |
hidden_pages, hidden_page_index,
|
| 371 |
hidden_start_time, hidden_total_pages, hidden_passage_id,
|
| 372 |
+
hidden_orig_lev, hidden_assigned_lev,
|
| 373 |
prev_btn, next_btn, finish_btn
|
| 374 |
]
|
| 375 |
)
|
|
|
|
| 381 |
),
|
| 382 |
inputs=[
|
| 383 |
hidden_pages, hidden_page_index,
|
| 384 |
+
hidden_passage_id, hidden_orig_lev,
|
| 385 |
hidden_start_time
|
| 386 |
],
|
| 387 |
outputs=[
|
| 388 |
text_display, page_display,
|
| 389 |
hidden_pages, hidden_page_index,
|
| 390 |
hidden_start_time, hidden_total_pages,
|
| 391 |
+
hidden_passage_id, hidden_orig_lev,
|
| 392 |
+
hidden_assigned_lev, prev_btn, next_btn, finish_btn
|
| 393 |
]
|
| 394 |
)
|
| 395 |
|