shiue2000 commited on
Commit
2995523
·
verified ·
1 Parent(s): 86936f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -11,14 +11,14 @@ whisper_model = whisper.load_model("small")
11
 
12
  def audio_prompt_to_chatgpt(audio_path):
13
  if audio_path is None:
14
- return "No audio detected.", ""
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️⃣ Send transcription as a prompt to ChatGPT
24
  try:
@@ -30,11 +30,25 @@ def audio_prompt_to_chatgpt(audio_path):
30
  ],
31
  temperature=0.7
32
  )
33
- chatgpt_reply = response.choices[0].message.content.strip()
34
  except Exception as e:
35
- chatgpt_reply = f"⚠️ ChatGPT failed: {e}"
36
 
37
- return chinese_text, chatgpt_reply
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # 🚀 Gradio UI
40
  app = gr.Interface(
@@ -46,10 +60,11 @@ app = gr.Interface(
46
  ),
47
  outputs=[
48
  gr.Textbox(label="🈶 Transcribed Chinese Prompt"),
49
- gr.Textbox(label="🤖 ChatGPT Response")
 
50
  ],
51
- title="🎤 Chinese Voice → ChatGPT Prompt",
52
- description="Speak Chinese or upload audio. Whisper transcribes, then ChatGPT executes your instructions."
53
  )
54
 
55
  if __name__ == "__main__":
 
11
 
12
  def audio_prompt_to_chatgpt(audio_path):
13
  if audio_path is None:
14
+ return "No audio detected.", "", ""
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️⃣ Send transcription as a prompt to ChatGPT
24
  try:
 
30
  ],
31
  temperature=0.7
32
  )
33
+ chatgpt_reply_chinese = response.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
+ translation_response = 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 = translation_response.choices[0].message.content.strip()
48
+ except Exception as e:
49
+ chatgpt_reply_english = f"⚠️ Translation failed: {e}"
50
+
51
+ return chinese_text, chatgpt_reply_chinese, chatgpt_reply_english
52
 
53
  # 🚀 Gradio UI
54
  app = gr.Interface(
 
60
  ),
61
  outputs=[
62
  gr.Textbox(label="🈶 Transcribed Chinese Prompt"),
63
+ gr.Textbox(label="🤖 ChatGPT Response (Chinese)"),
64
+ gr.Textbox(label="🌍 ChatGPT Response (English)")
65
  ],
66
+ title="🎤 Chinese Voice → ChatGPT Prompt with English",
67
+ description="Speak Chinese or upload audio. Whisper transcribes, ChatGPT executes your instruction, and the reply is shown in Chinese and English."
68
  )
69
 
70
  if __name__ == "__main__":