Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import openai
|
| 3 |
import PyPDF2
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
|
@@ -12,6 +13,9 @@ difficulties = ["簡單", "中等", "困難"]
|
|
| 12 |
# 學習者錯誤統計(歷史紀錄)
|
| 13 |
user_errors = {}
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
# **開發者預設教材 PDF 檔案**
|
| 16 |
DEFAULT_PDF_PATH = "教材.pdf"
|
| 17 |
|
|
@@ -37,9 +41,12 @@ def generate_question(topic, difficulty):
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
| 40 |
-
# AI
|
| 41 |
def analyze_answer(user_input, topic):
|
| 42 |
-
global user_errors
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# 使用 AI 來分析回答
|
| 45 |
prompt = f"學生回答:'{user_input}'\n\n請分析學生的回答是否正確,並提供詳細講解與建議。"
|
|
@@ -52,9 +59,12 @@ def analyze_answer(user_input, topic):
|
|
| 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 |
|
|
@@ -68,6 +78,13 @@ def get_weaknesses():
|
|
| 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("# 教師檢定智慧陪讀家教 🚀")
|
|
@@ -90,4 +107,10 @@ with gr.Blocks() as demo:
|
|
| 90 |
weakness_btn = gr.Button("查看過去錯誤主題")
|
| 91 |
weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
demo.launch()
|
|
|
|
| 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 |
|
|
|
|
| 13 |
# 學習者錯誤統計(歷史紀錄)
|
| 14 |
user_errors = {}
|
| 15 |
|
| 16 |
+
# 存儲「錯題歷史」的字典
|
| 17 |
+
error_history = {}
|
| 18 |
+
|
| 19 |
# **開發者預設教材 PDF 檔案**
|
| 20 |
DEFAULT_PDF_PATH = "教材.pdf"
|
| 21 |
|
|
|
|
| 41 |
)
|
| 42 |
return response['choices'][0]['message']['content']
|
| 43 |
|
| 44 |
+
# AI 判斷對錯並提供講解,並記錄錯題與日期
|
| 45 |
def analyze_answer(user_input, topic):
|
| 46 |
+
global user_errors, error_history
|
| 47 |
+
|
| 48 |
+
# 取得當前日期
|
| 49 |
+
current_date = datetime.today().strftime("%Y-%m-%d")
|
| 50 |
|
| 51 |
# 使用 AI 來分析回答
|
| 52 |
prompt = f"學生回答:'{user_input}'\n\n請分析學生的回答是否正確,並提供詳細講解與建議。"
|
|
|
|
| 59 |
|
| 60 |
feedback = response['choices'][0]['message']['content']
|
| 61 |
|
| 62 |
+
# 記錄錯誤主題與日期
|
| 63 |
if "❌" in feedback:
|
| 64 |
user_errors[topic] = user_errors.get(topic, 0) + 1
|
| 65 |
+
if current_date not in error_history:
|
| 66 |
+
error_history[current_date] = []
|
| 67 |
+
error_history[current_date].append({"題目": topic, "回答": user_input, "AI 分析": feedback})
|
| 68 |
|
| 69 |
return feedback
|
| 70 |
|
|
|
|
| 78 |
history_text = "\n".join([f"{k}: {v} 次錯誤" for k, v in sorted_weaknesses])
|
| 79 |
return f"📌 **你的弱點領域**:\n{history_text}"
|
| 80 |
|
| 81 |
+
# 查詢特定日期的錯題歷史
|
| 82 |
+
def get_errors_by_date(date):
|
| 83 |
+
if date in error_history:
|
| 84 |
+
errors = error_history[date]
|
| 85 |
+
return "\n".join([f"🔹 題目: {e['題目']}\n📝 回答: {e['回答']}\n📖 AI 分析: {e['AI 分析']}" for e in errors])
|
| 86 |
+
return "❌ 該日期沒有錯題紀錄"
|
| 87 |
+
|
| 88 |
# 設定 Gradio 介面
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
|
|
|
|
| 107 |
weakness_btn = gr.Button("查看過去錯誤主題")
|
| 108 |
weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
|
| 109 |
|
| 110 |
+
# 新增「錯題日期選擇」功能
|
| 111 |
+
date_input = gr.Textbox(label="輸入日期(YYYY-MM-DD)")
|
| 112 |
+
error_history_output = gr.Textbox(label="當日錯題紀錄")
|
| 113 |
+
search_errors_btn = gr.Button("查看該日期錯題")
|
| 114 |
+
search_errors_btn.click(get_errors_by_date, inputs=date_input, outputs=error_history_output)
|
| 115 |
+
|
| 116 |
demo.launch()
|