PhilSpiel commited on
Commit
c4e7a85
·
1 Parent(s): 3d43559

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
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
- # Build conversation completion with the OpenAI chat model
 
 
 
12
  completion = client.chat.completions.create(
13
- model="gpt-3.5-turbo-1106", # Specify the model to use
14
- messages=[ # This is the conversation history with user input appended
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
- # Extract generated text (response by the assistant) from OpenAI's API response
24
- message_content = completion.choices[0].message.content.strip() # Remove whitespace from the ends
 
 
 
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", # Specify the TTS model to use
29
- voice="alloy", # Specify the voice model to use
30
- input=str(message_content) # The text we want to convert to speech
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, # Function to invoke
39
- inputs=gr.Textbox(), # Input element in the UI: a textbox for user text
40
- outputs=gr.Audio(autoplay=True), # Output element in the UI: an audio player
41
- live=False # Whether or not to automatically call the function on input change
42
  )
43
 
44
  # Launch the interface
45
- iface.launch(show_api=False) # Start the Gradio UI; hide the API schema part of the interface
 
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)