Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import os
|
|
| 5 |
|
| 6 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
-
#
|
| 9 |
topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"]
|
| 10 |
difficulties = ["簡單", "中等", "困難"]
|
| 11 |
|
|
@@ -37,7 +37,7 @@ def generate_question(topic, difficulty):
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
| 40 |
-
# AI
|
| 41 |
def ai_speak(text):
|
| 42 |
response = openai.ChatCompletion.create(
|
| 43 |
model="gpt-4",
|
|
@@ -46,6 +46,11 @@ def ai_speak(text):
|
|
| 46 |
)
|
| 47 |
return response['choices'][0]['message']['content']
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# 分析回答完整性 + 記錄弱點
|
| 50 |
def analyze_answer(user_input, correct_answer, topic):
|
| 51 |
global user_errors
|
|
@@ -96,6 +101,16 @@ with gr.Blocks() as demo:
|
|
| 96 |
weakness_btn = gr.Button("查看可加強的知識點")
|
| 97 |
weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
demo.launch()
|
|
|
|
| 5 |
|
| 6 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
+
# **更新後的主題選項**
|
| 9 |
topics = ["教育哲學", "教育社會學", "教育心理學", "課程與教學", "教學原理", "班級經營", "教育測驗與評量", "青少年問題與輔導"]
|
| 10 |
difficulties = ["簡單", "中等", "困難"]
|
| 11 |
|
|
|
|
| 37 |
)
|
| 38 |
return response['choices'][0]['message']['content']
|
| 39 |
|
| 40 |
+
# AI 語音輸出(TTS)
|
| 41 |
def ai_speak(text):
|
| 42 |
response = openai.ChatCompletion.create(
|
| 43 |
model="gpt-4",
|
|
|
|
| 46 |
)
|
| 47 |
return response['choices'][0]['message']['content']
|
| 48 |
|
| 49 |
+
# 學習者語音輸入(STT)
|
| 50 |
+
def transcribe_speech(audio):
|
| 51 |
+
response = openai.Audio.transcribe("whisper-1", audio)
|
| 52 |
+
return response["text"]
|
| 53 |
+
|
| 54 |
# 分析回答完整性 + 記錄弱點
|
| 55 |
def analyze_answer(user_input, correct_answer, topic):
|
| 56 |
global user_errors
|
|
|
|
| 101 |
weakness_btn = gr.Button("查看可加強的知識點")
|
| 102 |
weakness_btn.click(get_weaknesses, outputs=weaknesses_output)
|
| 103 |
|
| 104 |
+
# 新增語音輸入功能
|
| 105 |
+
speech_input = gr.Audio(source="microphone", type="file", label="你的語音回答")
|
| 106 |
+
transcribed_text = gr.Textbox(label="語音轉文字")
|
| 107 |
+
|
| 108 |
+
speech_to_text_btn = gr.Button("轉換語音")
|
| 109 |
+
speech_to_text_btn.click(transcribe_speech, inputs=speech_input, outputs=transcribed_text)
|
| 110 |
+
|
| 111 |
+
# 新增 AI 語音回應功能
|
| 112 |
+
ai_speech_output = gr.Textbox(label="AI 語音回答")
|
| 113 |
+
speech_response_btn = gr.Button("語音回應")
|
| 114 |
+
speech_response_btn.click(ai_speak, inputs=transcribed_text, outputs=ai_speech_output)
|
| 115 |
|
| 116 |
demo.launch()
|