PhilSpiel commited on
Commit
698100a
·
1 Parent(s): 0b627aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -19,24 +19,25 @@ history_openai_format = [
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"
@@ -46,7 +47,7 @@ def generate_speech(name, input_text):
46
  "xi-api-key": elevenlabs_api_key
47
  }
48
  data = {
49
- "text": message_content,
50
  "model_id": "eleven_multilingual_v2",
51
  "voice_settings": {
52
  "stability": 1.0,
@@ -63,7 +64,7 @@ def generate_speech(name, input_text):
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
@@ -83,7 +84,4 @@ iface = gr.Interface(
83
  )
84
 
85
  # Launch the interface
86
- iface.launch(show_api=False)
87
-
88
- # Clear the conversation history when the interface is closed or interrupted
89
- history_openai_format.clear() # This line will reset the history when the script is stopped
 
19
  def generate_speech(name, input_text):
20
  global history_openai_format # Use the global history variable to maintain state
21
 
22
+ # Append user message to history
23
+ if name:
24
+ input_text = f"Welcome to the Tonight Show, {name}. " + input_text
25
 
26
+ # Add user's input to the history
27
+ history_openai_format.append({"role": "user", "content": input_text})
28
 
29
+ # Build completion with OpenAI using the accumulated history
30
  completion = client.chat.completions.create(
31
  model="gpt-3.5-turbo-1106",
32
+ messages=history_openai_format
33
  )
34
 
35
  # Extract generated text (response by the assistant) from completion
36
+ message_parts = completion.choices[0].message.content.split("\n")
37
+ assistant_message = message_parts[-1].strip() # Get the last line which is the assistant's response
38
 
39
+ # Append assistant's message to history
40
+ history_openai_format.append({"role": "assistant", "content": assistant_message})
 
41
 
42
  # Use ElevenLabs TTS API settings and request for the latest assistant response
43
  url = "https://api.elevenlabs.io/v1/text-to-speech/eIH76iW9yjOWnOKIkd6y/stream"
 
47
  "xi-api-key": elevenlabs_api_key
48
  }
49
  data = {
50
+ "text": assistant_message,
51
  "model_id": "eleven_multilingual_v2",
52
  "voice_settings": {
53
  "stability": 1.0,
 
64
  # Send the request to ElevenLabs API
65
  response = requests.post(url, json=data, headers=headers)
66
 
67
+ # Return the response content if successful, otherwise print error details
68
  if response.status_code == 200:
69
  # Return only the audio of the latest assistant message
70
  return response.content
 
84
  )
85
 
86
  # Launch the interface
87
+ iface.launch(show_api=False)