Spaces:
Sleeping
Sleeping
import os | |
import whisper | |
import gradio as gr | |
import requests | |
from gtts import gTTS | |
# Load Whisper model (base is a good balance of speed + quality) | |
model = whisper.load_model("base") | |
# Put your actual Groq API key here | |
GROQ_API_KEY = "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" | |
def transcribe_and_respond(audio_file): | |
# 1. Transcribe audio to text | |
result = model.transcribe(audio_file) | |
user_text = result["text"] | |
# 2. Send to Groq LLM (LLaMA 3.3 70B) | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {GROQ_API_KEY}" | |
} | |
data = { | |
"model": "llama-3.3-70b-versatile", | |
"messages": [{"role": "user", "content": user_text}] | |
} | |
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=data) | |
if response.status_code == 200: | |
output_text = response.json()['choices'][0]['message']['content'] | |
else: | |
output_text = f"Error: {response.status_code} - {response.text}" | |
# 3. Convert reply to speech | |
tts = gTTS(text=output_text, lang='en') | |
tts_path = "response.mp3" | |
tts.save(tts_path) | |
return output_text, tts_path | |
# Gradio interface | |
iface = gr.Interface( | |
fn=transcribe_and_respond, | |
inputs=gr.Audio(type="filepath", label="ποΈ Speak"), | |
outputs=[gr.Textbox(label="π§ LLM Reply"), gr.Audio(label="π Spoken Response")], | |
title="Voice-to-Voice Chatbot (Whisper + Groq + gTTS)", | |
description="Record your voice, get a reply from LLaMA 3.3 70B, and hear it spoken back!" | |
) | |
if __name__ == "__main__": | |
iface.launch() | |