Update app.py
Browse files
app.py
CHANGED
|
@@ -8,26 +8,26 @@ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
| 8 |
|
| 9 |
# Define the function that generates the speech
|
| 10 |
def generate_speech(input_text):
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
| 12 |
completion = client.chat.completions.create(
|
| 13 |
-
model="gpt-3.5-turbo-1106",
|
| 14 |
-
messages=
|
| 15 |
-
{
|
| 16 |
-
"role": "system", # System message with instructions for the AI
|
| 17 |
-
"content": (os.getenv("PROMPT"))
|
| 18 |
-
},
|
| 19 |
-
{"role": "user", "content": input_text} # The user's input message
|
| 20 |
-
]
|
| 21 |
)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
message_content = completion.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Use OpenAI's text-to-speech API to convert the text response to audio
|
| 27 |
response = client.audio.speech.create(
|
| 28 |
-
model="tts-1",
|
| 29 |
-
voice="alloy",
|
| 30 |
-
input=str(message_content)
|
| 31 |
)
|
| 32 |
|
| 33 |
# Return the binary audio data received from OpenAI
|
|
@@ -35,11 +35,11 @@ def generate_speech(input_text):
|
|
| 35 |
|
| 36 |
# Define the Gradio interface with our function
|
| 37 |
iface = gr.Interface(
|
| 38 |
-
fn=generate_speech,
|
| 39 |
-
inputs=gr.Textbox(
|
| 40 |
-
outputs=gr.Audio(autoplay=True),
|
| 41 |
-
live=False
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the interface
|
| 45 |
-
iface.launch(show_api=False)
|
|
|
|
| 8 |
|
| 9 |
# Define the function that generates the speech
|
| 10 |
def generate_speech(input_text):
|
| 11 |
+
# Append user message to history
|
| 12 |
+
history_openai_format.append({"role": "user", "content": input_text})
|
| 13 |
+
|
| 14 |
+
# Build completion with OpenAI using the accumulated history
|
| 15 |
completion = client.chat.completions.create(
|
| 16 |
+
model="gpt-3.5-turbo-1106",
|
| 17 |
+
messages=history_openai_format
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Get the assistant's message from the completion
|
| 21 |
+
message_content = completion.choices[0].message.content.strip()
|
| 22 |
+
|
| 23 |
+
# Append assistant's message to history
|
| 24 |
+
history_openai_format.append({"role": "assistant", "content": message_content})
|
| 25 |
|
| 26 |
# Use OpenAI's text-to-speech API to convert the text response to audio
|
| 27 |
response = client.audio.speech.create(
|
| 28 |
+
model="tts-1",
|
| 29 |
+
voice="alloy",
|
| 30 |
+
input=str(message_content)
|
| 31 |
)
|
| 32 |
|
| 33 |
# Return the binary audio data received from OpenAI
|
|
|
|
| 35 |
|
| 36 |
# Define the Gradio interface with our function
|
| 37 |
iface = gr.Interface(
|
| 38 |
+
fn=generate_speech,
|
| 39 |
+
inputs=gr.Textbox(label="Your question or comment:"),
|
| 40 |
+
outputs=gr.Audio(autoplay=True),
|
| 41 |
+
live=False
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the interface
|
| 45 |
+
iface.launch(show_api=False)
|