Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,12 +37,6 @@ def generate_question(topic, difficulty):
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
| 40 |
-
# 學習者語音輸入(STT)
|
| 41 |
-
def transcribe_speech(audio_file):
|
| 42 |
-
with open(audio_file, "rb") as audio:
|
| 43 |
-
response = openai.Audio.transcribe("whisper-1", audio)
|
| 44 |
-
return response["text"]
|
| 45 |
-
|
| 46 |
# AI 語音輸出(TTS)
|
| 47 |
def ai_speak(text):
|
| 48 |
response = openai.ChatCompletion.create(
|
|
@@ -52,6 +46,43 @@ def ai_speak(text):
|
|
| 52 |
)
|
| 53 |
return response['choices'][0]['message']['content']
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# 設定 Gradio 介面
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
|
|
@@ -63,16 +94,26 @@ with gr.Blocks() as demo:
|
|
| 63 |
ask_btn = gr.Button("生成問題")
|
| 64 |
ask_btn.click(generate_question, inputs=[topic_input, difficulty_input], outputs=question_output)
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
speech_input = gr.Audio(type="filepath", label="你的語音回答")
|
| 68 |
transcribed_text = gr.Textbox(label="語音轉文字")
|
| 69 |
|
| 70 |
-
speech_to_text_btn = gr.Button("
|
| 71 |
speech_to_text_btn.click(transcribe_speech, inputs=speech_input, outputs=transcribed_text)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
demo.launch()
|
|
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# AI 語音輸出(TTS)
|
| 41 |
def ai_speak(text):
|
| 42 |
response = openai.ChatCompletion.create(
|
|
|
|
| 46 |
)
|
| 47 |
return response['choices'][0]['message']['content']
|
| 48 |
|
| 49 |
+
# 學習者語音輸入(STT)
|
| 50 |
+
def transcribe_speech(audio_file):
|
| 51 |
+
with open(audio_file, "rb") as audio:
|
| 52 |
+
response = openai.Audio.transcribe("whisper-1", audio)
|
| 53 |
+
return response["text"]
|
| 54 |
+
|
| 55 |
+
# 分析回答完整性 + 記錄弱點(新增長期歷史紀錄)
|
| 56 |
+
def analyze_answer(user_input, correct_answer, topic):
|
| 57 |
+
global user_errors
|
| 58 |
+
|
| 59 |
+
if user_input.strip().lower() == correct_answer.strip().lower():
|
| 60 |
+
return "✅ 正確!"
|
| 61 |
+
|
| 62 |
+
elif user_input in correct_answer:
|
| 63 |
+
feedback = "⚠️ 部分正確,請補充完整"
|
| 64 |
+
else:
|
| 65 |
+
feedback = "❌ 答非所問,請重新思考"
|
| 66 |
+
|
| 67 |
+
# 記錄錯誤主題(長期紀錄)
|
| 68 |
+
if feedback != "✅ 正確!":
|
| 69 |
+
if topic in user_errors:
|
| 70 |
+
user_errors[topic] += 1
|
| 71 |
+
else:
|
| 72 |
+
user_errors[topic] = 1
|
| 73 |
+
|
| 74 |
+
return feedback
|
| 75 |
+
|
| 76 |
+
# 列出可加強的知識點(新增歷史紀錄顯示)
|
| 77 |
+
def get_weaknesses():
|
| 78 |
+
if not user_errors:
|
| 79 |
+
return "🎯 目前沒有明顯弱點,繼續保持!"
|
| 80 |
+
|
| 81 |
+
sorted_weaknesses = sorted(user_errors.items(), key=lambda x: x[1], reverse=True)
|
| 82 |
+
|
| 83 |
+
history_text = "\n".join([f"{k}: {v} 次錯誤" for k, v in sorted_weaknesses])
|
| 84 |
+
return f"📌 **你的弱點領域**:\n{history_text}"
|
| 85 |
+
|
| 86 |
# 設定 Gradio 介面
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
|
|
|
|
| 94 |
ask_btn = gr.Button("生成問題")
|
| 95 |
ask_btn.click(generate_question, inputs=[topic_input, difficulty_input], outputs=question_output)
|
| 96 |
|
| 97 |
+
user_answer = gr.Textbox(label="你的回答")
|
| 98 |
+
analysis_result = gr.Textbox(label="分析結果")
|
| 99 |
+
|
| 100 |
+
analyze_btn = gr.Button("分析回答")
|
| 101 |
+
analyze_btn.click(analyze_answer, inputs=[user_answer, question_output, topic_input], outputs=analysis_result)
|
| 102 |
+
|
| 103 |
+
weaknesses_output = gr.Textbox(label="智能弱點分析")
|
| 104 |
+
weakness_btn = gr.Button("查看可加強的知識點")
|
| 105 |
+
weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
|
| 106 |
+
|
| 107 |
+
# 新增語音輸入功能(使用檔案模式)
|
| 108 |
speech_input = gr.Audio(type="filepath", label="你的語音回答")
|
| 109 |
transcribed_text = gr.Textbox(label="語音轉文字")
|
| 110 |
|
| 111 |
+
speech_to_text_btn = gr.Button("轉換語音")
|
| 112 |
speech_to_text_btn.click(transcribe_speech, inputs=speech_input, outputs=transcribed_text)
|
| 113 |
|
| 114 |
+
# 新增 AI 語音回應功能
|
| 115 |
+
ai_speech_output = gr.Textbox(label="AI 語音回答")
|
| 116 |
+
speech_response_btn = gr.Button("語音回應")
|
| 117 |
+
speech_response_btn.click(ai_speak, inputs=transcribed_text, outputs=ai_speech_output)
|
| 118 |
|
| 119 |
demo.launch()
|