voice_chatbot / app.py
SumbalFatima1122's picture
Create app.py
2d17967 verified
# Importing necessary libraries
import os
import whisper
from groq import Groq
from gtts import gTTS
import gradio as gr
# Step 1: Load the Whisper model
print("Loading Whisper model...")
whisper_model = whisper.load_model("base")
# Step 2: Initialize the Groq API client
GROP_API_KEY = "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"
client = Groq(api_key=GROP_API_KEY)
# Function to transcribe audio using Whisper
def transcribe_audio(audio_file):
print("Transcribing audio...")
result = whisper_model.transcribe(audio_file)
return result['text']
# Function to interact with Groq's LLM
def get_llm_response(user_text):
print(f"Sending request to Groq API with input: {user_text}")
try:
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": user_text}],
model="llama3-8b-8192",
)
print(f"Groq API full response: {chat_completion}")
return chat_completion.choices[0].message.content
except Exception as e:
print(f"Error while fetching response from Groq API: {e}")
return f"Error occurred: {e}"
# Function to convert text to speech using gTTS
def text_to_speech(response_text, output_path="response.mp3"):
print("Converting text to speech...")
tts = gTTS(response_text)
tts.save(output_path)
return output_path
# Complete chat pipeline function
def chat_pipeline(audio_input):
# Step 1: Transcribe audio
transcribed_text = transcribe_audio(audio_input)
# Step 2: Get response from LLM
llm_response = get_llm_response(transcribed_text)
# Step 3: Convert LLM response to audio
response_audio_path = text_to_speech(llm_response)
return transcribed_text, llm_response, response_audio_path
# Gradio Interface
print("Setting up Gradio interface...")
interface = gr.Interface(
fn=chat_pipeline,
inputs=gr.Audio(type="filepath", label="Record or Upload Audio"),
outputs=[
gr.Textbox(label="Transcribed Text"),
gr.Textbox(label="LLM Response"),
gr.Audio(label="Response Audio"),
],
live=True
)
# Launch the Gradio app
print("Launching Gradio app...")
interface.launch()