Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# 查詢特定日期的錯題歷史
|
| 2 |
def get_errors_by_date(date):
|
| 3 |
if date in error_history:
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import PyPDF2
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
# **更新後的主題選項**
|
| 10 |
+
topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"]
|
| 11 |
+
difficulties = ["簡單", "中等", "困難"]
|
| 12 |
+
|
| 13 |
+
# 學習者錯誤統計(歷史紀錄)
|
| 14 |
+
user_errors = {}
|
| 15 |
+
error_history = {}
|
| 16 |
+
|
| 17 |
+
# **開發者預設教材 PDF 檔案**
|
| 18 |
+
DEFAULT_PDF_PATH = "教材.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:
|
| 26 |
+
text += page.extract_text() + "\n"
|
| 27 |
+
return text
|
| 28 |
+
|
| 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 |
+
|
| 35 |
+
response = openai.ChatCompletion.create(
|
| 36 |
+
model="gpt-4",
|
| 37 |
+
messages=[{"role": "system", "content": "你是一位教育專家,請根據教材內容提供符合主題的問題。"},
|
| 38 |
+
{"role": "user", "content": prompt}]
|
| 39 |
+
)
|
| 40 |
+
return response['choices'][0]['message']['content']
|
| 41 |
+
|
| 42 |
+
# AI 根據 PDF 教材分析回答,並記錄錯題與日期
|
| 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 |
+
# 使用 AI 來分析回答(比對 PDF 教材)
|
| 50 |
+
prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與詳細講解:\n{pdf_text}\n\n學生回答:'{user_input}'"
|
| 51 |
+
|
| 52 |
+
response = openai.ChatCompletion.create(
|
| 53 |
+
model="gpt-4",
|
| 54 |
+
messages=[{"role": "system", "content": "你是一位教育專家,請根據教材內容分析學生的回答,並提供正確答案與講解。"},
|
| 55 |
+
{"role": "user", "content": prompt}]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
feedback = response['choices'][0]['message']['content']
|
| 59 |
+
|
| 60 |
+
# 記錄錯題與日期
|
| 61 |
+
if "❌" in feedback:
|
| 62 |
+
user_errors[topic] = user_errors.get(topic, 0) + 1
|
| 63 |
+
if current_date not in error_history:
|
| 64 |
+
error_history[current_date] = []
|
| 65 |
+
error_history[current_date].append({"題目": topic, "回答": user_input, "AI 分析": feedback})
|
| 66 |
+
|
| 67 |
+
return feedback
|
| 68 |
+
|
| 69 |
# 查詢特定日期的錯題歷史
|
| 70 |
def get_errors_by_date(date):
|
| 71 |
if date in error_history:
|