Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import openai | |
| import PyPDF2 | |
| import os | |
| from datetime import datetime | |
| # ✅ openai 0.28:API 金鑰設定 | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"] | |
| difficulties = ["簡單", "中等", "困難"] | |
| user_errors = {} | |
| error_history = {} | |
| DEFAULT_PDF_PATH = "教材.pdf" | |
| def extract_text_from_pdf(): | |
| if not os.path.exists(DEFAULT_PDF_PATH): | |
| print(f"[錯誤] 教材未找到:{DEFAULT_PDF_PATH}") | |
| return "" | |
| try: | |
| with open(DEFAULT_PDF_PATH, "rb") as f: | |
| reader = PyPDF2.PdfReader(f) | |
| return "\n".join([page.extract_text() or "" for page in reader.pages]) | |
| except Exception as e: | |
| print(f"[錯誤] PDF 載入失敗:{e}") | |
| return "" | |
| pdf_text = extract_text_from_pdf() | |
| def generate_question(topic, difficulty): | |
| if not pdf_text.strip(): | |
| return "⚠️ 無法載入教材內容,請確認 PDF 是否存在。" | |
| prompt = f"請根據以下教育學教材內容,設計一個屬於「{topic}」主題、「{difficulty}」難度的考題:\n\n{pdf_text}" | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o-mini-2024-07-18", | |
| messages=[ | |
| {"role": "system", "content": "你是一位教育專家,請根據教材內容設計問題。"}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| return response["choices"][0]["message"]["content"] | |
| except Exception as e: | |
| return f"⚠️ 發生錯誤:{e}" | |
| def analyze_answer(user_input, topic): | |
| global user_errors, error_history | |
| if not user_input.strip(): | |
| return "⚠️ 請輸入回答。" | |
| if not pdf_text.strip(): | |
| return "⚠️ 教材內容未載入,請確認 PDF。" | |
| current_date = datetime.today().strftime("%Y-%m-%d") | |
| prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與講解:\n{pdf_text}\n\n學生回答:'{user_input}'" | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o-mini-2024-07-18", | |
| messages=[ | |
| {"role": "system", "content": "你是一位教育專家,請根據教材內容分析學生回答。"}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| feedback = response["choices"][0]["message"]["content"] | |
| except Exception as e: | |
| return f"⚠️ 發生錯誤:{e}" | |
| if "❌" in feedback or "錯" in feedback: | |
| user_errors[topic] = user_errors.get(topic, 0) + 1 | |
| error_history.setdefault(current_date, []).append({ | |
| "題目": topic, | |
| "回答": user_input, | |
| "AI 分析": feedback | |
| }) | |
| return feedback | |
| def get_errors_by_date_safe(date_str): | |
| try: | |
| datetime.strptime(date_str, "%Y-%m-%d") | |
| except ValueError: | |
| return "⚠️ 日期格式錯誤,請使用 YYYY-MM-DD。" | |
| errors = error_history.get(date_str) | |
| if not errors: | |
| return "✅ 該日無錯題紀錄。" | |
| return "\n\n".join([f"🔹 題目: {e['題目']}\n📝 回答: {e['回答']}\n📖 AI 分析: {e['AI 分析']}" for e in errors]) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 👨🏫 教師檢定智慧陪讀家教 🚀") | |
| with gr.Row(): | |
| topic_input = gr.Dropdown(choices=topics, label="選擇複習主題") | |
| difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級") | |
| ask_btn = gr.Button("🎯 生成問題") | |
| question_output = gr.Textbox(label="AI 生成的問題", lines=4) | |
| ask_btn.click(fn=lambda t, d: generate_question(t, d), | |
| inputs=[topic_input, difficulty_input], | |
| outputs=question_output) | |
| user_answer = gr.Textbox(label="你的回答", lines=3) | |
| analyze_btn = gr.Button("📊 分析回答") | |
| analysis_result = gr.Textbox(label="AI 分析與講解", lines=5) | |
| analyze_btn.click(fn=lambda ans, topic: analyze_answer(ans, topic), | |
| inputs=[user_answer, topic_input], | |
| outputs=analysis_result) | |
| gr.Markdown("---") | |
| gr.Markdown("📅 查詢錯題紀錄") | |
| date_input = gr.Textbox(label="輸入日期(YYYY-MM-DD)") | |
| search_errors_btn = gr.Button("🔍 查看該日期錯題") | |
| error_history_output = gr.Textbox(label="錯題紀錄", lines=5) | |
| search_errors_btn.click(fn=get_errors_by_date_safe, | |
| inputs=date_input, | |
| outputs=error_history_output) | |
| demo.launch() | |