PhilSpiel commited on
Commit
b87f2a3
·
1 Parent(s): 2bafe3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -1,6 +1,7 @@
1
- import gradio as gr
2
  import os
3
  from openai import OpenAI
 
4
 
5
  # Initialize OpenAI API client with API key
6
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
@@ -10,12 +11,30 @@ history_openai_format = [
10
  {"role": "system", "content": (os.getenv("PROMPT"))}
11
  ]
12
 
13
- # Define the function that takes a name and text input to generate the speech
14
- def generate_speech(name, input_text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  global history_openai_format # Use the global history variable to maintain state
 
16
 
17
  # Append user message to history with the name included
18
- input_text1 = f"I'm {name}. " + input_text
19
  history_openai_format.append({"role": "user", "content": input_text1})
20
 
21
  # Build completion with OpenAI using the accumulated history
@@ -56,16 +75,14 @@ def generate_speech(name, input_text):
56
  # Return the binary audio data and the transcript
57
  return response.content, transcript
58
 
59
- # Define the Gradio interface with inputs for name and user text
60
- iface = gr.Interface(
61
  fn=generate_speech,
62
- inputs=[
63
- gr.Textbox(label="Your Name (required):", placeholder="Enter your FIRST NAME"),
64
- gr.Textbox(label="Your question or comment for Johnny:")
65
- ],
66
  outputs=[gr.Audio(autoplay=True), gr.TextArea(label="Transcript")],
67
  live=False
68
  )
69
 
70
- # Launch the interface
71
- iface.launch(show_api=True)
 
 
1
+ mport gradio as gr
2
  import os
3
  from openai import OpenAI
4
+ from datetime import datetime
5
 
6
  # Initialize OpenAI API client with API key
7
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
 
11
  {"role": "system", "content": (os.getenv("PROMPT"))}
12
  ]
13
 
14
+ # Global variable to store the user's name
15
+ user_name = ""
16
+
17
+ # Define the function that takes a name input
18
+ def set_name(name):
19
+ global user_name # Use the global user_name variable to maintain state
20
+ user_name = name # Set the user_name variable to the input name
21
+ return "Name set successfully!"
22
+
23
+ # Define the Gradio interface with input for name
24
+ iface1 = gr.Interface(
25
+ fn=set_name,
26
+ inputs=gr.Textbox(label="Your Name (required):", placeholder="Enter your FIRST NAME"),
27
+ outputs=gr.Text(label="Result"),
28
+ live=False
29
+ )
30
+
31
+ # Define the function that takes a text input to generate the speech
32
+ def generate_speech(input_text):
33
  global history_openai_format # Use the global history variable to maintain state
34
+ global user_name # Use the global user_name variable to get the user's name
35
 
36
  # Append user message to history with the name included
37
+ input_text1 = f"I'm {user_name}. " + input_text
38
  history_openai_format.append({"role": "user", "content": input_text1})
39
 
40
  # Build completion with OpenAI using the accumulated history
 
75
  # Return the binary audio data and the transcript
76
  return response.content, transcript
77
 
78
+ # Define the Gradio interface with input for user text and outputs
79
+ iface2 = gr.Interface(
80
  fn=generate_speech,
81
+ inputs=gr.Textbox(label="Your question or comment for Johnny:"),
 
 
 
82
  outputs=[gr.Audio(autoplay=True), gr.TextArea(label="Transcript")],
83
  live=False
84
  )
85
 
86
+ # Launch the interfaces
87
+ iface1.launch(show_api=True)
88
+ iface2.launch(show_api=True)