ggggtp1 / v2.py
Excel Espina
Initial commit
f60f8fd
raw
history blame
1.96 kB
import openai, random, time
import gradio as gr
openai.api_key = "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"
with open('prompt.txt') as f:
prompt = f.read()
messages = [
{"role": "system", "content": prompt},
]
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, history):
return "", history + [[user_message, None]]
def gpt_chatbot(input):
if input:
messages_with_formatting = []
for line in messages:
if line['role'] == "user":
messages_with_formatting.append({"role": "user", "content": "Them: " + line['content']})
if line['role'] == "assistant":
messages_with_formatting.append({"role": "assistant", "content": "You: " + line['content']})
if line['role'] == "system":
messages_with_formatting.append({"role": "system", "content": line['content']})
messages_with_formatting.append({"role": "user", "content": "Them: " + input})
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages_with_formatting
)
reply = chat.choices[0].message.content
# Remove the "You: " prefix from the assistant's response
if reply.startswith("You: "):
reply = reply[4:].strip()
return reply
def bot(history):
input = history[-1][0]
messages.append({"role": "user", "content": input})
bot_message = gpt_chatbot(input)
messages.append({"role": "assistant", "content": bot_message})
history[-1][1] = bot_message
return history
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch(share=True)