Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 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 |
-
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:
|
|
@@ -69,4 +22,10 @@ with gr.Blocks() as demo:
|
|
| 69 |
analyze_btn = gr.Button("分析回答")
|
| 70 |
analyze_btn.click(analyze_answer, inputs=[user_answer, topic_input], outputs=analysis_result)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
demo.launch()
|
|
|
|
| 1 |
+
# 查詢特定日期的錯題歷史
|
| 2 |
+
def get_errors_by_date(date):
|
| 3 |
+
if date in error_history:
|
| 4 |
+
errors = error_history[date]
|
| 5 |
+
return "\n".join([f"🔹 題目: {e['題目']}\n📝 回答: {e['回答']}\n📖 AI 分析: {e['AI 分析']}" for e in errors])
|
| 6 |
+
return "❌ 該日期沒有錯題紀錄"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# 設定 Gradio 介面
|
| 9 |
with gr.Blocks() as demo:
|
|
|
|
| 22 |
analyze_btn = gr.Button("分析回答")
|
| 23 |
analyze_btn.click(analyze_answer, inputs=[user_answer, topic_input], outputs=analysis_result)
|
| 24 |
|
| 25 |
+
# 新增「錯題日期選擇」功能
|
| 26 |
+
date_input = gr.Textbox(label="輸入日期(YYYY-MM-DD)")
|
| 27 |
+
error_history_output = gr.Textbox(label="當日錯題紀錄")
|
| 28 |
+
search_errors_btn = gr.Button("查看該日期錯題")
|
| 29 |
+
search_errors_btn.click(get_errors_by_date, inputs=date_input, outputs=error_history_output)
|
| 30 |
+
|
| 31 |
demo.launch()
|