Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,68 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
print("Session ID:", session_opened.session_id)
|
| 38 |
-
|
| 39 |
-
def on_data(self, transcript: aai.RealtimeTranscript):
|
| 40 |
-
if not transcript.text:
|
| 41 |
-
return
|
| 42 |
-
|
| 43 |
-
if isinstance(transcript, aai.RealtimeFinalTranscript):
|
| 44 |
-
self.generate_ai_response(transcript)
|
| 45 |
-
else:
|
| 46 |
-
print(transcript.text, end="\r")
|
| 47 |
-
|
| 48 |
-
def on_error(self, error: aai.RealtimeError):
|
| 49 |
-
print("An error occurred:", error)
|
| 50 |
-
|
| 51 |
-
def on_close(self):
|
| 52 |
-
print("Session closed.")
|
| 53 |
-
|
| 54 |
-
def generate_ai_response(self, transcript):
|
| 55 |
-
self.stop_transcription()
|
| 56 |
-
self.full_transcript.append({"role": "user", "content": transcript.text})
|
| 57 |
-
print(f"\nCustomer: {transcript.text}\n")
|
| 58 |
-
|
| 59 |
-
response = self.openai_client.chat.completions.create(
|
| 60 |
-
model="gpt-3.5-turbo",
|
| 61 |
-
messages=self.full_transcript
|
| 62 |
)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
-
|
| 84 |
-
ai_assistant = AI_Assistant()
|
| 85 |
-
ai_assistant.generate_audio(greeting)
|
| 86 |
-
ai_assistant.start_transcription()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gtts import gTTS
|
| 3 |
+
import openai
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set OpenAI API Key
|
| 8 |
+
openai.api_key = "YOUR_OPENAI_API_KEY" # Replace with your OpenAI API Key
|
| 9 |
+
|
| 10 |
+
# Text-to-Speech Function
|
| 11 |
+
def text_to_speech(response_text):
|
| 12 |
+
tts = gTTS(response_text, lang="en")
|
| 13 |
+
audio_file = "response.mp3"
|
| 14 |
+
tts.save(audio_file)
|
| 15 |
+
return audio_file
|
| 16 |
+
|
| 17 |
+
# Speech Recognition Function
|
| 18 |
+
def speech_to_text(audio_file):
|
| 19 |
+
recognizer = sr.Recognizer()
|
| 20 |
+
with sr.AudioFile(audio_file) as source:
|
| 21 |
+
audio_data = recognizer.record(source)
|
| 22 |
+
try:
|
| 23 |
+
return recognizer.recognize_google(audio_data)
|
| 24 |
+
except sr.UnknownValueError:
|
| 25 |
+
return "I'm sorry, I couldn't understand that. Could you repeat?"
|
| 26 |
+
except sr.RequestError:
|
| 27 |
+
return "There was an error with the speech recognition service."
|
| 28 |
+
|
| 29 |
+
# Chatbot Logic using OpenAI GPT
|
| 30 |
+
def chatbot_response(user_input):
|
| 31 |
+
try:
|
| 32 |
+
response = openai.Completion.create(
|
| 33 |
+
engine="text-davinci-003", # Use a powerful GPT model
|
| 34 |
+
prompt=f"User: {user_input}\nChatbot:",
|
| 35 |
+
max_tokens=150,
|
| 36 |
+
temperature=0.7,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
+
return response.choices[0].text.strip()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error generating response: {e}"
|
| 41 |
+
|
| 42 |
+
# Gradio Interface Logic
|
| 43 |
+
def process_interaction(audio_file):
|
| 44 |
+
# Convert user speech to text
|
| 45 |
+
user_text = speech_to_text(audio_file)
|
| 46 |
+
if "Error" in user_text or "sorry" in user_text:
|
| 47 |
+
return user_text, None
|
| 48 |
+
|
| 49 |
+
# Get chatbot response
|
| 50 |
+
chatbot_reply = chatbot_response(user_text)
|
| 51 |
+
|
| 52 |
+
# Convert chatbot reply to speech
|
| 53 |
+
chatbot_audio = text_to_speech(chatbot_reply)
|
| 54 |
+
|
| 55 |
+
return chatbot_reply, chatbot_audio
|
| 56 |
+
|
| 57 |
+
# Gradio Interface
|
| 58 |
+
interface = gr.Interface(
|
| 59 |
+
fn=process_interaction,
|
| 60 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
| 61 |
+
outputs=[gr.Textbox(label="Chatbot Reply"), gr.Audio(label="Chatbot Voice Reply")],
|
| 62 |
+
title="Face-to-Face Chatbot",
|
| 63 |
+
description="Talk to this chatbot like you're having a real conversation! Speak into your microphone to start.",
|
| 64 |
+
live=True,
|
| 65 |
+
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
interface.launch()
|
|
|
|
|
|
|
|
|