Toya0421 commited on
Commit
4fa98df
·
verified ·
1 Parent(s): 386cf5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -30
app.py CHANGED
@@ -2,11 +2,11 @@ import gradio as gr
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,28 +17,39 @@ texts = {
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 (A–D).
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:
@@ -47,37 +58,47 @@ def adaptive_test(prev_level, prev_correct):
47
  new_level = levels[idx - 1]
48
  else:
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:
@@ -85,26 +106,28 @@ with gr.Blocks() as demo:
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 (A–D)")
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
 
110
  demo.launch()
 
2
  from openai import OpenAI
3
  import random
4
 
5
+ # ✅ OpenRouter APIキー(ここ直接書く
6
+ API_KEY = "sk-or-v1-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
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
+ # --- OpenAIクライアント設定 ---
21
  client = OpenAI(
22
  base_url=BASE_URL,
23
  api_key=API_KEY
24
  )
25
 
26
+ # --- AIに問題生成を依頼 ---
27
  def generate_question(text):
28
  prompt = f"""
29
  Read the following passage and generate ONE multiple-choice question with 4 options (A–D).
30
+ Clearly mark the correct answer with an asterisk (*).
31
+ The format must be:
32
+
33
+ Q: <question text>
34
+ A. <option>
35
+ B. <option>
36
+ C. <option>
37
+ D. <option>
38
+ Correct: <A/B/C/D>
39
 
40
  Passage:
41
  {text}
42
  """
43
+
44
  response = client.chat.completions.create(
45
  model="google/gemma-3-27b-it:free",
46
  messages=[{"role": "user", "content": prompt}],
47
+ max_tokens=400,
48
+ temperature=0.7,
49
  )
50
  return response.choices[0].message.content.strip()
51
 
52
+ # --- 適応型テスト進行 ---
53
  def adaptive_test(prev_level, prev_correct):
54
  idx = levels.index(prev_level)
55
  if prev_correct and idx < len(levels) - 1:
 
58
  new_level = levels[idx - 1]
59
  else:
60
  new_level = prev_level
61
+ return new_level
 
 
 
62
 
63
  # --- テスト開始 ---
64
  def start_test():
65
+ level = 850 # 中間レベルから開始
66
  text = texts[level]
67
  question = generate_question(text)
68
+ return f"Lexile: {level}L", text, question, level, None, ""
69
 
70
+ # --- 回答処理して次へ ---
71
  def next_step(prev_level, user_answer, question_text):
72
  # 正解の抽出
 
73
  correct_option = None
74
+ for line in question_text.splitlines():
75
+ if line.lower().startswith("correct:"):
76
+ correct_option = line.split(":")[1].strip().upper()
77
  break
78
 
79
+ correct = (user_answer == correct_option)
80
+
81
+ # レベルを更新
82
+ new_level = adaptive_test(prev_level, correct)
83
+ new_text = texts[new_level]
84
+ new_question = generate_question(new_text)
85
 
86
  feedback = "✅ Correct!" if correct else "❌ Incorrect."
87
  if new_level == prev_level:
88
+ feedback += f"\n🎯 Your estimated reading level is **{new_level}L** (final)."
 
89
  else:
90
+ feedback += f"\n➡️ Moving to next level: **{new_level}L**"
91
+
92
+ # 回答欄をリセットするために "" を返す
93
+ return (
94
+ feedback,
95
+ f"Lexile: {new_level}L",
96
+ new_text,
97
+ new_question,
98
+ new_level,
99
+ None, # user_answerをリセット
100
+ ""
101
+ )
102
 
103
  # --- Gradio UI ---
104
  with gr.Blocks() as demo:
 
106
 
107
  start_btn = gr.Button("▶️ Start Test")
108
 
109
+ level_display = gr.Textbox(label="Current Level", interactive=False)
110
+ text_display = gr.Textbox(label="Reading Passage", lines=6, interactive=False)
111
+ question_display = gr.Textbox(label="Generated Question", lines=8, interactive=False)
112
 
113
+ user_answer = gr.Radio(choices=["A", "B", "C", "D"], label="Your Answer")
114
+ submit_btn = gr.Button("Submit Answer")
115
 
116
  feedback_display = gr.Markdown()
117
  hidden_level = gr.Number(visible=False)
118
 
119
+ # --- Start Test ---
120
  start_btn.click(
121
  fn=start_test,
122
  inputs=[],
123
+ outputs=[level_display, text_display, question_display, hidden_level, user_answer, feedback_display]
124
  )
125
 
126
+ # --- Submit & Move to Next ---
127
  submit_btn.click(
128
  fn=next_step,
129
  inputs=[hidden_level, user_answer, question_display],
130
+ outputs=[feedback_display, level_display, text_display, question_display, hidden_level, user_answer, feedback_display]
131
  )
132
 
133
  demo.launch()