yiming-0120 commited on
Commit
4110c58
·
verified ·
1 Parent(s): 9dd339c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -3,32 +3,29 @@ import os
3
  import openai
4
  import time
5
  from pathlib import Path
6
- # from pydub import AudioSegment
7
 
8
- # --- OpenAI API 設定 ---
9
  openai.api_key = os.getenv("OPENAI_API_KEY")
10
- if not openai.api_key:
11
- raise ValueError("請設定 OpenAI API Key 環境變數 OPENAI_API_KEY")
12
 
13
  client = openai.OpenAI()
14
 
15
- # --- 模型選擇 (OpenAI Models) ---
16
- LLM_MODEL = "gpt-3.5-turbo"
17
- ASR_MODEL = "whisper-1"
18
- TTS_MODEL = "tts-1"
19
  TTS_VOICE = "nova"
20
 
21
- # --- 預設辯論主題 (更新為台灣時事) ---
22
  CURRENT_TW_TOPICS = [
23
  "台灣是否應提高核能發電比例以應對能源需求?",
24
  "面對高房價,興建社會住宅是最佳解方嗎?",
25
- "如何平衡經濟發展與環境保護(例如:能源轉型、生態保育)?",
26
  "少子化下,延後退休年齡或引進更多外籍移工哪個更急迫?",
27
- "改善台灣交通亂象,應優先加強執法還是改善道路設計?",
28
  "健保制度改革:提高保費、部分負擔,或有其他永續方案?",
 
 
29
  ]
30
 
31
- # --- Helper 函數:呼叫 OpenAI API (call_asr, call_llm, call_tts 保持不變) ---
32
  def call_asr(audio_filepath):
33
  if not audio_filepath: return ""
34
  try:
@@ -68,7 +65,7 @@ def call_tts(text):
68
  print(f"TTS Error (OpenAI): {e}")
69
  return None
70
 
71
- # --- Gradio 主函數 (與上一版相同) ---
72
  def debate_turn(topic_from_dropdown, custom_topic, user_stance, user_input_text, user_input_audio, history):
73
  final_topic = ""
74
  if custom_topic and custom_topic.strip():
@@ -160,7 +157,7 @@ def debate_turn(topic_from_dropdown, custom_topic, user_stance, user_input_text,
160
 
161
  # --- Gradio UI (與上一版相同) ---
162
  with gr.Blocks(theme=gr.themes.Soft(), title="時事觀點對對碰 (OpenAI + 自訂主題)") as demo:
163
- gr.Markdown("## 🗣️ 時事觀點對對碰 (OpenAI + 自訂主題)")
164
  gr.Markdown("選擇預設議題或輸入自訂議題,選擇立場,用文字或語音提出論點,AI 將扮演對手與你辯論!")
165
 
166
  chat_history = gr.State([])
@@ -180,7 +177,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="時事觀點對對碰 (OpenAI +
180
 
181
  submit_btn = gr.Button("送出論點", variant="primary")
182
 
183
- # --- 事件綁定 (與上一版相同,但函數內部已修正 gr.skip) ---
184
  submit_btn.click(
185
  fn=debate_turn,
186
  inputs=[topic_dd, custom_topic_txt, stance_radio, user_txt, user_audio, chatbot_ui],
@@ -191,7 +188,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="時事觀點對對碰 (OpenAI +
191
  def clear_custom_topic(dropdown_value):
192
  if dropdown_value:
193
  return ""
194
- return gr.skip() # Corrected: Use lowercase 's'
195
 
196
  topic_dd.change(fn=clear_custom_topic, inputs=[topic_dd], outputs=[custom_topic_txt])
197
 
@@ -205,4 +202,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="時事觀點對對碰 (OpenAI +
205
 
206
 
207
  if __name__ == "__main__":
208
- demo.launch(debug=True) # debug=True is helpful
 
3
  import openai
4
  import time
5
  from pathlib import Path
 
6
 
 
7
  openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
8
 
9
  client = openai.OpenAI()
10
 
11
+ LLM_MODEL = "gpt-4o"
12
+ ASR_MODEL = "gpt-4o-transcribe"
13
+ TTS_MODEL = "gpt-4o-mini-tts"
 
14
  TTS_VOICE = "nova"
15
 
16
+ # --- 預設辯論主題 (台灣時事) ---
17
  CURRENT_TW_TOPICS = [
18
  "台灣是否應提高核能發電比例以應對能源需求?",
19
  "面對高房價,興建社會住宅是最佳解方嗎?",
20
+ "台灣是否應放寬代理孕母的限制? (涉及生育權、倫理爭議、法律規範)",
21
  "少子化下,延後退休年齡或引進更多外籍移工哪個更急迫?",
22
+ "台灣是否應積極推動雙語教育(中英文)作為主要教學語言?",
23
  "健保制度改革:提高保費、部分負擔,或有其他永續方案?",
24
+ "台灣是否應對網路言論進行更嚴格的管制?",
25
+ "台灣的死刑制度是否應廢除?",
26
  ]
27
 
28
+ # --- Helper 函數:呼叫 OpenAI API ---
29
  def call_asr(audio_filepath):
30
  if not audio_filepath: return ""
31
  try:
 
65
  print(f"TTS Error (OpenAI): {e}")
66
  return None
67
 
68
+ # --- Gradio 主函數 ---
69
  def debate_turn(topic_from_dropdown, custom_topic, user_stance, user_input_text, user_input_audio, history):
70
  final_topic = ""
71
  if custom_topic and custom_topic.strip():
 
157
 
158
  # --- Gradio UI (與上一版相同) ---
159
  with gr.Blocks(theme=gr.themes.Soft(), title="時事觀點對對碰 (OpenAI + 自訂主題)") as demo:
160
+ gr.Markdown("## 🗣️ 時事觀點對對碰")
161
  gr.Markdown("選擇預設議題或輸入自訂議題,選擇立場,用文字或語音提出論點,AI 將扮演對手與你辯論!")
162
 
163
  chat_history = gr.State([])
 
177
 
178
  submit_btn = gr.Button("送出論點", variant="primary")
179
 
180
+ # --- 事件綁定 ---
181
  submit_btn.click(
182
  fn=debate_turn,
183
  inputs=[topic_dd, custom_topic_txt, stance_radio, user_txt, user_audio, chatbot_ui],
 
188
  def clear_custom_topic(dropdown_value):
189
  if dropdown_value:
190
  return ""
191
+ return gr.skip()
192
 
193
  topic_dd.change(fn=clear_custom_topic, inputs=[topic_dd], outputs=[custom_topic_txt])
194
 
 
202
 
203
 
204
  if __name__ == "__main__":
205
+ demo.launch(debug=True)