Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 =
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
client = OpenAI(api_key=api_key)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
prompt = f"""
|
| 25 |
-
Read the following passage and generate
|
| 26 |
-
|
| 27 |
|
| 28 |
-
Passage:
|
|
|
|
| 29 |
"""
|
| 30 |
response = client.chat.completions.create(
|
| 31 |
-
model="
|
| 32 |
-
messages=[{"role": "user", "content": prompt}]
|
|
|
|
| 33 |
)
|
| 34 |
return response.choices[0].message.content.strip()
|
| 35 |
|
| 36 |
-
|
|
|
|
| 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(
|
| 47 |
return new_level, text, question
|
| 48 |
|
| 49 |
-
# ---
|
| 50 |
-
def start_test(
|
| 51 |
-
level = 850 # 中
|
| 52 |
text = texts[level]
|
| 53 |
-
question = generate_question(
|
| 54 |
return f"Lexile: {level}L", text, question, level
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
lines = question_text.split("\n")
|
| 59 |
correct_option = None
|
| 60 |
for line in lines:
|
| 61 |
if "*" in line:
|
| 62 |
-
correct_option = line.
|
| 63 |
break
|
| 64 |
|
| 65 |
correct = (user_answer.strip().upper() == correct_option)
|
| 66 |
-
new_level, new_text, new_question = adaptive_test(
|
| 67 |
-
|
| 68 |
feedback = "✅ Correct!" if correct else "❌ Incorrect."
|
| 69 |
if new_level == prev_level:
|
| 70 |
-
feedback += "\n\n🎯 Your estimated reading level is
|
| 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("# 📘
|
| 77 |
|
| 78 |
-
|
| 79 |
-
start_btn = gr.Button("▶️ テストを開始")
|
| 80 |
|
| 81 |
-
level_display = gr.Textbox(label="
|
| 82 |
-
text_display = gr.Textbox(label="
|
| 83 |
-
question_display = gr.Textbox(label="
|
| 84 |
|
| 85 |
-
user_answer = gr.Textbox(label="
|
| 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=[
|
| 94 |
outputs=[level_display, text_display, question_display, hidden_level]
|
| 95 |
)
|
| 96 |
|
| 97 |
submit_btn.click(
|
| 98 |
fn=next_step,
|
| 99 |
-
inputs=[
|
| 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 (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:
|
| 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 (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 |
|