Help Needed: Integrating GPT and 11Labs for Creating a Digital Human

Hello everyone,

I’m working on a project to create a digital human that can interact via voice. I’m using OpenAI’s GPT-3.5-turbo for generating responses and 11Labs for text-to-speech conversion. I’m using Python and Flask to build the application.

I’m currently encountering an AttributeError: module 'openai' has no attribute 'error' . Any advice on how to resolve this or suggestions for improvement are highly appreciated.

Here is the code I'm working with:`import openai
import speech_recognition as sr
import requests
import json
from flask import Flask, request, jsonify
import sounddevice as sd
import soundfile as sf
import os
import time

# Ensure environment variables are set
openai_api_key = os.getenv('OPENAI_API_KEY') 
eleven_labs_api_key = os.getenv('ELEVEN_LABS_API_KEY') 
eleven_labs_voice_id = os.getenv('ELEVEN_LABS_VOICE_ID')

if not openai_api_key:
    raise ValueError("The environment variable OPENAI_API_KEY is not set.")
if not eleven_labs_api_key:
    raise ValueError("The environment variable ELEVEN_LABS_API_KEY is not set.")
if not eleven_labs_voice_id:
    raise ValueError("The environment variable ELEVEN_LABS_VOICE_ID is not set.")

# Set API key
openai.api_key = openai_api_key

app = Flask(__name__)

# Cache mechanism to store recent requests and responses
cache = {}

def generate_response(user_input):
    if user_input in cache:
        return cache[user_input]
    
    retries = 3
    for attempt in range(retries):
        try:
            response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
                    {"role": "user", "content": user_input}
                ]
            )
            print("OpenAI response:", response)
            result = response.choices[0].message['content']
            cache[user_input] = result
            return result
        except openai.error.RateLimitError as e:
            if attempt < retries - 1:
                print(f"Rate limit exceeded, retrying... ({attempt + 1}/{retries})")
                time.sleep(2 ** attempt)  # Exponential backoff
            else:
                raise e
        except openai.error.OpenAIError as e:
            print(f"OpenAI error: {e}")
            return "Error: Unable to generate response."

def recognize_speech():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        print("Please speak...")
        audio = recognizer.listen(source)
        try:
            text = recognizer.recognize_google(audio, language='en-US')
            print(f"You said: {text}")
            return text
        except sr.UnknownValueError:
            print("Sorry, I could not understand the audio")
            return ""
        except sr.RequestError:
            print("Request error")
            return ""

def generate_speech(text):
    url = "https://api.elevenlabs.io/v1/text-to-speech"
    headers = {
        "xi-api-key": eleven_labs_api_key,
        "Content-Type": "application/json"
    }
    data = {
        "text": text,
        "voice_settings": {
            "voice_id": eleven_labs_voice_id
        }
    }
    response = requests.post(url, headers=headers, data=json.dumps(data))
    print("Eleven Labs response status code:", response.status_code)
    if response.status_code == 200:
        with open("output.wav", "wb") as f:
            f.write(response.content)
        print("Audio file saved as output.wav")
        return "output.wav"
    else:
        print("Error generating speech:", response.text)
        return None

def play_audio(file_path):
    data, fs = sf.read(file_path, dtype='float32')
    sd.play(data, fs)
    sd.wait()

@app.route('/message', methods=['POST'])
def chat():
    if request.is_json:
        data = request.get_json()
        print("Received data:", data)
        user_input = data['message']
        response = generate_response(user_input)
        print("Generated response:", response)
        audio_file = generate_speech(response)
        if audio_file:
            play_audio(audio_file)
        return jsonify({'response': response})
    else:
        return jsonify({'error': 'Unsupported Media Type'}), 415

if __name__ == '__main__':
    print("Starting Flask server...")
    app.run(debug=True)
`