PhilSpiel commited on
Commit
7ff0301
·
1 Parent(s): 8ea0861

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -17,29 +17,28 @@ history_openai_format = [
17
 
18
  # Define the function that takes a name and text input to generate the speech
19
  def generate_speech(name, input_text):
 
 
20
  # Include the user's name in the dialogue if provided
21
  personalized_greeting = f"Welcome to the Tonight Show, {name}. " if name else ""
22
 
23
  # Append user message to history with personalized greeting
24
- history_openai_format.append({"role": "user", "content": personalized_greeting + input_text})
25
 
26
- # Build completion with OpenAI using the accumulated history
27
  completion = client.chat.completions.create(
28
  model="gpt-3.5-turbo-1106",
29
- messages=history_openai_format
30
  )
31
 
32
  # Extract generated text (response by the assistant) from completion
33
  message_content = completion.choices[0].message.content.strip()
34
 
35
- # Append assistant's message to history
36
- # NOTE: This is crucial for keeping memory of the conversation.
37
- history_openai_format.append({"role": "assistant", "content": message_content})
38
-
39
- # Prepare the text for TTS conversion of just the latest reply, NOT the entire conversation
40
- text_to_speech = message_content # Only the latest message from the assistant is used
41
 
42
- # ElevenLabs TTS API settings and request for the latest response
43
  url = "https://api.elevenlabs.io/v1/text-to-speech/eIH76iW9yjOWnOKIkd6y/stream"
44
  headers = {
45
  "Accept": "audio/mpeg",
@@ -61,10 +60,12 @@ def generate_speech(name, input_text):
61
  }
62
  }
63
 
 
64
  response = requests.post(url, json=data, headers=headers)
65
 
66
- # Return the response content directly if successful, otherwise print error details
67
  if response.status_code == 200:
 
68
  return response.content
69
  else:
70
  print("Error with ElevenLabs API:", response.status_code, response.text)
 
17
 
18
  # Define the function that takes a name and text input to generate the speech
19
  def generate_speech(name, input_text):
20
+ global history_openai_format # Use the global history variable to maintain state
21
+
22
  # Include the user's name in the dialogue if provided
23
  personalized_greeting = f"Welcome to the Tonight Show, {name}. " if name else ""
24
 
25
  # Append user message to history with personalized greeting
26
+ current_exchange = [{"role": "user", "content": personalized_greeting + input_text}]
27
 
28
+ # Build completion with OpenAI using the current exchange and the accumulated history
29
  completion = client.chat.completions.create(
30
  model="gpt-3.5-turbo-1106",
31
+ messages=history_openai_format + current_exchange
32
  )
33
 
34
  # Extract generated text (response by the assistant) from completion
35
  message_content = completion.choices[0].message.content.strip()
36
 
37
+ # Update the conversation history with the current exchange
38
+ # This ensures the full history is preserved for the next call but only the latest messages are sent to TTS
39
+ history_openai_format.extend(current_exchange + [{"role": "assistant", "content": message_content}])
 
 
 
40
 
41
+ # Use ElevenLabs TTS API settings and request for the latest assistant response
42
  url = "https://api.elevenlabs.io/v1/text-to-speech/eIH76iW9yjOWnOKIkd6y/stream"
43
  headers = {
44
  "Accept": "audio/mpeg",
 
60
  }
61
  }
62
 
63
+ # Send the request to ElevenLabs API
64
  response = requests.post(url, json=data, headers=headers)
65
 
66
+ # Return the response content if successful, otherwise, print error details
67
  if response.status_code == 200:
68
+ # Return only the audio of the latest assistant message
69
  return response.content
70
  else:
71
  print("Error with ElevenLabs API:", response.status_code, response.text)