import os import whisper from gtts import gTTS import gradio as gr from groq import Groq # Load the Whisper model for speech-to-text whisper_model = whisper.load_model("large") # Initialize Groq client for text generation GROQ_API_KEY = "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" client = Groq(api_key=GROQ_API_KEY) # Function to convert text to speech using gTTS and return the audio path def text_to_speech(text): tts = gTTS(text=text, lang='en') response_audio_path = "response.mp3" tts.save(response_audio_path) return response_audio_path # Function to transcribe audio to text using Whisper def transcribe_audio(audio): print("Transcribing audio...") result = whisper_model.transcribe(audio) return result["text"] # Function to get a response from the Groq API using LLaMA model def get_response_from_groq(input_text): chat_completion = client.chat.completions.create( messages=[{"role": "user", "content": input_text}], model="llama3-8b-8192", ) response = chat_completion.choices[0].message.content return response # Gradio function to handle chatbot interaction def chatbot(audio): # Step 1: Transcribe audio to text using Whisper user_input = transcribe_audio(audio) print(f"User said: {user_input}") # Step 2: Get chatbot response from Groq (LLaMA model) response = get_response_from_groq(user_input) print(f"Chatbot response: {response}") # Step 3: Convert response text to speech and return audio response_audio_path = text_to_speech(response) # Ensure the audio file exists and return text + audio response if os.path.exists(response_audio_path): return response, response_audio_path else: return response, None # Fallback in case the audio file wasn't generated # Gradio interface for the chatbot interface = gr.Interface( fn=chatbot, inputs=gr.Audio(type="filepath"), # Use microphone input without the 'source' argument outputs=["text", gr.Audio(type="filepath")], # Text and audio output live=True, title="Voice-Enabled Chatbot", description="Speak into your microphone, and the chatbot will respond with both text and audio." ) # Launch Gradio interface interface.launch(share=True)