File size: 1,621 Bytes
acb8d17
 
 
 
 
 
4d13ea4
acb8d17
 
4d13ea4
 
 
acb8d17
4d13ea4
acb8d17
 
 
4d13ea4
acb8d17
 
 
 
 
 
 
 
 
 
 
 
 
 
4d13ea4
acb8d17
4d13ea4
acb8d17
 
 
 
 
 
4d13ea4
acb8d17
 
 
 
4d13ea4
 
acb8d17
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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()