Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import whisper | |
| import os | |
| from openai import OpenAI | |
| # 🔑 Load OpenAI API key from Hugging Face Space secrets | |
| client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | |
| # 🎧 Load Whisper model once | |
| whisper_model = whisper.load_model("small") | |
| def audio_prompt_to_chatgpt(audio_path): | |
| if audio_path is None: | |
| return "" | |
| # 1️⃣ Transcribe audio → Chinese text (Traditional Chinese) | |
| result = whisper_model.transcribe(audio_path, language="zh") | |
| chinese_text = result.get("text", "").strip() | |
| if not chinese_text: | |
| return "⚠️ 無法辨識語音。" | |
| # 2️⃣ ChatGPT reply in Traditional Chinese | |
| try: | |
| response_ch = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": "你是一位樂於助人的助手,請使用繁體中文回答使用者。"}, | |
| {"role": "user", "content": chinese_text} | |
| ], | |
| temperature=0.7 | |
| ) | |
| chatgpt_reply_chinese = response_ch.choices[0].message.content.strip() | |
| except Exception as e: | |
| chatgpt_reply_chinese = f"⚠️ ChatGPT 錯誤: {e}" | |
| # 3️⃣ Translate ChatGPT reply into English | |
| try: | |
| response_en = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": "You are a professional translator. Translate the following Traditional Chinese text into fluent English."}, | |
| {"role": "user", "content": chatgpt_reply_chinese} | |
| ], | |
| temperature=0 | |
| ) | |
| chatgpt_reply_english = response_en.choices[0].message.content.strip() | |
| except Exception as e: | |
| chatgpt_reply_english = f"⚠️ 翻譯失敗: {e}" | |
| # Combine everything in one cell | |
| combined_output = ( | |
| f"🈶 **語音辨識結果(繁體中文):**\n{chinese_text}\n\n" | |
| f"🤖 **ChatGPT 回覆(繁體中文):**\n{chatgpt_reply_chinese}\n\n" | |
| f"🌍 **ChatGPT 回覆(英文翻譯):**\n{chatgpt_reply_english}" | |
| ) | |
| return combined_output | |
| # 🚀 Gradio UI | |
| with gr.Blocks() as app: | |
| gr.Markdown("### 🎤 中文語音 → ChatGPT(繁體中文+英文翻譯)") | |
| audio_input = gr.Audio( | |
| sources=["microphone", "upload"], | |
| type="filepath", | |
| label="🎙️ 請講中文或上傳語音檔" | |
| ) | |
| output_box = gr.Textbox( | |
| label="📋 結果(可複製)", | |
| lines=15, | |
| interactive=False, | |
| elem_id="result_box" | |
| ) | |
| submit_btn = gr.Button("講中文辦事,請按這裡~") | |
| # 📋 複製按鈕(用 JavaScript 把 Textbox 文字複製到剪貼簿) | |
| copy_btn = gr.HTML( | |
| """ | |
| <button onclick="navigator.clipboard.writeText(document.getElementById('result_box').querySelector('textarea').value)"> | |
| 📋 複製結果 | |
| </button> | |
| """ | |
| ) | |
| # Connect submit button | |
| submit_btn.click( | |
| fn=audio_prompt_to_chatgpt, | |
| inputs=audio_input, | |
| outputs=output_box | |
| ) | |
| if __name__ == "__main__": | |
| app.launch() | |