|
|
import gradio as gr |
|
|
import os |
|
|
from openai import OpenAI |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
dt = datetime.now() |
|
|
dt_string = str(dt) |
|
|
|
|
|
|
|
|
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) |
|
|
|
|
|
|
|
|
history_openai_format = [ |
|
|
{"role": "system", "content": os.getenv("PROMPT")} |
|
|
] |
|
|
|
|
|
|
|
|
def generate_speech(name, input_text): |
|
|
global history_openai_format |
|
|
|
|
|
|
|
|
dt = datetime.now() |
|
|
dt_string = str(dt) |
|
|
|
|
|
|
|
|
user_id = name if name else "unidentified" |
|
|
user_id = user_id.lower() |
|
|
user_id = user_id.replace(" ", "") |
|
|
user_hist_file = "jcTSS-" + user_id + ".txt" |
|
|
|
|
|
|
|
|
if os.path.exists(user_hist_file): |
|
|
|
|
|
with open(user_hist_file, "r", encoding="UTF-8") as file: |
|
|
user_hist = file.read().strip() |
|
|
else: |
|
|
|
|
|
with open(user_hist_file, "w", encoding="UTF-8") as file: |
|
|
file.write("User ID: " + user_id) |
|
|
|
|
|
|
|
|
input_text1 = f"I'm {name}. " + input_text |
|
|
history_openai_format.append({"role": "user", "content": input_text1}) |
|
|
|
|
|
|
|
|
completion = client.chat.completions.create( |
|
|
model="gpt-3.5-turbo-1106", |
|
|
messages=history_openai_format |
|
|
) |
|
|
|
|
|
|
|
|
message_content = completion.choices[0].message.content.strip() |
|
|
|
|
|
|
|
|
if message_content.lower().startswith("johnny"): |
|
|
|
|
|
message_content = message_content[6:].strip() |
|
|
|
|
|
|
|
|
history_openai_format.append({"role": "assistant", "content": message_content}) |
|
|
|
|
|
|
|
|
response = client.audio.speech.create( |
|
|
model="tts-1", |
|
|
voice="onyx", |
|
|
input=str(message_content) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
transcript = "" |
|
|
transcript += "Date/Time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n\n" |
|
|
for i, msg in enumerate(history_openai_format): |
|
|
if msg['role'] != 'system': |
|
|
if msg['role'] == 'user': |
|
|
|
|
|
|
|
|
user_input = msg['content'].split(". ", 1)[1] if ". " in msg['content'] else msg['content'] |
|
|
|
|
|
transcript += f"GUEST: {user_input}\n" |
|
|
else: |
|
|
transcript += f"JOHNNY: {msg['content']}\n\n" |
|
|
|
|
|
|
|
|
with open(user_hist_file, "a+", encoding="UTF-8") as file: |
|
|
file.write("\n\nDate/Time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S")) |
|
|
for message in history_openai_format[-2:]: |
|
|
file.write(f"\n{message['role'].title()}: {message['content']}") |
|
|
|
|
|
|
|
|
return response.content, transcript |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=generate_speech, |
|
|
inputs=[ |
|
|
gr.Textbox(label="Your Name (REQUIRED):", placeholder="Enter your FIRST NAME"), |
|
|
gr.Textbox(label="Your question or comment for Johnny:") |
|
|
], |
|
|
outputs=[gr.Audio(autoplay=True, label="Johnny's response:"), gr.Textbox(label="Transcript", max_lines=12, autoscroll="True", show_copy_button="True")], |
|
|
live=False |
|
|
) |
|
|
|
|
|
|
|
|
iface.launch(show_api=False) |