Spaces:
Sleeping
Sleeping
import gradio as gr | |
from gtts import gTTS | |
import openai | |
import speech_recognition as sr | |
import os | |
# Set OpenAI API Key | |
openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your OpenAI API Key | |
# Text-to-Speech Function | |
def text_to_speech(response_text): | |
tts = gTTS(response_text, lang="en") | |
audio_file = "response.mp3" | |
tts.save(audio_file) | |
return audio_file | |
# Speech Recognition Function | |
def speech_to_text(audio_file): | |
recognizer = sr.Recognizer() | |
with sr.AudioFile(audio_file) as source: | |
audio_data = recognizer.record(source) | |
try: | |
return recognizer.recognize_google(audio_data) | |
except sr.UnknownValueError: | |
return "I'm sorry, I couldn't understand that. Could you repeat?" | |
except sr.RequestError: | |
return "There was an error with the speech recognition service." | |
# Chatbot Logic using OpenAI GPT | |
def chatbot_response(user_input): | |
try: | |
response = openai.Completion.create( | |
engine="text-davinci-003", # Use a powerful GPT model | |
prompt=f"User: {user_input}\nChatbot:", | |
max_tokens=150, | |
temperature=0.7, | |
) | |
return response.choices[0].text.strip() | |
except Exception as e: | |
return f"Error generating response: {e}" | |
# Gradio Interface Logic | |
def process_interaction(audio_file): | |
# Convert user speech to text | |
user_text = speech_to_text(audio_file) | |
if "Error" in user_text or "sorry" in user_text: | |
return user_text, None | |
# Get chatbot response | |
chatbot_reply = chatbot_response(user_text) | |
# Convert chatbot reply to speech | |
chatbot_audio = text_to_speech(chatbot_reply) | |
return chatbot_reply, chatbot_audio | |
# Gradio Interface | |
interface = gr.Interface( | |
fn=process_interaction, | |
inputs=gr.Audio(source="microphone", type="filepath"), | |
outputs=[gr.Textbox(label="Chatbot Reply"), gr.Audio(label="Chatbot Voice Reply")], | |
title="Face-to-Face Chatbot", | |
description="Talk to this chatbot like you're having a real conversation! Speak into your microphone to start.", | |
live=True, | |
) | |
if __name__ == "__main__": | |
interface.launch() | |