Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ DEFAULT_PDF_PATH = "教材.pdf"
|
|
| 17 |
|
| 18 |
# 解析 PDF 並擷取文本(使用開發者預設的教材)
|
| 19 |
def extract_text_from_pdf():
|
| 20 |
-
with open(DEFAULT_PDF_PATH, "rb") as pdf_file:
|
| 21 |
reader = PyPDF2.PdfReader(pdf_file)
|
| 22 |
text = ""
|
| 23 |
for page in reader.pages:
|
|
@@ -37,20 +37,24 @@ def generate_question(topic, difficulty):
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def analyze_answer(user_input, correct_answer, topic):
|
| 42 |
global user_errors
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# 記錄錯誤主題(長期紀錄)
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
return feedback
|
| 56 |
|
|
@@ -71,15 +75,16 @@ with gr.Blocks() as demo:
|
|
| 71 |
topic_input = gr.Dropdown(choices=topics, label="選擇複習主題")
|
| 72 |
difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級")
|
| 73 |
question_output = gr.Textbox(label="AI 生成的問題")
|
|
|
|
| 74 |
|
| 75 |
ask_btn = gr.Button("生成問題")
|
| 76 |
ask_btn.click(generate_question, inputs=[topic_input, difficulty_input], outputs=question_output)
|
| 77 |
|
| 78 |
user_answer = gr.Textbox(label="你的回答")
|
| 79 |
-
analysis_result = gr.Textbox(label="
|
| 80 |
|
| 81 |
analyze_btn = gr.Button("分析回答")
|
| 82 |
-
analyze_btn.click(analyze_answer, inputs=[user_answer,
|
| 83 |
|
| 84 |
# 新增弱點歷史紀錄功能
|
| 85 |
weaknesses_output = gr.Textbox(label="弱點歷史紀錄")
|
|
|
|
| 17 |
|
| 18 |
# 解析 PDF 並擷取文本(使用開發者預設的教材)
|
| 19 |
def extract_text_from_pdf():
|
| 20 |
+
with open(DEFAULT_PDF_PATH, "rb") as pdf_file: # 修正此處為 "rb"
|
| 21 |
reader = PyPDF2.PdfReader(pdf_file)
|
| 22 |
text = ""
|
| 23 |
for page in reader.pages:
|
|
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
| 40 |
+
# AI 判斷對錯並提供正確答案與講解
|
| 41 |
def analyze_answer(user_input, correct_answer, topic):
|
| 42 |
global user_errors
|
| 43 |
|
| 44 |
+
# 使用 AI 來分析回答
|
| 45 |
+
prompt = f"學生回答:'{user_input}'\n\n正確答案:'{correct_answer}'\n\n請分析學生的回答是否正確,並提供正確答案與詳細講解。"
|
| 46 |
|
| 47 |
+
response = openai.ChatCompletion.create(
|
| 48 |
+
model="gpt-4",
|
| 49 |
+
messages=[{"role": "system", "content": "你是一位教育專家,請評估學生的回答,判斷是否正確,並提供正確答案與詳細講解。"},
|
| 50 |
+
{"role": "user", "content": prompt}]
|
| 51 |
+
)
|
| 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 |
|
|
|
|
| 75 |
topic_input = gr.Dropdown(choices=topics, label="選擇複習主題")
|
| 76 |
difficulty_input = gr.Dropdown(choices=difficulties, label="選擇難度等級")
|
| 77 |
question_output = gr.Textbox(label="AI 生成的問題")
|
| 78 |
+
correct_answer_output = gr.Textbox(label="正確答案")
|
| 79 |
|
| 80 |
ask_btn = gr.Button("生成問題")
|
| 81 |
ask_btn.click(generate_question, inputs=[topic_input, difficulty_input], outputs=question_output)
|
| 82 |
|
| 83 |
user_answer = gr.Textbox(label="你的回答")
|
| 84 |
+
analysis_result = gr.Textbox(label="AI 分析與講解")
|
| 85 |
|
| 86 |
analyze_btn = gr.Button("分析回答")
|
| 87 |
+
analyze_btn.click(analyze_answer, inputs=[user_answer, correct_answer_output, topic_input], outputs=analysis_result)
|
| 88 |
|
| 89 |
# 新增弱點歷史紀錄功能
|
| 90 |
weaknesses_output = gr.Textbox(label="弱點歷史紀錄")
|