Toya0421 commited on
Commit
2ae392c
·
verified ·
1 Parent(s): 024a994

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -27
app.py CHANGED
@@ -1,18 +1,18 @@
1
  import gradio as gr
2
  import random
3
- from openai import OpenAI
4
  import json
 
5
 
6
- # ✅ ここで直接APIキーを指定
7
  API_KEY = "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"
8
 
9
- # ✅ OpenRouter経由のクライアント設定(base_urlを必ず指定)
10
  client = OpenAI(
11
  base_url="https://openrouter.ai/api/v1",
12
  api_key=API_KEY
13
  )
14
 
15
- # ===== Lexileレベル教材 =====
16
  materials = {
17
  300: [
18
  "The cat is on the mat.",
@@ -41,26 +41,27 @@ materials = {
41
  ]
42
  }
43
 
44
- # ===== GPTで問題生成 =====
45
  def generate_question(text):
46
  prompt = f"""
47
  Please create one English reading comprehension question about the following passage.
48
- Format the response strictly as JSON in this format:
49
  {{
50
  "question": "Question text",
51
  "choices": ["A) ...", "B) ...", "C) ...", "D) ..."],
52
  "answer": "A"
53
  }}
54
- Ensure that the correct answer is NOT explicitly shown or hinted in the question or choices.
55
  Passage:
56
  {text}
57
  """
58
 
59
  try:
60
  response = client.chat.completions.create(
61
- model="google/gemma-3-27b-it:free", # ✅ OpenRouter無料モデル
62
  messages=[{"role": "user", "content": prompt}],
63
- max_tokens=300
 
64
  )
65
  data = response.choices[0].message.content.strip()
66
  q = json.loads(data)
@@ -75,14 +76,15 @@ def generate_question(text):
75
 
76
  # ===== テスト初期化 =====
77
  def start_test():
78
- return {
79
- "level": 850,
80
  "asked": [],
81
  "score": 0,
82
  "count": 0
83
- }, "✅ Test started! Beginning from 850L.", "", "", [], ""
 
84
 
85
- # ===== 最初の問題生成 =====
86
  def get_first_question(state):
87
  text = random.choice(materials[state["level"]])
88
  state["asked"].append(text)
@@ -96,7 +98,7 @@ def next_step(state, user_choice):
96
  return state, "Please start the test first.", "", "", [], ""
97
 
98
  correct = user_choice == state["current_q"]["answer"]
99
- feedback = "✅ Correct!" if correct else f"❌ Wrong! The correct answer was {state['current_q']['answer']}."
100
 
101
  # レベル変動
102
  if correct:
@@ -109,10 +111,10 @@ def next_step(state, user_choice):
109
 
110
  # 5問で終了
111
  if state["count"] >= 5:
112
- result = f"🎯 Test finished! Your estimated reading level: {state['level']}L"
113
  return state, result, "", "", [], ""
114
 
115
- # 重複い教材を選
116
  available = [t for t in materials[state["level"]] if t not in state["asked"]]
117
  if not available:
118
  available = materials[state["level"]]
@@ -122,27 +124,39 @@ def next_step(state, user_choice):
122
  q = generate_question(text)
123
  state["current_q"] = q
124
 
 
125
  return state, text, q["question"], "", q["choices"], feedback
126
 
127
  # ===== Gradio UI =====
128
  with gr.Blocks() as demo:
129
- gr.Markdown("## 📘 Adaptive Reading Test (Lexile-based, 5 questions)")
130
 
131
  state = gr.State()
132
- passage = gr.Textbox(label="Reading Passage", interactive=False)
133
- question = gr.Textbox(label="Question", interactive=False)
134
- feedback = gr.Textbox(label="Feedback", interactive=False)
135
- choices = gr.Radio(label="Your Answer", choices=["A", "B", "C", "D"])
136
- msg = gr.Textbox(label="System Message", interactive=False)
137
 
138
  with gr.Row():
139
  start_btn = gr.Button("▶️ Start Test")
140
  next_btn = gr.Button("➡️ Submit & Next")
141
 
142
- start_btn.click(start_test, outputs=[state, msg, passage, question, choices, feedback])\
143
- .then(get_first_question, inputs=state, outputs=[state, passage, question, choices, feedback])
144
-
145
- next_btn.click(next_step, inputs=[state, choices],
146
- outputs=[state, passage, question, choices, feedback, msg])
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  demo.launch(share=True)
 
1
  import gradio as gr
2
  import random
 
3
  import json
4
+ from openai import OpenAI
5
 
6
+ # ✅ APIキーをここに直接指定
7
  API_KEY = "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"
8
 
9
+ # ✅ OpenRouterクライアント設定
10
  client = OpenAI(
11
  base_url="https://openrouter.ai/api/v1",
12
  api_key=API_KEY
13
  )
14
 
15
+ # ===== Lexileレベルごとの教材 =====
16
  materials = {
17
  300: [
18
  "The cat is on the mat.",
 
41
  ]
42
  }
43
 
44
+ # ===== 問題生成 =====
45
  def generate_question(text):
46
  prompt = f"""
47
  Please create one English reading comprehension question about the following passage.
48
+ Format your output strictly as JSON like this:
49
  {{
50
  "question": "Question text",
51
  "choices": ["A) ...", "B) ...", "C) ...", "D) ..."],
52
  "answer": "A"
53
  }}
54
+ The correct answer must not be explicitly revealed in the choices or question text.
55
  Passage:
56
  {text}
57
  """
58
 
59
  try:
60
  response = client.chat.completions.create(
61
+ model="google/gemma-3-27b-it:free",
62
  messages=[{"role": "user", "content": prompt}],
63
+ max_tokens=300,
64
+ temperature=0.7
65
  )
66
  data = response.choices[0].message.content.strip()
67
  q = json.loads(data)
 
76
 
77
  # ===== テスト初期化 =====
78
  def start_test():
79
+ state = {
80
+ "level": 850, # 初期レベル(中間)
81
  "asked": [],
82
  "score": 0,
83
  "count": 0
84
+ }
85
+ return state, "✅ Test started! Please read the passage carefully.", "", "", [], ""
86
 
87
+ # ===== 最初の問題を取得 =====
88
  def get_first_question(state):
89
  text = random.choice(materials[state["level"]])
90
  state["asked"].append(text)
 
98
  return state, "Please start the test first.", "", "", [], ""
99
 
100
  correct = user_choice == state["current_q"]["answer"]
101
+ feedback = "✅ Correct!" if correct else f"❌ Incorrect. The correct answer was {state['current_q']['answer']}."
102
 
103
  # レベル変動
104
  if correct:
 
111
 
112
  # 5問で終了
113
  if state["count"] >= 5:
114
+ result = f"🎯 Test finished! Your estimated reading ability corresponds to approximately {state['level']}L."
115
  return state, result, "", "", [], ""
116
 
117
+ # しい教材を重複なしで
118
  available = [t for t in materials[state["level"]] if t not in state["asked"]]
119
  if not available:
120
  available = materials[state["level"]]
 
124
  q = generate_question(text)
125
  state["current_q"] = q
126
 
127
+ # 次の問題を返す(選択肢リセット)
128
  return state, text, q["question"], "", q["choices"], feedback
129
 
130
  # ===== Gradio UI =====
131
  with gr.Blocks() as demo:
132
+ gr.Markdown("## 📘 Adaptive Reading Test")
133
 
134
  state = gr.State()
135
+ passage = gr.Textbox(label="Reading Passage", lines=6, interactive=False)
136
+ question = gr.Textbox(label="Question", lines=2, interactive=False)
137
+ feedback = gr.Textbox(label="Feedback", lines=2, interactive=False)
138
+ choices = gr.Radio(label="Your Answer", choices=["A", "B", "C", "D"], value=None)
139
+ msg = gr.Textbox(label="Message", lines=2, interactive=False)
140
 
141
  with gr.Row():
142
  start_btn = gr.Button("▶️ Start Test")
143
  next_btn = gr.Button("➡️ Submit & Next")
144
 
145
+ # スタートボタン
146
+ start_btn.click(
147
+ start_test,
148
+ outputs=[state, msg, passage, question, choices, feedback]
149
+ ).then(
150
+ get_first_question,
151
+ inputs=state,
152
+ outputs=[state, passage, question, choices, feedback]
153
+ )
154
+
155
+ # 次の問題ボタン
156
+ next_btn.click(
157
+ next_step,
158
+ inputs=[state, choices],
159
+ outputs=[state, passage, question, choices, feedback, msg]
160
+ )
161
 
162
  demo.launch(share=True)