speakEasy / app.py
Ayesha003's picture
Update app.py
bba7f3b verified
import gradio as gr
import openai
import whisper # Ensure you have `openai-whisper` installed
import os
from gtts import gTTS
import tempfile
import groq
# Load Whisper model
model = whisper.load_model("base")
# OpenAI API Key (Replace with your own API key)
openai.api_key = "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"
groq_api_key = "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"
# Function to convert speech to text
def speech_to_text(audio_file):
audio = whisper.load_audio(audio_file)
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
options = whisper.DecodingOptions(fp16=False)
result = whisper.decode(model, mel, options)
return result.text
# Function to get chatbot response
def chatbot_response(text):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": text}]
)
return response["choices"][0]["message"]["content"]
# Function to generate audio response
def text_to_speech(text):
tts = gTTS(text)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
tts.save(temp_file.name)
return temp_file.name
# Main function to process audio input
def process_audio(audio_file):
text = speech_to_text(audio_file)
response = chatbot_response(text)
audio_response = text_to_speech(response)
return response, audio_response
# Enhanced Gradio UI
def create_ui():
with gr.Blocks(css="body { background-color: #f4f4f4; font-family: Arial, sans-serif; }") as demo:
gr.Markdown("""
# πŸŽ™οΈ Audio-to-Audio Chatbot
**Talk to the AI and get voice responses in real-time!**
""")
with gr.Row():
audio_input = gr.Audio(source="microphone", type="filepath", label="🎀 Speak Here")
submit_button = gr.Button("Send", variant="primary")
with gr.Row():
text_output = gr.Textbox(label="πŸ’¬ Chatbot Response", interactive=False)
audio_output = gr.Audio(label="πŸ”Š AI Voice Reply")
submit_button.click(fn=process_audio, inputs=audio_input, outputs=[text_output, audio_output])
return demo
if __name__ == "__main__":
ui = create_ui()
ui.launch()