Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
-
import re
|
| 5 |
from openai import OpenAI
|
| 6 |
|
| 7 |
# Initialize OpenAI API client with API key
|
|
@@ -30,14 +29,17 @@ def generate_speech(name, input_text):
|
|
| 30 |
messages=history_openai_format
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# Extract generated text (response by the assistant) from
|
| 34 |
message_content = completion.choices[0].message.content.strip()
|
| 35 |
|
| 36 |
# Append assistant's message to history
|
|
|
|
| 37 |
history_openai_format.append({"role": "assistant", "content": message_content})
|
| 38 |
|
| 39 |
-
# Prepare the text for TTS conversion
|
| 40 |
-
text_to_speech = message_content
|
|
|
|
|
|
|
| 41 |
url = "https://api.elevenlabs.io/v1/text-to-speech/eIH76iW9yjOWnOKIkd6y/stream"
|
| 42 |
headers = {
|
| 43 |
"Accept": "audio/mpeg",
|
|
@@ -73,7 +75,7 @@ iface = gr.Interface(
|
|
| 73 |
fn=generate_speech,
|
| 74 |
inputs=[
|
| 75 |
gr.Textbox(label="Your Name (optional):", placeholder="Enter your FIRST NAME"),
|
| 76 |
-
gr.Textbox(label="Your question or comment:")
|
| 77 |
],
|
| 78 |
outputs=gr.Audio(autoplay=True),
|
| 79 |
live=False
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
|
|
|
| 4 |
from openai import OpenAI
|
| 5 |
|
| 6 |
# Initialize OpenAI API client with API key
|
|
|
|
| 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",
|
|
|
|
| 75 |
fn=generate_speech,
|
| 76 |
inputs=[
|
| 77 |
gr.Textbox(label="Your Name (optional):", placeholder="Enter your FIRST NAME"),
|
| 78 |
+
gr.Textbox(label="Your question or comment for Johnny:")
|
| 79 |
],
|
| 80 |
outputs=gr.Audio(autoplay=True),
|
| 81 |
live=False
|