File size: 3,164 Bytes
edb00ca
 
eeb9c40
c3b887e
edb00ca
2a9b264
c3b887e
eeb9c40
86936f4
9b0a1ac
edb00ca
86936f4
2a9b264
2df333c
2a9b264
5d9074c
2a9b264
 
 
 
5d9074c
eeb9c40
5d9074c
2a9b264
de39378
ff98e3b
86936f4
5d9074c
86936f4
 
 
2a9b264
de39378
2a9b264
5d9074c
edb00ca
2995523
 
de39378
2995523
 
5d9074c
2995523
 
 
 
de39378
2995523
5d9074c
2995523
2df333c
 
5d9074c
 
 
2df333c
 
 
edb00ca
2a9b264
de39378
5d9074c
de39378
 
 
2a9b264
5d9074c
de39378
 
2df333c
5d9074c
2df333c
703517a
 
2df333c
de39378
e593d16
de39378
703517a
 
 
 
 
 
 
 
 
de39378
 
 
 
2df333c
de39378
 
edb00ca
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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()