Update app.py
Browse files
app.py
CHANGED
|
@@ -18,6 +18,24 @@ history_openai_format = [
|
|
| 18 |
# Define the function that takes a name and text input to generate the speech
|
| 19 |
def generate_speech(name, input_text):
|
| 20 |
global history_openai_format # Use the global history variable to maintain state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Append user message to history with the name included
|
| 23 |
input_text1 = f"I'm {name}. " + input_text
|
|
@@ -59,6 +77,12 @@ def generate_speech(name, input_text):
|
|
| 59 |
else:
|
| 60 |
transcript += f"Johnny: {msg['content']}\n\n" # Change 'Assistant' to 'Johnny'
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
# Return the binary audio data and the transcript
|
| 63 |
return response.content, transcript
|
| 64 |
|
|
@@ -66,7 +90,7 @@ def generate_speech(name, input_text):
|
|
| 66 |
iface = gr.Interface(
|
| 67 |
fn=generate_speech,
|
| 68 |
inputs=[
|
| 69 |
-
gr.Textbox(label="Your Name (
|
| 70 |
gr.Textbox(label="Your question or comment for Johnny:")
|
| 71 |
],
|
| 72 |
outputs=[gr.Audio(autoplay=True, label="Johnny's response:"), gr.TextArea(label="Transcript", autoscroll="True", show_copy_button="True")],
|
|
|
|
| 18 |
# Define the function that takes a name and text input to generate the speech
|
| 19 |
def generate_speech(name, input_text):
|
| 20 |
global history_openai_format # Use the global history variable to maintain state
|
| 21 |
+
|
| 22 |
+
# Get dateTime string to build a filename reflecting the UserID + Timestamp
|
| 23 |
+
dt = datetime.now()
|
| 24 |
+
dt_string = str(dt)
|
| 25 |
+
|
| 26 |
+
# Define the user ID and construct the filename for the user's history file
|
| 27 |
+
user_id = name if name else "unidentified" # User identification, typically an email.
|
| 28 |
+
user_hist_file = "files/gp-" + user_id + ".txt" # Filename where the user history will be stored.
|
| 29 |
+
|
| 30 |
+
# Check if the user's history file exists
|
| 31 |
+
if os.path.exists(user_hist_file):
|
| 32 |
+
# If it exists, open and read its contents, then print it
|
| 33 |
+
with open(user_hist_file, "r", encoding="UTF-8") as file:
|
| 34 |
+
user_hist = file.read().strip() # Remove leading/trailing whitespace
|
| 35 |
+
else:
|
| 36 |
+
# If it does not exist, create the file and initialize it with the user ID
|
| 37 |
+
with open(user_hist_file, "w", encoding="UTF-8") as file:
|
| 38 |
+
file.write("User ID: " + user_id)
|
| 39 |
|
| 40 |
# Append user message to history with the name included
|
| 41 |
input_text1 = f"I'm {name}. " + input_text
|
|
|
|
| 77 |
else:
|
| 78 |
transcript += f"Johnny: {msg['content']}\n\n" # Change 'Assistant' to 'Johnny'
|
| 79 |
|
| 80 |
+
# Write the user and assistant messages to the history file after the exchange
|
| 81 |
+
with open(user_hist_file, "a+", encoding="UTF-8") as file:
|
| 82 |
+
file.write("\n\nDate/Time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
| 83 |
+
for message in history_openai_format[-2:]: # Last 2 messages include the user and assistant responses
|
| 84 |
+
file.write(f"\n{message['role'].title()}: {message['content']}")
|
| 85 |
+
|
| 86 |
# Return the binary audio data and the transcript
|
| 87 |
return response.content, transcript
|
| 88 |
|
|
|
|
| 90 |
iface = gr.Interface(
|
| 91 |
fn=generate_speech,
|
| 92 |
inputs=[
|
| 93 |
+
gr.Textbox(label="Your Name (REQUIRED):", placeholder="Enter your FIRST NAME"),
|
| 94 |
gr.Textbox(label="Your question or comment for Johnny:")
|
| 95 |
],
|
| 96 |
outputs=[gr.Audio(autoplay=True, label="Johnny's response:"), gr.TextArea(label="Transcript", autoscroll="True", show_copy_button="True")],
|