iamSammi commited on
Commit
63b16a0
·
verified ·
1 Parent(s): fb0bb69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -45
app.py CHANGED
@@ -12,7 +12,7 @@ difficulties = ["簡單", "中等", "困難"]
12
 
13
  user_errors = {}
14
  error_history = {}
15
- reference_answers = {} # 儲存參考答案
16
 
17
  DEFAULT_PDF_PATH = "教材.pdf"
18
 
@@ -43,15 +43,13 @@ def generate_question(topic, difficulty):
43
  ]
44
  )
45
  question = response["choices"][0]["message"]["content"]
46
-
47
  # 儲存參考答案
48
- reference_answers[topic] = question # 假設問題即為參考答案根據實際情況調整
49
-
50
- return question # 返回生成的問題
51
  except Exception as e:
52
  return f"⚠️ 發生錯誤:{e}"
53
 
54
- def analyze_answer(user_input, topic):
55
  global user_errors, error_history
56
  if not user_input.strip():
57
  return "⚠️ 請輸入回答。"
@@ -59,6 +57,7 @@ def analyze_answer(user_input, topic):
59
  return "⚠️ 教材內容未載入,請確認 PDF。"
60
 
61
  current_date = datetime.today().strftime("%Y-%m-%d")
 
62
  prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與講解:\n{pdf_text}\n\n學生回答:'{user_input}'"
63
 
64
  try:
@@ -73,17 +72,15 @@ def analyze_answer(user_input, topic):
73
  except Exception as e:
74
  return f"⚠️ 發生錯誤:{e}"
75
 
76
- # 檢查用戶的回答是否正確
77
- correct_answer = reference_answers.get(topic, "無參考答案")
78
  if "❌" in feedback or "錯" in feedback:
79
- user_errors[topic] = user_errors.get(topic, 0) + 1
80
  error_history.setdefault(current_date, []).append({
81
- "題目": topic,
82
  "回答": user_input,
83
- "AI 分析": feedback,
84
- "參考答案": correct_answer # 儲存參考答案
85
  })
86
- return f"{feedback}\n\n參考答案:{correct_answer}" # 這裡可以選擇不返回參考答案
87
 
88
  def get_errors_by_date_safe(date_str):
89
  try:
@@ -93,19 +90,10 @@ def get_errors_by_date_safe(date_str):
93
  errors = error_history.get(date_str)
94
  if not errors:
95
  return "✅ 該日無錯題紀錄。"
96
- return "\n\n".join([f"🔹 題目: {e['題目']}\n📝 回答: {e['回答']}\n📖 AI 分析: {e['AI 分析']}\n📜 參考答案: {e['參考答案']}" for e in errors])
97
 
98
- def clear_all():
99
- # 清空問題、回答、參考答案與錯題歷史,但保留PDF文本
100
- global reference_answers, user_errors, error_history
101
- reference_answers = {}
102
- user_errors = {}
103
- error_history.clear()
104
- return "", "", "", get_errors_text()
105
-
106
- def clear_question_and_answer():
107
- # 清空生成的問題和用戶的回答
108
- return "", ""
109
 
110
  with gr.Blocks() as demo:
111
  gr.Markdown("# 👨‍🏫 教師檢定智慧陪讀家教 🚀")
@@ -113,29 +101,28 @@ with gr.Blocks() as demo:
113
  with gr.Row():
114
  topic_input = gr.Dropdown(choices=topics, label="選擇複習主題")
115
  difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級")
116
- with gr.Row():
117
- ask_btn = gr.Button("🎯 生成問題")
118
- clear_question_btn = gr.Button("❌ 清空問題")
119
  question_output = gr.Textbox(label="AI 生成的問題", lines=4)
120
-
 
 
 
 
121
  user_answer = gr.Textbox(label="你的回答", lines=3)
122
  analyze_btn = gr.Button("📊 分析回答")
123
  analysis_result = gr.Textbox(label="AI 分析與講解", lines=5)
124
-
 
 
 
125
  gr.Markdown("---")
126
- gr.Markdown("📋 錯題自動記錄")
127
- error_history_output = gr.Textbox(label="錯題紀錄", lines=10)
128
-
129
- # 生成問按鈕事件
130
- ask_btn.click(fn=lambda t, d: generate_question(t, d),
131
- inputs=[topic_input, difficulty_input],
132
- outputs=[question_output])
133
- # 清空問題按鈕事件
134
- clear_question_btn.click(fn=clear_question_and_answer,
135
- inputs=[],
136
- outputs=[question_output, user_answer])
137
- # 分析回答按鈕事件
138
- analyze_btn.click(fn=analyze_answer,
139
- inputs=[user_answer, topic_input],
140
- outputs=[analysis_result, error_history_output])
141
  demo.launch()
 
12
 
13
  user_errors = {}
14
  error_history = {}
15
+ reference_answers = {}
16
 
17
  DEFAULT_PDF_PATH = "教材.pdf"
18
 
 
43
  ]
44
  )
45
  question = response["choices"][0]["message"]["content"]
 
46
  # 儲存參考答案
47
+ reference_answers[question] = "這是參考答案。" # 這裡可以根據實際情況獲取正確答案
48
+ return question
 
49
  except Exception as e:
50
  return f"⚠️ 發生錯誤:{e}"
51
 
52
+ def analyze_answer(user_input, question):
53
  global user_errors, error_history
54
  if not user_input.strip():
55
  return "⚠️ 請輸入回答。"
 
57
  return "⚠️ 教材內容未載入,請確認 PDF。"
58
 
59
  current_date = datetime.today().strftime("%Y-%m-%d")
60
+ correct_answer = reference_answers.get(question, "無法獲取正確答案")
61
  prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與講解:\n{pdf_text}\n\n學生回答:'{user_input}'"
62
 
63
  try:
 
72
  except Exception as e:
73
  return f"⚠️ 發生錯誤:{e}"
74
 
 
 
75
  if "❌" in feedback or "錯" in feedback:
76
+ user_errors[question] = user_errors.get(question, 0) + 1
77
  error_history.setdefault(current_date, []).append({
78
+ "題目": question,
79
  "回答": user_input,
80
+ "正確答案": correct_answer,
81
+ "AI 分析": feedback
82
  })
83
+ return feedback
84
 
85
  def get_errors_by_date_safe(date_str):
86
  try:
 
90
  errors = error_history.get(date_str)
91
  if not errors:
92
  return "✅ 該日無錯題紀錄。"
93
+ return "\n\n".join([f"🔹 題目: {e['題目']}\n📝 回答: {e['回答']}\n📖 正確答案: {e['正確答案']}\n📖 AI 分析: {e['AI 分析']}" for e in errors])
94
 
95
+ def clear_fields():
96
+ return "", "", "", ""
 
 
 
 
 
 
 
 
 
97
 
98
  with gr.Blocks() as demo:
99
  gr.Markdown("# 👨‍🏫 教師檢定智慧陪讀家教 🚀")
 
101
  with gr.Row():
102
  topic_input = gr.Dropdown(choices=topics, label="選擇複習主題")
103
  difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級")
104
+ ask_btn = gr.Button("🎯 生成問題")
105
+ clear_btn = gr.Button("🧹 清空")
 
106
  question_output = gr.Textbox(label="AI 生成的問題", lines=4)
107
+ ask_btn.click(fn=lambda t, d: generate_question(t, d),
108
+ inputs=[topic_input, difficulty_input],
109
+ outputs=question_output)
110
+ clear_btn.click(fn=clear_fields, outputs=[question_output, user_answer, analysis_result])
111
+
112
  user_answer = gr.Textbox(label="你的回答", lines=3)
113
  analyze_btn = gr.Button("📊 分析回答")
114
  analysis_result = gr.Textbox(label="AI 分析與講解", lines=5)
115
+ analyze_btn.click(fn=lambda ans, q: analyze_answer(ans, q),
116
+ inputs=[user_answer, question_output],
117
+ outputs=analysis_result)
118
+
119
  gr.Markdown("---")
120
+ gr.Markdown("📅 查詢錯題錄")
121
+ date_input = gr.Textbox(label="輸入日期(YYYY-MM-DD)")
122
+ search_errors_btn = gr.Button("🔍 查看該日期錯題")
123
+ error_history_output = gr.Textbox(label="錯紀錄", lines=5)
124
+ search_errors_btn.click(fn=get_errors_by_date_safe,
125
+ inputs=date_input,
126
+ outputs=error_history_output)
127
+
 
 
 
 
 
 
 
128
  demo.launch()