shiue2000 commited on
Commit
de39378
·
verified ·
1 Parent(s): 9a12212

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -20
app.py CHANGED
@@ -11,7 +11,7 @@ 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")
@@ -20,9 +20,9 @@ def audio_prompt_to_chatgpt(audio_path):
20
  if not chinese_text:
21
  return "⚠️ Could not transcribe audio.", "", ""
22
 
23
- # 2️⃣ Send transcription as a prompt to ChatGPT
24
  try:
25
- response = 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."},
@@ -30,13 +30,13 @@ def audio_prompt_to_chatgpt(audio_path):
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."},
@@ -44,28 +44,44 @@ def audio_prompt_to_chatgpt(audio_path):
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(
55
- fn=audio_prompt_to_chatgpt,
56
- inputs=gr.Audio(
57
- sources=["microphone", "upload"], # microphone or upload
 
58
  type="filepath",
59
  label="🎙️ Speak Chinese or Upload Audio"
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__":
71
  app.launch()
 
11
 
12
  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")
 
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."},
 
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."},
 
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
+ return chatgpt_reply_chinese, chatgpt_reply_english, chinese_text
52
 
53
  # 🚀 Gradio UI
54
+ with gr.Blocks() as app:
55
+ gr.Markdown("### 🎤 Chinese Voice → ChatGPT with English Translation")
56
+
57
+ audio_input = gr.Audio(
58
+ sources=["microphone", "upload"],
59
  type="filepath",
60
  label="🎙️ Speak Chinese or Upload Audio"
61
+ )
62
+
63
+ chinese_output = gr.Textbox(label="🤖 ChatGPT Response (Chinese)")
64
+ english_output = gr.Textbox(label="🌍 ChatGPT Response (English)")
65
+ transcription_output = gr.Textbox(label="🈶 Transcribed Chinese Prompt")
66
+
67
+ submit_btn = gr.Button("Generate Response")
68
+
69
+ # Copy buttons
70
+ copy_chinese_btn = gr.Button("Copy Chinese")
71
+ copy_english_btn = gr.Button("Copy English")
72
+ copy_transcription_btn = gr.Button("Copy Transcription")
73
+
74
+ # Connect submit button
75
+ submit_btn.click(
76
+ fn=audio_prompt_to_chatgpt,
77
+ inputs=audio_input,
78
+ outputs=[chinese_output, english_output, transcription_output]
79
+ )
80
+
81
+ # Connect copy buttons to copy respective outputs
82
+ copy_chinese_btn.click(lambda x: x, inputs=chinese_output, outputs=chinese_output)
83
+ copy_english_btn.click(lambda x: x, inputs=english_output, outputs=english_output)
84
+ copy_transcription_btn.click(lambda x: x, inputs=transcription_output, outputs=transcription_output)
85
 
86
  if __name__ == "__main__":
87
  app.launch()