|
|
|
try: |
|
import whisper, gradio as gr |
|
from gtts import gTTS |
|
from groq import Groq |
|
except: |
|
import whisper, gradio as gr |
|
from gtts import gTTS |
|
from groq import Groq |
|
|
|
import os |
|
import tempfile |
|
|
|
|
|
whisper_model = whisper.load_model("base") |
|
|
|
|
|
GROQ_API_KEY = "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" |
|
client = Groq(api_key=GROQ_API_KEY) |
|
|
|
|
|
def voice_chat(audio_path): |
|
|
|
result = whisper_model.transcribe(audio_path) |
|
user_text = result["text"] |
|
|
|
|
|
response = client.chat.completions.create( |
|
messages=[{"role": "user", "content": user_text}], |
|
model="llama3-8b-8192", |
|
) |
|
bot_reply = response.choices[0].message.content |
|
|
|
|
|
tts = gTTS(bot_reply) |
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f: |
|
tts.save(f.name) |
|
audio_response_path = f.name |
|
|
|
return user_text, bot_reply, audio_response_path |
|
|
|
|
|
iface = gr.Interface( |
|
fn=voice_chat, |
|
inputs=gr.Microphone(label="π€ Speak your question", type="filepath"), |
|
outputs=[ |
|
gr.Text(label="π Transcribed Input"), |
|
gr.Text(label="π€ LLM Reply"), |
|
gr.Audio(label="π Spoken Reply", type="filepath") |
|
], |
|
title="π£οΈ Real-Time Voice-to-Voice Chatbot (Whisper + Groq + gTTS)", |
|
live=True |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|