shiue2000 commited on
Commit
2a9b264
Β·
verified Β·
1 Parent(s): 93602e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -19
app.py CHANGED
@@ -3,43 +3,55 @@ import whisper
3
  import openai
4
  import os
5
 
6
- # Make sure you set your OpenAI API key in the HF Space secrets (Settings β†’ Variables β†’ Add "OPENAI_API_KEY")
7
  openai.api_key = os.environ.get("OPENAI_API_KEY")
8
 
9
- # Load Whisper for speech-to-text
10
  whisper_model = whisper.load_model("small")
11
 
12
- def transcribe_and_translate(audio):
 
 
 
13
  # Step 1: Transcribe audio β†’ Chinese text
14
- result = whisper_model.transcribe(audio, language="zh")
15
- chinese_text = result["text"].strip()
 
 
 
16
 
17
  # Step 2: Send transcription to ChatGPT for English translation
18
  messages = [
19
- {"role": "system", "content": "You are a translator. Translate the following Chinese into natural English."},
20
  {"role": "user", "content": chinese_text}
21
  ]
22
 
23
- response = openai.ChatCompletion.create(
24
- model="gpt-4o-mini", # fast & lightweight, can use gpt-4o if you want best quality
25
- messages=messages,
26
- temperature=0
27
- )
28
-
29
- english_text = response["choices"][0]["message"]["content"].strip()
 
 
30
 
31
  return chinese_text, english_text
32
 
33
- # Gradio UI
34
  app = gr.Interface(
35
  fn=transcribe_and_translate,
36
- inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="πŸŽ™οΈ Speak Chinese or Upload Audio"),
 
 
 
 
37
  outputs=[
38
- gr.Textbox(label="🈢 Chinese Text"),
39
- gr.Textbox(label="🌍 ChatGPT Translation (English)")
40
  ],
41
- title="🎀 Chinese Voice β†’ English (via ChatGPT)",
42
- description="Speak Chinese, the app will transcribe it and then use ChatGPT to translate into English."
43
  )
44
 
45
  if __name__ == "__main__":
 
3
  import openai
4
  import os
5
 
6
+ # πŸ”‘ Load OpenAI API key from Hugging Face Space secrets
7
  openai.api_key = os.environ.get("OPENAI_API_KEY")
8
 
9
+ # 🎧 Load Whisper model once (small = faster; use "base" for lighter)
10
  whisper_model = whisper.load_model("small")
11
 
12
+ def transcribe_and_translate(audio_path):
13
+ if audio_path is None:
14
+ return "No audio detected.", "No translation."
15
+
16
  # Step 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.", "No translation."
22
 
23
  # Step 2: Send transcription to ChatGPT for English translation
24
  messages = [
25
+ {"role": "system", "content": "You are a professional translator. Translate the following Chinese into fluent, natural English."},
26
  {"role": "user", "content": chinese_text}
27
  ]
28
 
29
+ try:
30
+ response = openai.ChatCompletion.create(
31
+ model="gpt-4o-mini", # can change to "gpt-4o" for higher quality
32
+ messages=messages,
33
+ temperature=0
34
+ )
35
+ english_text = response["choices"][0]["message"]["content"].strip()
36
+ except Exception as e:
37
+ english_text = f"⚠️ Translation failed: {e}"
38
 
39
  return chinese_text, english_text
40
 
41
+ # πŸš€ Gradio UI
42
  app = gr.Interface(
43
  fn=transcribe_and_translate,
44
+ inputs=gr.Audio(
45
+ sources=["microphone", "upload"],
46
+ type="filepath",
47
+ label="πŸŽ™οΈ Speak Chinese or Upload Audio"
48
+ ),
49
  outputs=[
50
+ gr.Textbox(label="🈢 Chinese Transcription"),
51
+ gr.Textbox(label="🌍 English Translation (ChatGPT)")
52
  ],
53
+ title="🎀 Chinese Voice β†’ English Translator",
54
+ description="Speak Chinese or upload audio. Whisper transcribes, then ChatGPT translates into English."
55
  )
56
 
57
  if __name__ == "__main__":