File size: 1,019 Bytes
a1db43e
 
 
9b01a76
a1db43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)