Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,96 +4,135 @@ import PyPDF2
|
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"]
|
| 11 |
difficulties = ["簡單", "中等", "困難"]
|
| 12 |
|
| 13 |
-
#
|
| 14 |
user_errors = {}
|
| 15 |
error_history = {}
|
| 16 |
|
| 17 |
-
#
|
| 18 |
DEFAULT_PDF_PATH = "教材.pdf"
|
| 19 |
|
| 20 |
-
#
|
| 21 |
def extract_text_from_pdf():
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def generate_question(topic, difficulty):
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def analyze_answer(user_input, topic):
|
| 44 |
global user_errors, error_history
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
current_date = datetime.today().strftime("%Y-%m-%d")
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
user_errors[topic] = user_errors.get(topic, 0) + 1
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
return feedback
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
def
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
# 設定 Gradio 介面
|
| 77 |
-
with gr.Blocks() as demo:
|
| 78 |
-
gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
date_input = gr.Textbox(label="輸入日期(YYYY-MM-DD)")
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
search_errors_btn.click(
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
demo.launch()
|
|
|
|
| 4 |
import os
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
+
# ✅ 設定 OpenAI API Key(從 Hugging Face Secrets 取得)
|
| 8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
+
# ✅ 主題與難度設定
|
| 11 |
topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"]
|
| 12 |
difficulties = ["簡單", "中等", "困難"]
|
| 13 |
|
| 14 |
+
# ✅ 錯題紀錄初始化
|
| 15 |
user_errors = {}
|
| 16 |
error_history = {}
|
| 17 |
|
| 18 |
+
# ✅ PDF 教材位置
|
| 19 |
DEFAULT_PDF_PATH = "教材.pdf"
|
| 20 |
|
| 21 |
+
# ✅ 擷取 PDF 文字內容
|
| 22 |
def extract_text_from_pdf():
|
| 23 |
+
if not os.path.exists(DEFAULT_PDF_PATH):
|
| 24 |
+
print(f"[錯誤] 教材檔案未找到:{DEFAULT_PDF_PATH}")
|
| 25 |
+
return ""
|
| 26 |
+
try:
|
| 27 |
+
with open(DEFAULT_PDF_PATH, "rb") as pdf_file:
|
| 28 |
+
reader = PyPDF2.PdfReader(pdf_file)
|
| 29 |
+
text = ""
|
| 30 |
+
for page in reader.pages:
|
| 31 |
+
text += page.extract_text() or ""
|
| 32 |
+
return text
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"[錯誤] 讀取 PDF 失敗:{e}")
|
| 35 |
+
return ""
|
| 36 |
+
|
| 37 |
+
pdf_text = extract_text_from_pdf()
|
| 38 |
+
|
| 39 |
+
# ✅ 生成題目
|
| 40 |
def generate_question(topic, difficulty):
|
| 41 |
+
if not pdf_text.strip():
|
| 42 |
+
return "⚠️ 無法載入教材內容,請確認 PDF 是否存在或格式正確。"
|
| 43 |
|
| 44 |
+
prompt = f"請根據以下教育學教材內容,設計一個屬於「{topic}」主題、「{difficulty}」難度的考題:\n\n{pdf_text}"
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
response = openai.ChatCompletion.create(
|
| 48 |
+
model="gpt-4",
|
| 49 |
+
messages=[
|
| 50 |
+
{"role": "system", "content": "你是一位教育專家,請根據教材內容提供符合主題的問題。"},
|
| 51 |
+
{"role": "user", "content": prompt}
|
| 52 |
+
]
|
| 53 |
+
)
|
| 54 |
+
return response['choices'][0]['message']['content']
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"⚠️ AI 生成失敗:{str(e)}"
|
| 57 |
+
|
| 58 |
+
# ✅ 分析學生回答
|
| 59 |
def analyze_answer(user_input, topic):
|
| 60 |
global user_errors, error_history
|
| 61 |
+
|
| 62 |
+
if not user_input.strip():
|
| 63 |
+
return "⚠️ 請先輸入你的回答。"
|
| 64 |
+
if not pdf_text.strip():
|
| 65 |
+
return "⚠️ 無法載入教材內容,請確認 PDF 是否存在或格式正確。"
|
| 66 |
+
|
| 67 |
current_date = datetime.today().strftime("%Y-%m-%d")
|
| 68 |
+
prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與講解:\n{pdf_text}\n\n學生回答:'{user_input}'"
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
response = openai.ChatCompletion.create(
|
| 72 |
+
model="gpt-4",
|
| 73 |
+
messages=[
|
| 74 |
+
{"role": "system", "content": "你是一位教育專家,請根據教材內容分析學生的回答,並提供正確答案與講解。"},
|
| 75 |
+
{"role": "user", "content": prompt}
|
| 76 |
+
]
|
| 77 |
+
)
|
| 78 |
+
feedback = response['choices'][0]['message']['content']
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return f"⚠️ AI 回應失敗:{str(e)}"
|
| 81 |
+
|
| 82 |
+
# 簡單判斷是否為錯誤回答
|
| 83 |
+
if "❌" in feedback or "錯" in feedback:
|
| 84 |
user_errors[topic] = user_errors.get(topic, 0) + 1
|
| 85 |
+
error_history.setdefault(current_date, []).append({
|
| 86 |
+
"題目": topic,
|
| 87 |
+
"回答": user_input,
|
| 88 |
+
"AI 分析": feedback
|
| 89 |
+
})
|
| 90 |
|
| 91 |
return feedback
|
| 92 |
|
| 93 |
+
# ✅ 錯題查詢
|
| 94 |
+
def get_errors_by_date_safe(date_str):
|
| 95 |
+
try:
|
| 96 |
+
datetime.strptime(date_str, "%Y-%m-%d")
|
| 97 |
+
except ValueError:
|
| 98 |
+
return "⚠️ 日期格式錯誤,請使用 YYYY-MM-DD"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
errors = error_history.get(date_str)
|
| 101 |
+
if not errors:
|
| 102 |
+
return "✅ 該日期沒有錯題紀錄"
|
| 103 |
|
| 104 |
+
return "\n\n".join([
|
| 105 |
+
f"🔹 題目: {e['題目']}\n📝 回答: {e['回答']}\n📖 AI 分析: {e['AI 分析']}"
|
| 106 |
+
for e in errors
|
| 107 |
+
])
|
| 108 |
|
| 109 |
+
# ✅ Gradio UI
|
| 110 |
+
with gr.Blocks() as demo:
|
| 111 |
+
gr.Markdown("# 👨🏫 教師檢定智慧陪讀家教 🚀")
|
| 112 |
+
|
| 113 |
+
with gr.Row():
|
| 114 |
+
topic_input = gr.Dropdown(choices=topics, label="選擇複習主題")
|
| 115 |
+
difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級")
|
| 116 |
+
ask_btn = gr.Button("🎯 生成問題")
|
| 117 |
+
question_output = gr.Textbox(label="AI 生成的問題", lines=4)
|
| 118 |
+
ask_btn.click(fn=lambda t, d: generate_question(t, d),
|
| 119 |
+
inputs=[topic_input, difficulty_input],
|
| 120 |
+
outputs=question_output)
|
| 121 |
+
|
| 122 |
+
user_answer = gr.Textbox(label="你的回答", lines=3)
|
| 123 |
+
analyze_btn = gr.Button("📊 分析回答")
|
| 124 |
+
analysis_result = gr.Textbox(label="AI 分析與講解", lines=5)
|
| 125 |
+
analyze_btn.click(fn=lambda ans, topic: analyze_answer(ans, topic),
|
| 126 |
+
inputs=[user_answer, topic_input],
|
| 127 |
+
outputs=analysis_result)
|
| 128 |
+
|
| 129 |
+
gr.Markdown("---")
|
| 130 |
+
gr.Markdown("📅 查詢錯題紀錄")
|
| 131 |
date_input = gr.Textbox(label="輸入日期(YYYY-MM-DD)")
|
| 132 |
+
search_errors_btn = gr.Button("🔍 查看該日期錯題")
|
| 133 |
+
error_history_output = gr.Textbox(label="錯題紀錄", lines=5)
|
| 134 |
+
search_errors_btn.click(fn=get_errors_by_date_safe,
|
| 135 |
+
inputs=date_input,
|
| 136 |
+
outputs=error_history_output)
|
| 137 |
|
| 138 |
+
demo.launch()
|