Toya0421 commited on
Commit
386cf5a
·
verified ·
1 Parent(s): 5de1877

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -35
app.py CHANGED
@@ -2,7 +2,11 @@ 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.",
@@ -13,27 +17,29 @@ texts = {
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]
@@ -43,60 +49,61 @@ def adaptive_test(api_key, prev_level, prev_correct):
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="あなたの回答(AD")
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
 
 
2
  from openai import OpenAI
3
  import random
4
 
5
+ # ✅ OpenRouter APIキーを直接設定Hugging Face Secretに置き換える場合 os.getenv() 推奨
6
+ API_KEY = "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"
7
+ BASE_URL = "https://openrouter.ai/api/v1"
8
+
9
+ # Lexile難易度別教材
10
  texts = {
11
  300: "Tom has a red ball. He plays with it in the park. The sun is bright.",
12
  600: "A young boy found a lost puppy near the river. He decided to take care of it.",
 
17
 
18
  levels = [300, 600, 850, 1050, 1250]
19
 
20
+ client = OpenAI(
21
+ base_url=BASE_URL,
22
+ api_key=API_KEY
23
+ )
 
24
 
25
+ # --- AIによる問題生成 ---
26
+ def generate_question(text):
27
  prompt = f"""
28
+ Read the following passage and generate ONE multiple-choice question with 4 options (AD).
29
+ Mark the correct answer with an asterisk (*). Use concise English.
30
 
31
+ Passage:
32
+ {text}
33
  """
34
  response = client.chat.completions.create(
35
+ model="google/gemma-3-27b-it:free",
36
+ messages=[{"role": "user", "content": prompt}],
37
+ max_tokens=300
38
  )
39
  return response.choices[0].message.content.strip()
40
 
41
+ # --- 適応的に次の問題を決定 ---
42
+ def adaptive_test(prev_level, prev_correct):
43
  idx = levels.index(prev_level)
44
  if prev_correct and idx < len(levels) - 1:
45
  new_level = levels[idx + 1]
 
49
  new_level = prev_level
50
 
51
  text = texts[new_level]
52
+ question = generate_question(text)
53
  return new_level, text, question
54
 
55
+ # --- テスト開始 ---
56
+ def start_test():
57
+ level = 850 # 真ん中から開始
58
  text = texts[level]
59
+ question = generate_question(text)
60
  return f"Lexile: {level}L", text, question, level
61
 
62
+ # --- 回答処理 ---
63
+ def next_step(prev_level, user_answer, question_text):
64
+ # 正解の抽出
65
  lines = question_text.split("\n")
66
  correct_option = None
67
  for line in lines:
68
  if "*" in line:
69
+ correct_option = line.replace("*", "").split(".")[0].strip().upper()
70
  break
71
 
72
  correct = (user_answer.strip().upper() == correct_option)
73
+ new_level, new_text, new_question = adaptive_test(prev_level, correct)
74
+
75
  feedback = "✅ Correct!" if correct else "❌ Incorrect."
76
  if new_level == prev_level:
77
+ feedback += f"\n\n🎯 Your estimated reading level is **{new_level}L**."
78
  return feedback, "", "", new_level
79
  else:
80
  return feedback, new_text, new_question, new_level
81
 
82
+ # --- Gradio UI ---
83
  with gr.Blocks() as demo:
84
+ gr.Markdown("# 📘 Adaptive Reading Level Test (Lexile-based)")
85
 
86
+ start_btn = gr.Button("▶️ Start Test")
 
87
 
88
+ level_display = gr.Textbox(label="Current Level")
89
+ text_display = gr.Textbox(label="Reading Passage")
90
+ question_display = gr.Textbox(label="Question (A–D)", lines=8)
91
 
92
+ user_answer = gr.Textbox(label="Your Answer (AD)")
93
+ submit_btn = gr.Button("Next")
94
 
95
  feedback_display = gr.Markdown()
96
  hidden_level = gr.Number(visible=False)
97
 
98
  start_btn.click(
99
  fn=start_test,
100
+ inputs=[],
101
  outputs=[level_display, text_display, question_display, hidden_level]
102
  )
103
 
104
  submit_btn.click(
105
  fn=next_step,
106
+ inputs=[hidden_level, user_answer, question_display],
107
  outputs=[feedback_display, text_display, question_display, hidden_level]
108
  )
109