Advertisement
Ra7eN

AI that responds with speech.

Sep 29th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.46 KB | Source Code | 0 0
  1. # create a a.i. gf that talks
  2. # updated from https://www.youtube.com/watch?v=5htSGLCroc8
  3. import time
  4. import openai
  5. from langchain_openai import OpenAI  # Updated import for OpenAI
  6. from langchain_core.prompts import PromptTemplate  # Updated import for PromptTemplate
  7. from langchain.memory import ConversationBufferWindowMemory  # Correct memory import
  8. from langchain.chains import LLMChain  # Revert to using LLMChain for correct usage
  9. from flask import Flask, render_template, request
  10. from dotenv import find_dotenv, load_dotenv
  11. import requests
  12. from playsound import playsound
  13. import os
  14. import textwrap  # Make sure to import this module
  15.  
  16.  
  17.  
  18.  
  19. # Load environment variables
  20. load_dotenv(find_dotenv())
  21.  
  22. ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
  23.  
  24. def get_voice_message(message):
  25.     # Ensure ELEVENLABS_API_KEY is loaded and not None
  26.     if not ELEVENLABS_API_KEY:
  27.         print("Error: ELEVENLABS_API_KEY is not set. Please check your environment variables.")
  28.         return None
  29.    
  30.     # Split the message into smaller chunks, each with a maximum length of 5000 characters
  31.     message_chunks = textwrap.wrap(message, 5000)
  32.    
  33.     for chunk_index, chunk in enumerate(message_chunks):
  34.         payload = {
  35.             "text": chunk,
  36.             "model_id": "eleven_monolingual_v1",
  37.             "voice_settings": {
  38.                 "stability": 0,
  39.                 "similarity_boost": 0
  40.             }
  41.         }
  42.  
  43.         headers = {
  44.             'accept': 'audio/mpeg',
  45.             'xi-api-key': ELEVENLABS_API_KEY,
  46.             'Content-Type': 'application/json'
  47.         }
  48.  
  49.         # Make the POST request to ElevenLabs API
  50.         # voice id: 21m00Tcm4TlvDq8ikWAM
  51.         # you can get voice id's from here: https://api.elevenlabs.io/v1/voices
  52.         response = requests.post('https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM/stream', json=payload, headers=headers)
  53.        
  54.         if response.status_code == 200 and response.content:
  55.             # Save each chunk's audio with a unique name
  56.             audio_file = f'audio_chunk_{chunk_index}.mp3'
  57.             with open(audio_file, 'wb') as f:
  58.                 f.write(response.content)
  59.             playsound(audio_file)
  60.             os.remove(audio_file)  # Clean up after playing
  61.         else:
  62.             print(f"Error with ElevenLabs API: {response.status_code}, {response.text}")
  63.             return None
  64.  
  65.  
  66. def get_response_from_ai(human_input, max_retries=5):
  67.     template = """
  68.   put your descriptions here
  69.    
  70.    {history}
  71.    boyfriend: {human_input}
  72.    girlfriend:
  73.    """
  74.  
  75.     prompt = PromptTemplate(
  76.         input_variables=["history", "human_input"],  
  77.         template=template
  78.     )
  79.  
  80.     # Initialize memory with the conversation history
  81.     memory = ConversationBufferWindowMemory(k=2)  
  82.  
  83.     llm = OpenAI(temperature=0.2, max_tokens=100)
  84.     chatgpt_chain = LLMChain(
  85.         llm=llm,
  86.         prompt=prompt,
  87.         memory=memory,  # Pass memory directly into LLMChain
  88.         verbose=True
  89.     )
  90.  
  91.     retries = 0
  92.     while retries < max_retries:
  93.         try:
  94.             output = chatgpt_chain.predict(human_input=human_input)
  95.             response_message = output.split("Brig-id:")[-1].strip()  # Only return the response after "Brig-id:"
  96.            
  97.             # Call the get_voice_message function to play the response as speech
  98.             get_voice_message(response_message)
  99.            
  100.             return response_message
  101.         except openai.error.RateLimitError:
  102.             retries += 1
  103.             wait_time = 2 ** retries  # Exponential backoff
  104.             print(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
  105.             time.sleep(wait_time)
  106.         except Exception as e:
  107.             print(f"An unexpected error occurred: {e}")
  108.             break
  109.  
  110.     raise Exception("API call failed after maximum retries")
  111.  
  112. # Build web GUI using Flask
  113. app = Flask(__name__)
  114.  
  115. @app.route("/")
  116. def home():
  117.     return render_template("index.html")
  118.    
  119. @app.route('/send_message', methods=['POST'])
  120. def send_message():
  121.     human_input = request.form['human_input']
  122.    
  123.     # Retrieve max_retries from the form, with a default value of 5 if not provided
  124.     max_retries = request.form.get('max_retries', default=5, type=int)
  125.    
  126.     # Call the function with the human_input and max_retries
  127.     message = get_response_from_ai(human_input, max_retries=max_retries)
  128.     return message
  129.    
  130. if __name__ == "__main__":
  131.     app.run(debug=True)
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement