Toya0421 commited on
Commit
d2b3910
·
verified ·
1 Parent(s): 9be2208

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py CHANGED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import random
4
+
5
+ # 難易度ごとの教材(ここではサンプル)
6
+ texts = {
7
+ 300: "Tom has a red ball. He plays with it in the park. The sun is bright.",
8
+ 600: "A young boy found a lost puppy near the river. He decided to take care of it.",
9
+ 850: "Sarah enjoyed reading stories about ancient civilizations and their discoveries.",
10
+ 1050: "The scientist developed a new hypothesis about the evolution of animal behavior.",
11
+ 1250: "Philosophers have long debated the intricate relationship between free will and determinism."
12
+ }
13
+
14
+ levels = [300, 600, 850, 1050, 1250]
15
+
16
+ client = None
17
+
18
+ def init_client(api_key):
19
+ global client
20
+ client = OpenAI(api_key=api_key)
21
+
22
+ def generate_question(api_key, text):
23
+ init_client(api_key)
24
+ prompt = f"""
25
+ Read the following passage and generate a multiple-choice question with 4 options (A-D).
26
+ Clearly mark the correct answer with an asterisk (*).
27
+
28
+ Passage: {text}
29
+ """
30
+ response = client.chat.completions.create(
31
+ model="gpt-4o-mini",
32
+ messages=[{"role": "user", "content": prompt}]
33
+ )
34
+ return response.choices[0].message.content.strip()
35
+
36
+ def adaptive_test(api_key, prev_level, prev_correct):
37
+ idx = levels.index(prev_level)
38
+ if prev_correct and idx < len(levels) - 1:
39
+ new_level = levels[idx + 1]
40
+ elif not prev_correct and idx > 0:
41
+ new_level = levels[idx - 1]
42
+ else:
43
+ new_level = prev_level
44
+
45
+ text = texts[new_level]
46
+ question = generate_question(api_key, text)
47
+ return new_level, text, question
48
+
49
+ # --- Gradio UI ---
50
+ def start_test(api_key):
51
+ level = 850 # 中央から開始
52
+ text = texts[level]
53
+ question = generate_question(api_key, text)
54
+ return f"Lexile: {level}L", text, question, level
55
+
56
+ def next_step(api_key, prev_level, user_answer, question_text):
57
+ # 正解の判定(AIが付けた * の位置で確認)
58
+ lines = question_text.split("\n")
59
+ correct_option = None
60
+ for line in lines:
61
+ if "*" in line:
62
+ correct_option = line.strip().replace("*", "").split(".")[0].strip()
63
+ break
64
+
65
+ correct = (user_answer.strip().upper() == correct_option)
66
+ new_level, new_text, new_question = adaptive_test(api_key, prev_level, correct)
67
+
68
+ feedback = "✅ Correct!" if correct else "❌ Incorrect."
69
+ if new_level == prev_level:
70
+ feedback += "\n\n🎯 Your estimated reading level is around **{}L**.".format(new_level)
71
+ return feedback, "", "", new_level
72
+ else:
73
+ return feedback, new_text, new_question, new_level
74
+
75
+ with gr.Blocks() as demo:
76
+ gr.Markdown("# 📘 適応型リーディングテスト (Adaptive Reading Test)")
77
+
78
+ api_key = gr.Textbox(label="🔑 OpenRouter / OpenAI API Key", type="password")
79
+ start_btn = gr.Button("▶️ テストを開始")
80
+
81
+ level_display = gr.Textbox(label="現在のレベル")
82
+ text_display = gr.Textbox(label="教材テキスト")
83
+ question_display = gr.Textbox(label="問題文(選択肢付き)", lines=8)
84
+
85
+ user_answer = gr.Textbox(label="あなたの回答(A〜D)")
86
+ submit_btn = gr.Button("次へ進む")
87
+
88
+ feedback_display = gr.Markdown()
89
+ hidden_level = gr.Number(visible=False)
90
+
91
+ start_btn.click(
92
+ fn=start_test,
93
+ inputs=[api_key],
94
+ outputs=[level_display, text_display, question_display, hidden_level]
95
+ )
96
+
97
+ submit_btn.click(
98
+ fn=next_step,
99
+ inputs=[api_key, hidden_level, user_answer, question_display],
100
+ outputs=[feedback_display, text_display, question_display, hidden_level]
101
+ )
102
+
103
+ demo.launch()