Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import random
|
|
|
|
| 4 |
|
| 5 |
-
# ✅ OpenRouter APIキー
|
| 6 |
API_KEY = "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"
|
| 7 |
BASE_URL = "https://openrouter.ai/api/v1"
|
| 8 |
|
|
@@ -27,8 +28,8 @@ client = OpenAI(
|
|
| 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
|
| 31 |
-
|
| 32 |
|
| 33 |
Q: <question text>
|
| 34 |
A. <option>
|
|
@@ -47,9 +48,18 @@ def generate_question(text):
|
|
| 47 |
max_tokens=400,
|
| 48 |
temperature=0.7,
|
| 49 |
)
|
| 50 |
-
|
| 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:
|
|
@@ -64,24 +74,15 @@ def adaptive_test(prev_level, prev_correct):
|
|
| 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,
|
| 69 |
|
| 70 |
# --- 回答を処理して次へ ---
|
| 71 |
-
def next_step(prev_level, user_answer,
|
| 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:
|
|
@@ -89,14 +90,15 @@ def next_step(prev_level, user_answer, question_text):
|
|
| 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 |
-
|
|
|
|
| 100 |
""
|
| 101 |
)
|
| 102 |
|
|
@@ -108,26 +110,44 @@ with gr.Blocks() as demo:
|
|
| 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="
|
| 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=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
# --- Submit & Move to Next ---
|
| 127 |
submit_btn.click(
|
| 128 |
fn=next_step,
|
| 129 |
-
inputs=[hidden_level, user_answer,
|
| 130 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
)
|
| 132 |
|
| 133 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import random
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
+
# ✅ OpenRouter APIキーをここに直接書く
|
| 7 |
API_KEY = "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"
|
| 8 |
BASE_URL = "https://openrouter.ai/api/v1"
|
| 9 |
|
|
|
|
| 28 |
def generate_question(text):
|
| 29 |
prompt = f"""
|
| 30 |
Read the following passage and generate ONE multiple-choice question with 4 options (A–D).
|
| 31 |
+
Clearly mark the correct answer in the format below.
|
| 32 |
+
Return ONLY in this format:
|
| 33 |
|
| 34 |
Q: <question text>
|
| 35 |
A. <option>
|
|
|
|
| 48 |
max_tokens=400,
|
| 49 |
temperature=0.7,
|
| 50 |
)
|
| 51 |
+
full_output = response.choices[0].message.content.strip()
|
| 52 |
|
| 53 |
+
# --- 正解を抽出 ---
|
| 54 |
+
match = re.search(r"Correct:\s*([A-D])", full_output, re.IGNORECASE)
|
| 55 |
+
correct_option = match.group(1).upper() if match else None
|
| 56 |
+
|
| 57 |
+
# --- 「Correct:」行を削除して受験者には非表示にする ---
|
| 58 |
+
visible_question = re.sub(r"(?i)Correct:\s*[A-D]", "", full_output).strip()
|
| 59 |
+
|
| 60 |
+
return visible_question, correct_option
|
| 61 |
+
|
| 62 |
+
# --- 適応型ロジック ---
|
| 63 |
def adaptive_test(prev_level, prev_correct):
|
| 64 |
idx = levels.index(prev_level)
|
| 65 |
if prev_correct and idx < len(levels) - 1:
|
|
|
|
| 74 |
def start_test():
|
| 75 |
level = 850 # 中間レベルから開始
|
| 76 |
text = texts[level]
|
| 77 |
+
question, correct = generate_question(text)
|
| 78 |
+
return f"Lexile: {level}L", text, question, level, correct, "", ""
|
| 79 |
|
| 80 |
# --- 回答を処理して次へ ---
|
| 81 |
+
def next_step(prev_level, user_answer, correct_option):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
correct = (user_answer == correct_option)
|
|
|
|
|
|
|
| 83 |
new_level = adaptive_test(prev_level, correct)
|
| 84 |
new_text = texts[new_level]
|
| 85 |
+
new_question, new_correct = generate_question(new_text)
|
| 86 |
|
| 87 |
feedback = "✅ Correct!" if correct else "❌ Incorrect."
|
| 88 |
if new_level == prev_level:
|
|
|
|
| 90 |
else:
|
| 91 |
feedback += f"\n➡️ Moving to next level: **{new_level}L**"
|
| 92 |
|
| 93 |
+
# 回答欄をリセット
|
| 94 |
return (
|
| 95 |
feedback,
|
| 96 |
f"Lexile: {new_level}L",
|
| 97 |
new_text,
|
| 98 |
new_question,
|
| 99 |
new_level,
|
| 100 |
+
new_correct,
|
| 101 |
+
None, # user_answerリセット
|
| 102 |
""
|
| 103 |
)
|
| 104 |
|
|
|
|
| 110 |
|
| 111 |
level_display = gr.Textbox(label="Current Level", interactive=False)
|
| 112 |
text_display = gr.Textbox(label="Reading Passage", lines=6, interactive=False)
|
| 113 |
+
question_display = gr.Textbox(label="Question", lines=8, interactive=False)
|
| 114 |
|
| 115 |
user_answer = gr.Radio(choices=["A", "B", "C", "D"], label="Your Answer")
|
| 116 |
submit_btn = gr.Button("Submit Answer")
|
| 117 |
|
| 118 |
feedback_display = gr.Markdown()
|
| 119 |
hidden_level = gr.Number(visible=False)
|
| 120 |
+
hidden_correct = gr.Textbox(visible=False)
|
| 121 |
|
| 122 |
# --- Start Test ---
|
| 123 |
start_btn.click(
|
| 124 |
fn=start_test,
|
| 125 |
inputs=[],
|
| 126 |
+
outputs=[
|
| 127 |
+
level_display,
|
| 128 |
+
text_display,
|
| 129 |
+
question_display,
|
| 130 |
+
hidden_level,
|
| 131 |
+
hidden_correct,
|
| 132 |
+
user_answer,
|
| 133 |
+
feedback_display,
|
| 134 |
+
],
|
| 135 |
)
|
| 136 |
|
| 137 |
# --- Submit & Move to Next ---
|
| 138 |
submit_btn.click(
|
| 139 |
fn=next_step,
|
| 140 |
+
inputs=[hidden_level, user_answer, hidden_correct],
|
| 141 |
+
outputs=[
|
| 142 |
+
feedback_display,
|
| 143 |
+
level_display,
|
| 144 |
+
text_display,
|
| 145 |
+
question_display,
|
| 146 |
+
hidden_level,
|
| 147 |
+
hidden_correct,
|
| 148 |
+
user_answer,
|
| 149 |
+
feedback_display,
|
| 150 |
+
],
|
| 151 |
)
|
| 152 |
|
| 153 |
demo.launch()
|