|
import os |
|
import gradio as gr |
|
import whisper |
|
from groq import Groq |
|
from gtts import gTTS |
|
import tempfile |
|
|
|
|
|
model = whisper.load_model("base") |
|
|
|
|
|
client = Groq(api_key="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") |
|
|
|
|
|
def chat_with_bot(audio_input): |
|
try: |
|
|
|
try: |
|
result = model.transcribe(audio_input) |
|
user_input = result['text'] |
|
except Exception as e: |
|
return "Error during transcription: " + str(e), "", None |
|
|
|
|
|
try: |
|
chat_completion = client.chat.completions.create( |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": user_input, |
|
} |
|
], |
|
model="llama3-8b-8192", |
|
) |
|
response_text = chat_completion.choices[0].message.content |
|
except Exception as e: |
|
return "Error during Groq API call: " + str(e), "", None |
|
|
|
|
|
try: |
|
tts = gTTS(text=response_text, lang='en') |
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f: |
|
tts.save(f.name) |
|
output_audio = f.name |
|
except Exception as e: |
|
return "Error during text-to-speech conversion: " + str(e), "", None |
|
|
|
|
|
return user_input, response_text, output_audio |
|
|
|
except Exception as e: |
|
return "An unexpected error occurred: " + str(e), "", None |
|
|
|
|
|
iface = gr.Interface( |
|
fn=chat_with_bot, |
|
inputs=gr.Audio(type="filepath"), |
|
outputs=[ |
|
gr.Textbox(label="Transcription"), |
|
gr.Textbox(label="Response"), |
|
gr.Audio(label="Generated Speech") |
|
], |
|
live=True, |
|
title="Real-Time Voice-to-Voice Chatbot", |
|
description="Speak into the microphone to chat with the Llama 8B model via Groq API." |
|
) |
|
|
|
|
|
iface.launch() |