iamSammi commited on
Commit
e8a6262
·
verified ·
1 Parent(s): ed295fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -49
app.py CHANGED
@@ -12,8 +12,6 @@ difficulties = ["簡單", "中等", "困難"]
12
 
13
  # 學習者錯誤統計(歷史紀錄)
14
  user_errors = {}
15
-
16
- # 存儲「錯題歷史」的字典
17
  error_history = {}
18
 
19
  # **開發者預設教材 PDF 檔案**
@@ -21,7 +19,7 @@ DEFAULT_PDF_PATH = "教材.pdf"
21
 
22
  # 解析 PDF 並擷取文本(使用開發者預設的教材)
23
  def extract_text_from_pdf():
24
- with open(DEFAULT_PDF_PATH, "rb") as pdf_file: # 修正此處為 "rb"
25
  reader = PyPDF2.PdfReader(pdf_file)
26
  text = ""
27
  for page in reader.pages:
@@ -31,7 +29,6 @@ def extract_text_from_pdf():
31
  pdf_text = extract_text_from_pdf() # 讀取教材
32
 
33
  # AI 生成問題函數(基於預設教材)
34
- print(pdf_text[:500]) # 顯示前 500 個字元,檢查是否成功讀取
35
  def generate_question(topic, difficulty):
36
  prompt = f"請根據以下教育學教材內容,設計一個屬於'{topic}'主題、'{difficulty}'難度的考題:\n{pdf_text}"
37
 
@@ -42,50 +39,19 @@ def generate_question(topic, difficulty):
42
  )
43
  return response['choices'][0]['message']['content']
44
 
45
- # AI 判斷對錯並提供講解,並記錄錯題與日期
46
  def analyze_answer(user_input, topic):
47
- global user_errors, error_history
48
-
49
- # 取得當前日期
50
- current_date = datetime.today().strftime("%Y-%m-%d")
51
-
52
- # 使用 AI 來分析回答
53
- prompt = f"學生回答:'{user_input}'\n\n請分析學生的回答是否正確,並提供詳細講解與建議。"
54
 
55
  response = openai.ChatCompletion.create(
56
  model="gpt-4",
57
- messages=[{"role": "system", "content": "你是一位教育專家,請評估學生的回答,並提供詳細講解。"},
58
  {"role": "user", "content": prompt}]
59
  )
60
 
61
  feedback = response['choices'][0]['message']['content']
62
-
63
- # 記錄錯誤主題與日期
64
- if "❌" in feedback:
65
- user_errors[topic] = user_errors.get(topic, 0) + 1
66
- if current_date not in error_history:
67
- error_history[current_date] = []
68
- error_history[current_date].append({"題目": topic, "回答": user_input, "AI 分析": feedback})
69
-
70
  return feedback
71
 
72
- # 顯示弱點歷史紀錄
73
- def get_weaknesses():
74
- if not user_errors:
75
- return "🎯 目前沒有明顯弱點,繼續保持!"
76
-
77
- sorted_weaknesses = sorted(user_errors.items(), key=lambda x: x[1], reverse=True)
78
-
79
- history_text = "\n".join([f"{k}: {v} 次錯誤" for k, v in sorted_weaknesses])
80
- return f"📌 **你的弱點領域**:\n{history_text}"
81
-
82
- # 查詢特定日期的錯題歷史
83
- def get_errors_by_date(date):
84
- if date in error_history:
85
- errors = error_history[date]
86
- return "\n".join([f"🔹 題目: {e['題目']}\n📝 回答: {e['回答']}\n📖 AI 分析: {e['AI 分析']}" for e in errors])
87
- return "❌ 該日期沒有錯題紀錄"
88
-
89
  # 設定 Gradio 介面
90
  with gr.Blocks() as demo:
91
  gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
@@ -103,15 +69,4 @@ with gr.Blocks() as demo:
103
  analyze_btn = gr.Button("分析回答")
104
  analyze_btn.click(analyze_answer, inputs=[user_answer, topic_input], outputs=analysis_result)
105
 
106
- # 新增弱點歷史紀錄功能
107
- weaknesses_output = gr.Textbox(label="弱點歷史紀錄")
108
- weakness_btn = gr.Button("查看過去錯誤主題")
109
- weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
110
-
111
- # 新增「錯題日期選擇」功能
112
- date_input = gr.Textbox(label="輸入日期(YYYY-MM-DD)")
113
- error_history_output = gr.Textbox(label="當日錯題紀錄")
114
- search_errors_btn = gr.Button("查看該日期錯題")
115
- search_errors_btn.click(get_errors_by_date, inputs=date_input, outputs=error_history_output)
116
-
117
  demo.launch()
 
12
 
13
  # 學習者錯誤統計(歷史紀錄)
14
  user_errors = {}
 
 
15
  error_history = {}
16
 
17
  # **開發者預設教材 PDF 檔案**
 
19
 
20
  # 解析 PDF 並擷取文本(使用開發者預設的教材)
21
  def extract_text_from_pdf():
22
+ with open(DEFAULT_PDF_PATH, "rb") as pdf_file:
23
  reader = PyPDF2.PdfReader(pdf_file)
24
  text = ""
25
  for page in reader.pages:
 
29
  pdf_text = extract_text_from_pdf() # 讀取教材
30
 
31
  # AI 生成問題函數(基於預設教材)
 
32
  def generate_question(topic, difficulty):
33
  prompt = f"請根據以下教育學教材內容,設計一個屬於'{topic}'主題、'{difficulty}'難度的考題:\n{pdf_text}"
34
 
 
39
  )
40
  return response['choices'][0]['message']['content']
41
 
42
+ # AI 根據 PDF 教材分析回答
43
  def analyze_answer(user_input, topic):
44
+ prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與詳細講解:\n{pdf_text}\n\n學生回答:'{user_input}'"
 
 
 
 
 
 
45
 
46
  response = openai.ChatCompletion.create(
47
  model="gpt-4",
48
+ messages=[{"role": "system", "content": "你是一位教育專家,請根據教材內容分析學生的回答,並提供正確答案與講解。"},
49
  {"role": "user", "content": prompt}]
50
  )
51
 
52
  feedback = response['choices'][0]['message']['content']
 
 
 
 
 
 
 
 
53
  return feedback
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  # 設定 Gradio 介面
56
  with gr.Blocks() as demo:
57
  gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
 
69
  analyze_btn = gr.Button("分析回答")
70
  analyze_btn.click(analyze_answer, inputs=[user_answer, topic_input], outputs=analysis_result)
71
 
 
 
 
 
 
 
 
 
 
 
 
72
  demo.launch()