iamSammi commited on
Commit
67166c4
·
verified ·
1 Parent(s): 928b997

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -46
app.py CHANGED
@@ -37,21 +37,6 @@ def generate_question(topic, difficulty):
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",
44
- messages=[{"role": "system", "content": "請以教師語氣回答"},
45
- {"role": "user", "content": text}]
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
@@ -66,24 +51,11 @@ def analyze_answer(user_input, correct_answer, topic):
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("# 教師檢定智慧陪讀家教 🚀")
89
 
@@ -100,20 +72,4 @@ with gr.Blocks() as demo:
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()
 
37
  )
38
  return response['choices'][0]['message']['content']
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # 分析回答完整性 + 記錄弱點(新增長期歷史紀錄)
41
  def analyze_answer(user_input, correct_answer, topic):
42
  global user_errors
 
51
 
52
  # 記錄錯誤主題(長期紀錄)
53
  if feedback != "✅ 正確!":
54
+ user_errors[topic] = user_errors.get(topic, 0) + 1
 
 
 
55
 
56
  return feedback
57
 
58
+ # 設定 Gradio 介面(移除語音)
 
 
 
 
 
 
 
 
 
 
59
  with gr.Blocks() as demo:
60
  gr.Markdown("# 教師檢定智慧陪讀家教 🚀")
61
 
 
72
  analyze_btn = gr.Button("分析回答")
73
  analyze_btn.click(analyze_answer, inputs=[user_answer, question_output, topic_input], outputs=analysis_result)
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  demo.launch()