Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,20 @@ from deep_translator import GoogleTranslator
|
|
| 8 |
from langdetect import detect
|
| 9 |
|
| 10 |
# -----------------------------
|
| 11 |
-
# STEP 1: API Key setup
|
| 12 |
# -----------------------------
|
| 13 |
-
#
|
| 14 |
-
#
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
client = Groq(api_key=
|
| 20 |
|
| 21 |
|
| 22 |
# -----------------------------
|
|
@@ -39,7 +44,6 @@ def transcribe_audio(audio_path):
|
|
| 39 |
# STEP 3: Language Detection
|
| 40 |
# -----------------------------
|
| 41 |
def detect_language(text):
|
| 42 |
-
"""Detect language using langdetect."""
|
| 43 |
try:
|
| 44 |
return detect(text)
|
| 45 |
except:
|
|
@@ -50,7 +54,6 @@ def detect_language(text):
|
|
| 50 |
# STEP 4: Translation
|
| 51 |
# -----------------------------
|
| 52 |
def translate_text(text, dest_lang):
|
| 53 |
-
"""Translate text using deep-translator with retry."""
|
| 54 |
if not text.strip():
|
| 55 |
return "[Translation error] Empty input text"
|
| 56 |
try:
|
|
@@ -68,7 +71,6 @@ def translate_text(text, dest_lang):
|
|
| 68 |
# STEP 5: Text-to-Speech
|
| 69 |
# -----------------------------
|
| 70 |
def text_to_speech(text, lang):
|
| 71 |
-
"""Convert text to speech using gTTS."""
|
| 72 |
try:
|
| 73 |
tts = gTTS(text=text, lang=lang)
|
| 74 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
@@ -82,7 +84,6 @@ def text_to_speech(text, lang):
|
|
| 82 |
# STEP 6: Full Pipeline
|
| 83 |
# -----------------------------
|
| 84 |
def pipeline_translate(audio, target_lang_hint):
|
| 85 |
-
"""Full pipeline: speech β text β translate β speech."""
|
| 86 |
if not audio:
|
| 87 |
return "[Error] No audio input", None, None, None
|
| 88 |
|
|
@@ -115,7 +116,7 @@ def pipeline_translate(audio, target_lang_hint):
|
|
| 115 |
# -----------------------------
|
| 116 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 117 |
gr.Markdown("## π AI Voice Translator")
|
| 118 |
-
gr.Markdown("Speak any language β it will transcribe, translate, and
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
audio_input = gr.Audio(sources=["microphone"], type="filepath", label="π€ Record Your Speech")
|
|
|
|
| 8 |
from langdetect import detect
|
| 9 |
|
| 10 |
# -----------------------------
|
| 11 |
+
# STEP 1: API Key setup (Hugging Face Safe)
|
| 12 |
# -----------------------------
|
| 13 |
+
# Make sure to set GROQ_API_KEY in your Space:
|
| 14 |
+
# Go to: Settings β Variables and secrets β Repository secrets β Add new secret
|
| 15 |
+
# Name: GROQ_API_KEY
|
| 16 |
+
# Value: your key (like gsk_xxxxxxx)
|
| 17 |
|
| 18 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 19 |
+
if not api_key:
|
| 20 |
+
raise ValueError(
|
| 21 |
+
"β GROQ_API_KEY not found! Please add it in your Hugging Face Space β Settings β Secrets."
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
client = Groq(api_key=api_key)
|
| 25 |
|
| 26 |
|
| 27 |
# -----------------------------
|
|
|
|
| 44 |
# STEP 3: Language Detection
|
| 45 |
# -----------------------------
|
| 46 |
def detect_language(text):
|
|
|
|
| 47 |
try:
|
| 48 |
return detect(text)
|
| 49 |
except:
|
|
|
|
| 54 |
# STEP 4: Translation
|
| 55 |
# -----------------------------
|
| 56 |
def translate_text(text, dest_lang):
|
|
|
|
| 57 |
if not text.strip():
|
| 58 |
return "[Translation error] Empty input text"
|
| 59 |
try:
|
|
|
|
| 71 |
# STEP 5: Text-to-Speech
|
| 72 |
# -----------------------------
|
| 73 |
def text_to_speech(text, lang):
|
|
|
|
| 74 |
try:
|
| 75 |
tts = gTTS(text=text, lang=lang)
|
| 76 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
|
|
| 84 |
# STEP 6: Full Pipeline
|
| 85 |
# -----------------------------
|
| 86 |
def pipeline_translate(audio, target_lang_hint):
|
|
|
|
| 87 |
if not audio:
|
| 88 |
return "[Error] No audio input", None, None, None
|
| 89 |
|
|
|
|
| 116 |
# -----------------------------
|
| 117 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 118 |
gr.Markdown("## π AI Voice Translator")
|
| 119 |
+
gr.Markdown("Speak in any language β it will transcribe, translate, and speak it back!")
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
audio_input = gr.Audio(sources=["microphone"], type="filepath", label="π€ Record Your Speech")
|