import gradio as gr import os import whisper from gtts import gTTS from groq import Groq # Set up Groq API client client = Groq(api_key="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") # Load Whisper model model = whisper.load_model("base") def chatbot(audio): # Transcribe the audio input using Whisper transcription = model.transcribe(audio) user_input = transcription["text"] # Generate a response using Llama 8B via Groq API chat_completion = client.chat.completions.create( messages=[ { "role": "user", "content": user_input, } ], model="llama3-8b-8192", ) response_text = chat_completion.choices[0].message.content # Convert the response text to speech using gTTS tts = gTTS(text=response_text, lang='en') response_audio_path = "response.mp3" tts.save(response_audio_path) return response_text, response_audio_path # Create a custom interface #css = ".gradio-container {background: rgb(0, 166, 228)}" def build_interface(): with gr.Blocks() as demo: gr.Markdown( """
Talk to the AI-powered chatbot and get responses in real-time. Start by recording your voice.
""" ) with gr.Row(): with gr.Column(scale=1): audio_input = gr.Audio(type="filepath", label="Record or Upload Your Voice") submit_button = gr.Button("Submit") with gr.Column(scale=2): chatbot_output_text = gr.Textbox(label="Chatbot Response") chatbot_output_audio = gr.Audio(label="Audio Response") submit_button.click(chatbot, inputs=audio_input, outputs=[chatbot_output_text, chatbot_output_audio]) gr.Markdown( """Developed by Shahid Hussain
""" ) return demo # Launch the interface if __name__ == "__main__": interface = build_interface() interface.launch()