Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,19 +33,30 @@ pdf_text = extract_text_from_pdf()
|
|
| 33 |
def generate_question(topic, difficulty):
|
| 34 |
if not pdf_text.strip():
|
| 35 |
return "⚠️ 無法載入教材內容,請確認 PDF 是否存在。"
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
response = openai.ChatCompletion.create(
|
| 39 |
model="gpt-4o-mini-2024-07-18",
|
| 40 |
messages=[
|
| 41 |
-
{"role": "system", "content": "
|
| 42 |
{"role": "user", "content": prompt}
|
| 43 |
]
|
| 44 |
)
|
| 45 |
question = response["choices"][0]["message"]["content"]
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
answer_response = openai.ChatCompletion.create(
|
| 50 |
model="gpt-4o-mini-2024-07-18",
|
| 51 |
messages=[
|
|
@@ -54,10 +65,7 @@ def generate_question(topic, difficulty):
|
|
| 54 |
]
|
| 55 |
)
|
| 56 |
correct_answer = answer_response["choices"][0]["message"]["content"]
|
| 57 |
-
|
| 58 |
-
# 儲存參考答案
|
| 59 |
reference_answers[question] = correct_answer.strip() # 儲存正確答案
|
| 60 |
-
return question
|
| 61 |
except Exception as e:
|
| 62 |
return f"⚠️ 發生錯誤:{e}"
|
| 63 |
|
|
@@ -74,10 +82,12 @@ def analyze_answer(user_input, question):
|
|
| 74 |
global user_errors
|
| 75 |
if not user_input.strip():
|
| 76 |
return "⚠️ 請輸入回答。"
|
| 77 |
-
if not pdf_text.strip():
|
| 78 |
-
return "⚠️ 教材內容未載入,請確認 PDF。"
|
| 79 |
|
| 80 |
correct_answer = reference_answers.get(question, "無法獲取正確答案")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與講解:\n{pdf_text}\n\n問題:{question}\n學生回答:'{user_input}'"
|
| 82 |
|
| 83 |
try:
|
|
|
|
| 33 |
def generate_question(topic, difficulty):
|
| 34 |
if not pdf_text.strip():
|
| 35 |
return "⚠️ 無法載入教材內容,請確認 PDF 是否存在。"
|
| 36 |
+
|
| 37 |
+
# 修改提示以生成選擇題或填空題
|
| 38 |
+
prompt = f"請根據以下教育學教材內容,設計一個屬於「{topic}」主題、「{difficulty}」難度的選擇題或填空題擇一:\n\n{pdf_text}"
|
| 39 |
+
|
| 40 |
try:
|
| 41 |
response = openai.ChatCompletion.create(
|
| 42 |
model="gpt-4o-mini-2024-07-18",
|
| 43 |
messages=[
|
| 44 |
+
{"role": "system", "content": "你是一位教育專家,請根據教材內容設計題目。(不需要包含解析)"},
|
| 45 |
{"role": "user", "content": prompt}
|
| 46 |
]
|
| 47 |
)
|
| 48 |
question = response["choices"][0]["message"]["content"]
|
| 49 |
+
return question.strip() # 返回生成的問題
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"⚠️ 發生錯誤:{e}"
|
| 52 |
+
|
| 53 |
+
def save_answer(question):
|
| 54 |
+
if not pdf_text.strip():
|
| 55 |
+
return "⚠️ 教材內容未載入,請確認 PDF。"
|
| 56 |
+
|
| 57 |
+
# 獲取參考答案
|
| 58 |
+
answer_prompt = f"請根據以下問題提供正確答案:\n問題:{question}\n\n教材內容:\n{pdf_text}"
|
| 59 |
+
try:
|
| 60 |
answer_response = openai.ChatCompletion.create(
|
| 61 |
model="gpt-4o-mini-2024-07-18",
|
| 62 |
messages=[
|
|
|
|
| 65 |
]
|
| 66 |
)
|
| 67 |
correct_answer = answer_response["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
| 68 |
reference_answers[question] = correct_answer.strip() # 儲存正確答案
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
return f"⚠️ 發生錯誤:{e}"
|
| 71 |
|
|
|
|
| 82 |
global user_errors
|
| 83 |
if not user_input.strip():
|
| 84 |
return "⚠️ 請輸入回答。"
|
|
|
|
|
|
|
| 85 |
|
| 86 |
correct_answer = reference_answers.get(question, "無法獲取正確答案")
|
| 87 |
+
|
| 88 |
+
# 獲取參考答案
|
| 89 |
+
save_answer(question) # 在分析之前儲存答案
|
| 90 |
+
|
| 91 |
prompt = f"請根據以下教材內容,檢查學生的回答是否正確,並提供正確答案與講解:\n{pdf_text}\n\n問題:{question}\n學生回答:'{user_input}'"
|
| 92 |
|
| 93 |
try:
|