Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import tempfile | |
| import time | |
| from groq import Groq | |
| from gtts import gTTS | |
| from deep_translator import GoogleTranslator | |
| from langdetect import detect | |
| # ----------------------------- | |
| # STEP 1: API Key setup (Hugging Face Safe) | |
| # ----------------------------- | |
| # Make sure to set GROQ_API_KEY in your Space: | |
| # Go to: Settings β Variables and secrets β Repository secrets β Add new secret | |
| # Name: GROQ_API_KEY | |
| # Value: your key (like gsk_xxxxxxx) | |
| api_key = os.getenv("GROQ_API_KEY") | |
| if not api_key: | |
| raise ValueError( | |
| "β GROQ_API_KEY not found! Please add it in your Hugging Face Space β Settings β Secrets." | |
| ) | |
| client = Groq(api_key=api_key) | |
| # ----------------------------- | |
| # STEP 2: Whisper Transcription | |
| # ----------------------------- | |
| def transcribe_audio(audio_path): | |
| """Transcribe audio using Groq Whisper model.""" | |
| try: | |
| with open(audio_path, "rb") as f: | |
| transcription = client.audio.transcriptions.create( | |
| model="whisper-large-v3", | |
| file=f | |
| ) | |
| return transcription.text.strip() | |
| except Exception as e: | |
| return f"[Transcription error] {e}" | |
| # ----------------------------- | |
| # STEP 3: Language Detection | |
| # ----------------------------- | |
| def detect_language(text): | |
| try: | |
| return detect(text) | |
| except: | |
| return "unknown" | |
| # ----------------------------- | |
| # STEP 4: Translation | |
| # ----------------------------- | |
| def translate_text(text, dest_lang): | |
| if not text.strip(): | |
| return "[Translation error] Empty input text" | |
| try: | |
| translator = GoogleTranslator(source="auto", target=dest_lang) | |
| translated = translator.translate(text) | |
| if translated.strip().lower() == text.strip().lower(): | |
| time.sleep(1) | |
| translated = translator.translate(text) | |
| return translated | |
| except Exception as e: | |
| return f"[Translation error] {e}" | |
| # ----------------------------- | |
| # STEP 5: Text-to-Speech | |
| # ----------------------------- | |
| def text_to_speech(text, lang): | |
| try: | |
| tts = gTTS(text=text, lang=lang) | |
| temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
| tts.save(temp_file.name) | |
| return temp_file.name | |
| except Exception as e: | |
| return f"[TTS error] {e}" | |
| # ----------------------------- | |
| # STEP 6: Full Pipeline | |
| # ----------------------------- | |
| def pipeline_translate(audio, target_lang_hint): | |
| if not audio: | |
| return "[Error] No audio input", None, None, None | |
| transcribed = transcribe_audio(audio) | |
| if transcribed.startswith("[Transcription error]"): | |
| return transcribed, None, None, None | |
| detected = detect_language(transcribed) | |
| # Auto-switch between English and Urdu if auto selected | |
| if target_lang_hint == "auto": | |
| target_lang_hint = "ur" if detected == "en" else "en" | |
| translated = transcribed | |
| if target_lang_hint != detected: | |
| translated = translate_text(transcribed, dest_lang=target_lang_hint) | |
| if translated.startswith("[Translation error]"): | |
| return transcribed, detected, translated, None | |
| audio_output = text_to_speech(translated, target_lang_hint) | |
| if isinstance(audio_output, str) and audio_output.startswith("[TTS error]"): | |
| return transcribed, detected, translated, None | |
| return transcribed, detected, translated, audio_output | |
| # ----------------------------- | |
| # STEP 7: Gradio Interface | |
| # ----------------------------- | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("## π AI Voice Translator") | |
| gr.Markdown("Speak in any language β it will transcribe, translate, and speak it back!") | |
| with gr.Row(): | |
| audio_input = gr.Audio(sources=["microphone"], type="filepath", label="π€ Record Your Speech") | |
| lang_dropdown = gr.Dropdown( | |
| choices=["auto", "en", "ur", "fr", "es", "de", "ar"], | |
| value="auto", | |
| label="π Target Language (auto detects and switches)" | |
| ) | |
| with gr.Row(): | |
| transcribed_text = gr.Textbox(label="π Transcribed Text") | |
| detected_lang = gr.Textbox(label="π Detected Language") | |
| translated_text = gr.Textbox(label="π¬ Translated Text") | |
| audio_output = gr.Audio(label="π Output Speech") | |
| translate_button = gr.Button("π Translate & Speak") | |
| translate_button.click( | |
| pipeline_translate, | |
| inputs=[audio_input, lang_dropdown], | |
| outputs=[transcribed_text, detected_lang, translated_text, audio_output] | |
| ) | |
| # ----------------------------- | |
| # STEP 8: Launch App | |
| # ----------------------------- | |
| if __name__ == "__main__": | |
| demo.launch() | |