Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 "⚠️
|
| 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": "
|
| 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
|
| 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"⚠️
|
| 50 |
|
| 51 |
# Combine everything in one cell
|
| 52 |
combined_output = (
|
| 53 |
-
f"🈶 **
|
| 54 |
-
f"🤖 **ChatGPT
|
| 55 |
-
f"🌍 **ChatGPT
|
| 56 |
)
|
| 57 |
|
| 58 |
return combined_output
|
| 59 |
|
| 60 |
# 🚀 Gradio UI
|
| 61 |
with gr.Blocks() as app:
|
| 62 |
-
gr.Markdown("### 🎤
|
| 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(
|
|
|
|
| 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(
|