iamSammi commited on
Commit
7628926
·
verified ·
1 Parent(s): 5668251

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py CHANGED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ import PyPDF2
4
+
5
+ # 設定教育心理學主題
6
+ topics = ["學習理論", "動機與情意", "發展心理學", "個別差異", "教學與評量", "班級經營與學習環境", "教育心理學研究方法"]
7
+ difficulties = ["簡單", "中等", "困難"]
8
+
9
+ # 學習者錯誤統計
10
+ user_errors = {}
11
+
12
+ # **開發者預設教材 PDF 檔案**
13
+ DEFAULT_PDF_PATH = "教材.pdf" # 確保此檔案存放在專案目錄中
14
+
15
+ # 解析 PDF 並擷取文本(使用開發者預設的教材)
16
+ def extract_text_from_pdf():
17
+ with open(DEFAULT_PDF_PATH, "rb") as pdf_file:
18
+ reader = PyPDF2.PdfReader(pdf_file)
19
+ text = ""
20
+ for page in reader.pages:
21
+ text += page.extract_text() + "\n"
22
+ return text
23
+
24
+ pdf_text = extract_text_from_pdf() # 讀取教材
25
+
26
+ # AI 生成問題函數(基於預設教材)
27
+ def generate_question(topic, difficulty):
28
+ prompt = f"請根據以下教育心理學教材內容,設計一個'{difficulty}'難度的考題:\n{pdf_text}"
29
+
30
+ response = openai.ChatCompletion.create(
31
+ model="gpt-4",
32
+ messages=[{"role": "system", "content": "你是一位教育心理學專家,請根據教材內容生成考題。"},
33
+ {"role": "user", "content": prompt}]
34
+ )
35
+ return response['choices'][0]['message']['content']
36
+
37
+ # AI 語音輸出
38
+ def ai_speak(text):
39
+ response = openai.ChatCompletion.create(
40
+ model="gpt-4",
41
+ messages=[{"role": "system", "content": "請以教師語氣回答"},
42
+ {"role": "user", "content": text}]
43
+ )
44
+ return response['choices'][0]['message']['content']
45
+
46
+ # 分析回答完整性 + 記錄弱點
47
+ def analyze_answer(user_input, correct_answer, topic):
48
+ global user_errors
49
+
50
+ if user_input.strip().lower() == correct_answer.strip().lower():
51
+ return "✅ 正確!"
52
+
53
+ elif user_input in correct_answer:
54
+ feedback = "⚠️ 部分正確,請補充完整"
55
+ else:
56
+ feedback = "❌ 答非所問,請重新思考"
57
+
58
+ # 記錄錯誤主題
59
+ if feedback != "✅ 正確!":
60
+ if topic in user_errors:
61
+ user_errors[topic] += 1
62
+ else:
63
+ user_errors[topic] = 1
64
+
65
+ return feedback
66
+
67
+ # 列出可加強的知識點
68
+ def get_weaknesses():
69
+ if not user_errors:
70
+ return "🎯 目前沒有明顯弱點,繼續保持!"
71
+
72
+ sorted_weaknesses = sorted(user_errors.items(), key=lambda x: x[1], reverse=True)
73
+ return f"📌 你的弱點領域:{', '.join([f'{k} ({v}次錯誤)' for k, v in sorted_weaknesses])}"
74
+
75
+ # 設定 Gradio 介面
76
+ with gr.Blocks() as demo:
77
+ gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
78
+
79
+ topic_input = gr.Dropdown(choices=topics, label="選擇教育心理學主題")
80
+ difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級")
81
+ question_output = gr.Textbox(label="AI 生成的問題")
82
+
83
+ ask_btn = gr.Button("生成問題")
84
+ ask_btn.click(generate_question, inputs=[topic_input, difficulty_input], outputs=question_output)
85
+
86
+ user_answer = gr.Textbox(label="你的回答")
87
+ analysis_result = gr.Textbox(label="分析結果")
88
+
89
+ analyze_btn = gr.Button("分析回答")
90
+ analyze_btn.click(analyze_answer, inputs=[user_answer, question_output, topic_input], outputs=analysis_result)
91
+
92
+ weaknesses_output = gr.Textbox(label="智能弱點分析")
93
+ weakness_btn = gr.Button("查看可加強的知識點")
94
+ weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
95
+
96
+ gr.Interface(fn=ai_speak, inputs="text", outputs="text")
97
+
98
+ demo.launch()