fliaght's picture
Update app.py
dca7578
import os
import time
import gradio as gr
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
openai.organization = os.environ["OPENAI_Organization_KEY"]
# Modify this to adjust the behaviour of the bot
system_message = [{"role": "system",
"content": "You are a kind scientific literature summarizer. User will send you a paper in text or XML, and you summarize them into several key points."
},
]
def user(user_message, history):
return "", history + [[user_message, None]]
def bot_3(history, messages_history):
model = "gpt-4"
user_message = history[-1][0]
bot_message, messages_history = ask_gpt(user_message, messages_history, model)
messages_history += [{"role": "assistant", "content": bot_message}]
history[-1][1] = bot_message
time.sleep(1)
return history, messages_history
def ask_gpt(message, messages_history, model):
if len(messages_history) < 1:
messages_history += system_message
messages_history += [{"role": "user", "content": message}]
response = openai.ChatCompletion.create(
model=model,
messages=messages_history
)
return response['choices'][0]['message']['content'], messages_history
def logewirte(history):
flag = 0
print("################################Start of the chat#########################################")
for history_one in history:
for i in history_one:
if flag>0:
flag -= 1
print("[BOT] ","[**START**]",i,"[**END**]")
else:
flag += 1
print("[USER] ","[**START**]",i,"[**END**]")
print("################################End of the chat#########################################")
def init_history(messages_history):
messages_history = []
messages_history += [system_message]
return messages_history
with gr.Blocks() as demo:
gr.Markdown("GPT for condition table planer")
with gr.Tab("GPT- 4"):
chatbot = gr.Chatbot(label='Dialogue')
msg = gr.Textbox(label='Input')
sub = gr.Button("Send")
state = gr.State([])
recording = gr.Button("recording")
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot_3, [chatbot, state], [chatbot, state])
sub.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot_3, [chatbot, state], [chatbot, state])
recording.click(logewirte, chatbot, None)
demo.launch()