Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,43 +3,55 @@ import whisper
|
|
| 3 |
import openai
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 8 |
|
| 9 |
-
# Load Whisper for
|
| 10 |
whisper_model = whisper.load_model("small")
|
| 11 |
|
| 12 |
-
def transcribe_and_translate(
|
|
|
|
|
|
|
|
|
|
| 13 |
# Step 1: Transcribe audio β Chinese text
|
| 14 |
-
result = whisper_model.transcribe(
|
| 15 |
-
chinese_text = result
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
return chinese_text, english_text
|
| 32 |
|
| 33 |
-
# Gradio UI
|
| 34 |
app = gr.Interface(
|
| 35 |
fn=transcribe_and_translate,
|
| 36 |
-
inputs=gr.Audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
outputs=[
|
| 38 |
-
gr.Textbox(label="πΆ Chinese
|
| 39 |
-
gr.Textbox(label="π
|
| 40 |
],
|
| 41 |
-
title="π€ Chinese Voice β English
|
| 42 |
-
description="Speak Chinese
|
| 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__":
|