Update app.py
Browse files
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 |
-
#
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
|
| 28 |
-
# Build completion with OpenAI using the
|
| 29 |
completion = client.chat.completions.create(
|
| 30 |
model="gpt-3.5-turbo-1106",
|
| 31 |
-
messages=history_openai_format
|
| 32 |
)
|
| 33 |
|
| 34 |
# Extract generated text (response by the assistant) from completion
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 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":
|
| 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
|
| 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)
|
|
|
|
|
|
|
|
|