Update app.py
Browse files
app.py
CHANGED
|
@@ -2,139 +2,93 @@ import gradio as gr
|
|
| 2 |
import openai
|
| 3 |
import PyPDF2
|
| 4 |
import os
|
| 5 |
-
from datetime import datetime
|
| 6 |
|
| 7 |
-
|
| 8 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
|
|
|
| 10 |
topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"]
|
| 11 |
difficulties = ["簡單", "中等", "困難"]
|
| 12 |
|
|
|
|
| 13 |
user_errors = {}
|
| 14 |
-
error_history = {}
|
| 15 |
-
reference_answers = {}
|
| 16 |
|
|
|
|
| 17 |
DEFAULT_PDF_PATH = "教材.pdf"
|
| 18 |
|
|
|
|
| 19 |
def extract_text_from_pdf():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return "\n".join([page.extract_text() or "" for page in reader.pages])
|
| 27 |
-
except Exception as e:
|
| 28 |
-
print(f"[錯誤] PDF 載入失敗:{e}")
|
| 29 |
-
return ""
|
| 30 |
-
|
| 31 |
-
pdf_text = extract_text_from_pdf()
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
def generate_question(topic, difficulty):
|
| 34 |
-
|
| 35 |
-
return "⚠️ 無法載入教材內容,請確認 PDF 是否存在。"
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
model="gpt-4o-mini-2024-07-18",
|
| 43 |
-
messages=[
|
| 44 |
-
{"role": "system", "content": "你是一位教育專家,請根據教材內容設計題目。(不需要包含解析)"},
|
| 45 |
-
{"role": "user", "content": prompt}
|
| 46 |
-
]
|
| 47 |
-
)
|
| 48 |
-
question = response["choices"][0]["message"]["content"]
|
| 49 |
-
return question.strip() # 返回生成的問題
|
| 50 |
-
except Exception as e:
|
| 51 |
-
return f"⚠️ 發生錯誤:{e}"
|
| 52 |
-
|
| 53 |
-
def save_answer(question):
|
| 54 |
-
if not pdf_text.strip():
|
| 55 |
-
return "⚠️ 教材內容未載入,請確認 PDF。"
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
messages=[
|
| 63 |
-
{"role": "system", "content": "你是一位教育專家,請根據教材內容提供問題的正確答案。"},
|
| 64 |
-
{"role": "user", "content": answer_prompt}
|
| 65 |
-
]
|
| 66 |
-
)
|
| 67 |
-
correct_answer = answer_response["choices"][0]["message"]["content"]
|
| 68 |
-
reference_answers[question] = correct_answer.strip() # 儲存正確答案
|
| 69 |
-
except Exception as e:
|
| 70 |
-
return f"⚠️ 發生錯誤:{e}"
|
| 71 |
-
|
| 72 |
-
def save_error(question, user_input, correct_answer, feedback):
|
| 73 |
-
current_date = datetime.today().strftime("%Y-%m-%d")
|
| 74 |
-
error_history.setdefault(current_date, []).append({
|
| 75 |
-
"題目": question,
|
| 76 |
-
"回答": user_input,
|
| 77 |
-
"正確答案": correct_answer,
|
| 78 |
-
"AI 分析": feedback
|
| 79 |
-
})
|
| 80 |
-
|
| 81 |
-
def analyze_answer(user_input, question):
|
| 82 |
-
global user_errors
|
| 83 |
-
if not user_input.strip():
|
| 84 |
-
return "⚠️ 請輸入回答。"
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
try:
|
| 94 |
-
response = openai.ChatCompletion.create(
|
| 95 |
-
model="gpt-4o-mini-2024-07-18",
|
| 96 |
-
messages=[
|
| 97 |
-
{"role": "system", "content": "你是一位教育專家,請根據教材內容分析學生回答。"},
|
| 98 |
-
{"role": "user", "content": prompt}
|
| 99 |
-
]
|
| 100 |
-
)
|
| 101 |
-
feedback = response["choices"][0]["message"]["content"]
|
| 102 |
-
except Exception as e:
|
| 103 |
-
return f"⚠️ 發生錯誤:{e}"
|
| 104 |
-
|
| 105 |
-
if "❌" in feedback or "錯" in feedback:
|
| 106 |
-
user_errors[question] = user_errors.get(question, 0) + 1
|
| 107 |
-
save_error(question, user_input, correct_answer, feedback) # 儲存錯題
|
| 108 |
-
# 自動輸出錯題記錄
|
| 109 |
-
error_output = f"🔹 題目: {question}\n📝 回答: {user_input}\n📖 正確答案: {correct_answer}\n📖 AI 分析: {feedback}"
|
| 110 |
-
return feedback, error_output
|
| 111 |
-
return feedback, ""
|
| 112 |
-
|
| 113 |
-
def clear_fields():
|
| 114 |
-
return "", "", "" # 清空問題、回答和分析結果,但不清空錯題紀錄
|
| 115 |
|
|
|
|
| 116 |
with gr.Blocks() as demo:
|
| 117 |
-
gr.Markdown("#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
ask_btn.click(
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
analyze_btn = gr.Button("
|
| 131 |
-
|
| 132 |
-
error_history_output = gr.Textbox(label="錯題紀錄", lines=5)
|
| 133 |
-
|
| 134 |
-
analyze_btn.click(fn=lambda ans, q: analyze_answer(ans, q),
|
| 135 |
-
inputs=[user_answer, question_output],
|
| 136 |
-
outputs=[analysis_result, error_history_output])
|
| 137 |
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
demo.launch()
|
|
|
|
| 2 |
import openai
|
| 3 |
import PyPDF2
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 7 |
|
| 8 |
+
# **更新後的主題選項**
|
| 9 |
topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"]
|
| 10 |
difficulties = ["簡單", "中等", "困難"]
|
| 11 |
|
| 12 |
+
# 學習者錯誤統計(歷史紀錄)
|
| 13 |
user_errors = {}
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# **開發者預設教材 PDF 檔案**
|
| 16 |
DEFAULT_PDF_PATH = "教材.pdf"
|
| 17 |
|
| 18 |
+
# 解析 PDF 並擷取文本(使用開發者預設的教材)
|
| 19 |
def extract_text_from_pdf():
|
| 20 |
+
with open(DEFAULT_PDF_PATH, "rb") as pdf_file: # 修正此處為 "rb"
|
| 21 |
+
reader = PyPDF2.PdfReader(pdf_file)
|
| 22 |
+
text = ""
|
| 23 |
+
for page in reader.pages:
|
| 24 |
+
text += page.extract_text() + "\n"
|
| 25 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
pdf_text = extract_text_from_pdf() # 讀取教材
|
| 28 |
+
|
| 29 |
+
# AI 生成問題函數(基於預設教材)
|
| 30 |
def generate_question(topic, difficulty):
|
| 31 |
+
prompt = f"請根據以下教育學教材內容,設計一個屬於'{topic}'主題、'{difficulty}'難度的考題:\n{pdf_text}"
|
|
|
|
| 32 |
|
| 33 |
+
response = openai.ChatCompletion.create(
|
| 34 |
+
model="gpt-4",
|
| 35 |
+
messages=[{"role": "system", "content": "你是一位教育專家,請根據教材內容提供符合主題的問題。"},
|
| 36 |
+
{"role": "user", "content": prompt}]
|
| 37 |
+
)
|
| 38 |
+
return response['choices'][0]['message']['content']
|
| 39 |
+
|
| 40 |
+
# AI 判斷對錯並提供正確答案與講解
|
| 41 |
+
def analyze_answer(user_input, correct_answer, topic):
|
| 42 |
+
global user_errors
|
| 43 |
|
| 44 |
+
# 使用 AI 來分析回答
|
| 45 |
+
prompt = f"學生回答:'{user_input}'\n\n正確答案:'{correct_answer}'\n\n請分析學生的回答是否正確,並提供正確答案與詳細講解。"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
response = openai.ChatCompletion.create(
|
| 48 |
+
model="gpt-4",
|
| 49 |
+
messages=[{"role": "system", "content": "你是一位教育專家,請評估學生的回答,判斷是否正確,並提供正確答案與詳細講解。"},
|
| 50 |
+
{"role": "user", "content": prompt}]
|
| 51 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
feedback = response['choices'][0]['message']['content']
|
| 54 |
|
| 55 |
+
# 記錄錯誤主題(長期紀錄)
|
| 56 |
+
if "❌" in feedback:
|
| 57 |
+
user_errors[topic] = user_errors.get(topic, 0) + 1
|
| 58 |
+
|
| 59 |
+
return feedback
|
| 60 |
+
|
| 61 |
+
# 顯示弱點歷史紀錄
|
| 62 |
+
def get_weaknesses():
|
| 63 |
+
if not user_errors:
|
| 64 |
+
return "🎯 目前沒有明顯弱點,繼續保持!"
|
| 65 |
+
|
| 66 |
+
sorted_weaknesses = sorted(user_errors.items(), key=lambda x: x[1], reverse=True)
|
| 67 |
|
| 68 |
+
history_text = "\n".join([f"{k}: {v} 次錯誤" for k, v in sorted_weaknesses])
|
| 69 |
+
return f"📌 **你的弱點領域**:\n{history_text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# 設定 Gradio 介面
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
+
gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
|
| 74 |
+
|
| 75 |
+
topic_input = gr.Dropdown(choices=topics, label="選擇複習主題")
|
| 76 |
+
difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級")
|
| 77 |
+
question_output = gr.Textbox(label="AI 生成的問題")
|
| 78 |
+
correct_answer_output = gr.Textbox(label="正確答案")
|
| 79 |
+
|
| 80 |
+
ask_btn = gr.Button("生成問題")
|
| 81 |
+
ask_btn.click(generate_question, inputs=[topic_input, difficulty_input], outputs=question_output)
|
| 82 |
+
|
| 83 |
+
user_answer = gr.Textbox(label="你的回答")
|
| 84 |
+
analysis_result = gr.Textbox(label="AI 分析與講解")
|
| 85 |
+
|
| 86 |
+
analyze_btn = gr.Button("分析回答")
|
| 87 |
+
analyze_btn.click(analyze_answer, inputs=[user_answer, correct_answer_output, topic_input], outputs=analysis_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# 新增弱點歷史紀錄功能
|
| 90 |
+
weaknesses_output = gr.Textbox(label="弱點歷史紀錄")
|
| 91 |
+
weakness_btn = gr.Button("查看過去錯誤主題")
|
| 92 |
+
weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
|
| 93 |
|
| 94 |
demo.launch()
|