import gradio as gr import os import openai from dotenv import load_dotenv load_dotenv() os.environ["OPENAI_API_KEY"] = os.environ['my_secret'] def predict(input, history): history.append({"role": "user", "content": input}) gpt_response = openai.ChatCompletion.create( model=model_id, messages=history ) response = gpt_response["choices"][0]["message"]["content"] history.append({"role": "assistant", "content": response}) messages = [(history[i]["content"], history[i+1]["content"]) for i in range(1, len(history), 2)] return messages, history with gr.Blocks() as demo: chatbot = gr.Chatbot(label="ChatBot") state = gr.State([{ "role": "system", "content": "You are a chatbot for psychological counseling." }]) with gr.Row(): txt = gr.Textbox(show_label=False, placeholder="상담을 요청해보세요").style(container=False) txt.submit(predict, [txt, state], [chatbot, state]) demo.launch(debug=True, share=True)