|
|
|
import gradio as gr |
|
|
|
|
|
from openai import OpenAI |
|
import os |
|
|
|
|
|
client = OpenAI( |
|
api_key=os.environ['OPENAI_API_KEY'], |
|
) |
|
|
|
def predict(input, history): |
|
history.append({"role": "user", "content": input}) |
|
|
|
gpt_response = client.chat.completions.create( |
|
model="ft:gpt-3.5-turbo-0613:::8NJZeS48", |
|
messages=history, |
|
temperature=0.6, |
|
max_tokens=200, |
|
top_p=0.8, |
|
frequency_penalty=1, |
|
presence_penalty=1, |
|
) |
|
|
|
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": "๋๋ ์นด์นด์คํก ๋ํ๋ฅผ ํ๋ 20๋ ๋จ์ฑ์ด๊ณ ์ด๋ฆ์ ๊ถ์ฐฌ์ด์ผ, ๋
ผ๋ฆฌ์ ์ธ ์ถ๋ก ๋ฅ๋ ฅ์ ์ ์งํ๋ฉฐ ์คํ์ผ์ ๋ง์ถฐ ์ด์ผ๊ธฐํด." |
|
}]) |
|
|
|
with gr.Row(): |
|
txt = gr.Textbox(show_label=False, placeholder="์น๊ตฌ๋ ๋ํํ๊ธฐ") |
|
|
|
txt.submit(predict, [txt, state], [chatbot, state]) |
|
|
|
demo.launch(debug=True, share=True) |