567 / app.py
gonsu's picture
Update app.py
b67c69f
#gradio๋กœ ์ฑ—๋ด‡ ์„œ๋น™ํ•˜๊ธฐ
import gradio as gr
#openai ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
from openai import OpenAI
import os
#api ํ‚ค ์ˆจ๊ธฐ๊ธฐ (์‹œํฌ๋ฆฟ ํ‚ค ์‚ฌ์šฉ)
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, # ๋‚ฎ์€ temperature ๊ฐ’์œผ๋กœ ๋” ๋ณด์ˆ˜์ ์ธ ๋‹ต๋ณ€์„ ์–ป์Šต๋‹ˆ๋‹ค.
max_tokens=200, # ์ถœ๋ ฅ์˜ ๊ธธ์ด๋ฅผ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค.
top_p=0.8, # top_p ๊ฐ’์„ ์กฐ์ ˆํ•˜์—ฌ ์ƒ˜ํ”Œ๋ง ์ „๋žต์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
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)