LLaMAListens / app.py
safiaa02's picture
Update app.py
25eb9f0 verified
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()