Spaces:
Sleeping
Sleeping
| import os | |
| import uuid | |
| import whisper | |
| from groq import Groq | |
| import gradio as gr | |
| from gtts import gTTS | |
| # ------------------------------- | |
| # Groq API Key (set in HF Secrets) | |
| # ------------------------------- | |
| client = Groq(api_key=os.environ["GROQ_API_KEY"]) | |
| # ------------------------------- | |
| # Load Whisper model | |
| # ------------------------------- | |
| stt_model = whisper.load_model("base") | |
| # ------------------------------- | |
| # Voice-to-Voice function | |
| # ------------------------------- | |
| def voice_to_voice(audio_path): | |
| try: | |
| # 1️⃣ Speech-to-Text | |
| text = stt_model.transcribe(audio_path)["text"] | |
| # 2️⃣ LLM response via Groq | |
| response = client.chat.completions.create( | |
| model="llama-3.3-70b-versatile", | |
| messages=[{"role": "user", "content": text}] | |
| ).choices[0].message.content | |
| # 3️⃣ Text-to-Speech (gTTS) | |
| output_file = f"out_{uuid.uuid4().hex}.mp3" | |
| tts = gTTS(text=response, lang='en') | |
| tts.save(output_file) | |
| return output_file | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # ------------------------------- | |
| # Gradio UI | |
| # ------------------------------- | |
| interface = gr.Interface( | |
| fn=voice_to_voice, | |
| inputs=gr.Audio(type="filepath", label="Speak"), | |
| outputs=gr.Audio(type="filepath", label="Assistant Reply"), | |
| title="🎙️ Voice-to-Voice AI Assistant", | |
| description="Whisper → Groq LLaMA-3 → Google TTS (Free & Open Source)" | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |