Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from groq import Groq | |
| from gtts import gTTS | |
| import tempfile | |
| import os | |
| # ---------------- CONFIG ---------------- | |
| # On Hugging Face Spaces, add your Groq API key under Settings β Secrets β GROQ_API_KEY | |
| client = Groq(api_key=os.getenv("GROQ_API_KEY")) | |
| # Store last few messages for context | |
| memory = [] | |
| # ---------------- LLM BOT ---------------- | |
| def llm_bot(prompt): | |
| # Use last few turns of context for continuity | |
| chat_history = "\n".join([f"{r[0]}: {r[1]}" for r in memory[-6:]]) | |
| completion = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=[ | |
| {"role": "system", "content": "You are a friendly and helpful AI voice assistant."}, | |
| {"role": "user", "content": f"{chat_history}\nUser: {prompt}"} | |
| ], | |
| ) | |
| return completion.choices[0].message.content.strip() | |
| # ---------------- MAIN FUNCTION ---------------- | |
| def speech_to_speech(audio): | |
| if audio is None: | |
| return "Please speak something!", None | |
| # Step 1: Transcribe speech to text using Groq Whisper | |
| with open(audio, "rb") as f: | |
| transcription = client.audio.transcriptions.create( | |
| file=("speech.wav", f.read()), | |
| model="whisper-large-v3", | |
| ) | |
| user_text = transcription.text.strip() | |
| # Step 2: Generate LLM response | |
| response = llm_bot(user_text) | |
| memory.append(("User", user_text)) | |
| memory.append(("Bot", response)) | |
| # Step 3: Convert LLM response to speech | |
| tts = gTTS(response) | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio: | |
| tts.save(temp_audio.name) | |
| audio_out = temp_audio.name | |
| # Step 4: Return text + audio | |
| return f"π£οΈ You said: {user_text}\n\nπ€ Bot: {response}", audio_out | |
| # ---------------- GRADIO INTERFACE ---------------- | |
| with gr.Blocks(title="Groq Voice Chatbot ποΈ") as demo: | |
| gr.Markdown("## π§ Groq-Powered Voice Assistant") | |
| gr.Markdown("Speak naturally β it listens, thinks, and replies with speech using LLaMA-3.1 and Whisper on Groq!") | |
| mic = gr.Audio(sources=["microphone"], type="filepath", label="ποΈ Speak Here") | |
| output_text = gr.Textbox(label="π§ Transcription + Bot Response") | |
| output_audio = gr.Audio(label="π Bot Voice Reply") | |
| submit_btn = gr.Button("π¬ Talk") | |
| submit_btn.click( | |
| fn=speech_to_speech, | |
| inputs=[mic], | |
| outputs=[output_text, output_audio] | |
| ) | |
| demo.launch() |