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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -1,46 +1,45 @@
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
8
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
9
 
10
  # Define the function that generates the speech
11
  def generate_speech(input_text):
12
- # Build completion with OpenAI
13
  completion = client.chat.completions.create(
14
- model="gpt-3.5-turbo-1106",
15
- messages=[
16
- {"role": "system",
17
- "content": "You are Johnny Carson, interviewing a guest (the user) on the Tonight Show in 1978. While chatting with the user, you make occasional jokes using Johnny Carson's characteristic humor. Your knowledge of the world ends in 1978."},
18
- {"role": "user", "content": input_text}
 
 
19
  ]
20
  )
21
 
22
  # Extract generated text (response by the assistant) from OpenAI's API response
23
  message_content = completion.choices[0].message.content.strip() # Remove whitespace from the ends
24
 
25
- # Prepare the text for TTS conversion (you may want to process the text further)
26
- #text_to_speech = message_content
27
-
28
  response = client.audio.speech.create(
29
- model="tts-1",
30
- voice="alloy",
31
- input=str(message_content)
32
- )
33
 
34
-
35
  return response.content
36
-
37
- # Define the Gradio interface
38
  iface = gr.Interface(
39
- fn=generate_speech,
40
- inputs=gr.Textbox(),
41
- outputs=gr.Audio(autoplay=True), # Setting autoplay to True
42
- live=False
43
  )
44
 
45
  # Launch the interface
46
- iface.launch(show_api=False)
 
1
+ # Import necessary libraries
2
+ import gradio as gr # Gradio for creating simple UI for functions
3
+ import os # For accessing environment variables
4
+ from openai import OpenAI # OpenAI API client
 
5
 
6
  # Initialize OpenAI API client with API key
7
  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
34
  return response.content
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