Toya0421 commited on
Commit
4cd5d7d
·
verified ·
1 Parent(s): 2ae392c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -44,35 +44,38 @@ materials = {
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)
68
- return q
69
- except Exception as e:
70
- print("⚠️ Error generating question:", e)
71
- return {
72
- "question": "Error generating question.",
73
- "choices": ["A) ---", "B) ---", "C) ---", "D) ---"],
74
- "answer": "A"
75
- }
76
 
77
  # ===== テスト初期化 =====
78
  def start_test():
 
44
  # ===== 問題生成 =====
45
  def generate_question(text):
46
  prompt = f"""
47
+ Read the following passage and generate ONE multiple-choice question with 4 options (A–D).
48
+ Clearly mark the correct answer in the format below.
49
+ Return ONLY in this format:
50
+
51
+ Q: <question text>
52
+ A. <option>
53
+ B. <option>
54
+ C. <option>
55
+ D. <option>
56
+ Correct: <A/B/C/D>
57
+
58
  Passage:
59
  {text}
60
  """
61
 
62
+ response = client.chat.completions.create(
63
+ model="google/gemma-3-27b-it:free",
64
+ messages=[{"role": "user", "content": prompt}],
65
+ max_tokens=400,
66
+ temperature=0.7,
67
+ )
68
+ full_output = response.choices[0].message.content.strip()
69
+
70
+ # --- 正解を抽出 ---
71
+ match = re.search(r"Correct:\s*([A-D])", full_output, re.IGNORECASE)
72
+ correct_option = match.group(1).upper() if match else None
73
+
74
+ # --- 「Correct:」行を削除して受験者には非表示にする ---
75
+ visible_question = re.sub(r"(?i)Correct:\s*[A-D]", "", full_output).strip()
76
+
77
+ return visible_question, correct_option
78
+
79
 
80
  # ===== テスト初期化 =====
81
  def start_test():