shiue2000 commited on
Commit
5d9074c
·
verified ·
1 Parent(s): f0971cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -13,67 +13,67 @@ def audio_prompt_to_chatgpt(audio_path):
13
  if audio_path is None:
14
  return ""
15
 
16
- # 1️⃣ Transcribe audio → Chinese text
17
  result = whisper_model.transcribe(audio_path, language="zh")
18
  chinese_text = result.get("text", "").strip()
19
 
20
  if not chinese_text:
21
- return "⚠️ Could not transcribe audio."
22
 
23
- # 2️⃣ ChatGPT reply in Chinese
24
  try:
25
  response_ch = client.chat.completions.create(
26
  model="gpt-4o-mini",
27
  messages=[
28
- {"role": "system", "content": "You are a helpful assistant. Follow the user's instructions."},
29
  {"role": "user", "content": chinese_text}
30
  ],
31
  temperature=0.7
32
  )
33
  chatgpt_reply_chinese = response_ch.choices[0].message.content.strip()
34
  except Exception as e:
35
- chatgpt_reply_chinese = f"⚠️ ChatGPT failed: {e}"
36
 
37
  # 3️⃣ Translate ChatGPT reply into English
38
  try:
39
  response_en = client.chat.completions.create(
40
  model="gpt-4o-mini",
41
  messages=[
42
- {"role": "system", "content": "You are a professional translator. Translate the following text into fluent English."},
43
  {"role": "user", "content": chatgpt_reply_chinese}
44
  ],
45
  temperature=0
46
  )
47
  chatgpt_reply_english = response_en.choices[0].message.content.strip()
48
  except Exception as e:
49
- chatgpt_reply_english = f"⚠️ Translation failed: {e}"
50
 
51
  # Combine everything in one cell
52
  combined_output = (
53
- f"🈶 **Transcribed Chinese Prompt:**\n{chinese_text}\n\n"
54
- f"🤖 **ChatGPT Response (Chinese):**\n{chatgpt_reply_chinese}\n\n"
55
- f"🌍 **ChatGPT Response (English):**\n{chatgpt_reply_english}"
56
  )
57
 
58
  return combined_output
59
 
60
  # 🚀 Gradio UI
61
  with gr.Blocks() as app:
62
- gr.Markdown("### 🎤 Chinese Voice → ChatGPT with English Translation")
63
 
64
  audio_input = gr.Audio(
65
  sources=["microphone", "upload"],
66
  type="filepath",
67
- label="🎙️ Speak Chinese or Upload Audio"
68
  )
69
 
70
  output_box = gr.Textbox(
71
- label="📋 Results (Copy Friendly)",
72
  lines=15,
73
  interactive=False
74
  )
75
 
76
- submit_btn = gr.Button("講中文辦事,請按這裡")
77
 
78
  # Connect submit button
79
  submit_btn.click(
 
13
  if audio_path is None:
14
  return ""
15
 
16
+ # 1️⃣ Transcribe audio → Chinese text (Traditional Chinese)
17
  result = whisper_model.transcribe(audio_path, language="zh")
18
  chinese_text = result.get("text", "").strip()
19
 
20
  if not chinese_text:
21
+ return "⚠️ 無法辨識語音。"
22
 
23
+ # 2️⃣ ChatGPT reply in Traditional Chinese
24
  try:
25
  response_ch = client.chat.completions.create(
26
  model="gpt-4o-mini",
27
  messages=[
28
+ {"role": "system", "content": "你是一位樂於助人的助手,請使用繁體中文回答使用者。"},
29
  {"role": "user", "content": chinese_text}
30
  ],
31
  temperature=0.7
32
  )
33
  chatgpt_reply_chinese = response_ch.choices[0].message.content.strip()
34
  except Exception as e:
35
+ chatgpt_reply_chinese = f"⚠️ ChatGPT 錯誤: {e}"
36
 
37
  # 3️⃣ Translate ChatGPT reply into English
38
  try:
39
  response_en = client.chat.completions.create(
40
  model="gpt-4o-mini",
41
  messages=[
42
+ {"role": "system", "content": "You are a professional translator. Translate the following Traditional Chinese text into fluent English."},
43
  {"role": "user", "content": chatgpt_reply_chinese}
44
  ],
45
  temperature=0
46
  )
47
  chatgpt_reply_english = response_en.choices[0].message.content.strip()
48
  except Exception as e:
49
+ chatgpt_reply_english = f"⚠️ 翻譯失敗: {e}"
50
 
51
  # Combine everything in one cell
52
  combined_output = (
53
+ f"🈶 **語音辨識結果(繁體中文):**\n{chinese_text}\n\n"
54
+ f"🤖 **ChatGPT 回覆(繁體中文):**\n{chatgpt_reply_chinese}\n\n"
55
+ f"🌍 **ChatGPT 回覆(英文翻譯):**\n{chatgpt_reply_english}"
56
  )
57
 
58
  return combined_output
59
 
60
  # 🚀 Gradio UI
61
  with gr.Blocks() as app:
62
+ gr.Markdown("### 🎤 中文語音 → ChatGPT(繁體中文+英文翻譯)")
63
 
64
  audio_input = gr.Audio(
65
  sources=["microphone", "upload"],
66
  type="filepath",
67
+ label="🎙️ 請講中文或上傳語音檔"
68
  )
69
 
70
  output_box = gr.Textbox(
71
+ label="📋 結果(可複製)",
72
  lines=15,
73
  interactive=False
74
  )
75
 
76
+ submit_btn = gr.Button("講中文辦事請按這裡")
77
 
78
  # Connect submit button
79
  submit_btn.click(