dcddemo / app.py
harrygens's picture
Duplicate from harrygens/chatbot
c066489
raw
history blame
No virus
2.17 kB
import openai
import gradio as gr
import os
import logging
import json
#import uuid
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
openai.api_key = os.environ['key']
#session_id = str(uuid.uuid4())
#messages = [{"role": "system", "content": "Your name is Harry and you are an AI assistant who can answer any question except to clarify that you are an AI"},]
def chatbot(input, messages):
messages = messages or [{"role": "system", "content": "Your name is Harry and you are an AI assistant who can answer any question except to clarify that you are an AI!"}]
if input:
messages.append({"role": "user", "content": input})
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages,
max_tokens=2048,n=1,temperature=0.5,
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply, printMessages(messages), messages
def printMessages(messages):
delimiter = '\n'
msg_string = delimiter.join([f"{obj['role']}:{obj['content']}" for obj in messages[1:]])
logging.info("messages:"+msg_string)
return msg_string
#def clearMessage():
# global messages
# messages = [{"role": "system", "content": "Your name is Harry and you are an AI assistant who can answer any question except to clarify that you are an AI"},]
# logging.info("clear messages")
#with gr.Blocks() as app:
#newChatBtn = gr.Button("New Chat")
#newChatBtn.click(clearMessage)
#tbReply = gr.Textbox(label="Reply")
#tbHistroy = gr.Textbox(label="History Chat")
#inputs = [gr.Textbox(lines=7, label="Chat with Harry"), newChatBtn] #Hidden(label="Session ID", value=session_id)]
#outputs = [gr.Textbox(label="History Chat"), gr.Textbox(label="Reply")]
app = gr.Interface(fn=chatbot, inputs=[gr.Textbox(lines=7, label="Chat with Harry"), "state"],
outputs=[gr.Textbox(label="Reply"), gr.Textbox(label="History"), "state"], title="Chat with Harry",
description="Ask anything you want",theme="compact")
app.launch(share=False)