File size: 5,990 Bytes
6cbf490 b529ff7 a831e24 6cbf490 |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import gradio as gr
from openai import OpenAI
import os
client=OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
#์๋ด๋ด
def counseling_bot_chat(message, chat_history):
if message == '':
return "", chat_history
else:
completion=client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[
{"role":'system','content':'๋น์ ์ ๊ฒฝ์ ์ ๋ฌธ ๊ธฐ์ ์
๋๋ค. ๊ฒฝ์ ๋ถ์ผ์ ๊ด๋ จ๋์ง ์๋ ์ง๋ฌธ์๋ ์ ์คํ ๊ฑฐ์ ํ์ธ์.'},
{'role':'user','content':message}
]
)
chat_history.append([message, completion.choices[0].message.content])
return '', chat_history
def counseling_bot_undo(chat_history):
if len(chat_history) > 1:
chat_history.pop()
return chat_history
def counseling_bot_reset(messaage, chat_history):
chat_history=[[None, "์๋
ํ์ธ์. ๊ฒฝ์ ์ ๋ฌธ๊ธฐ์์
๋๋ค. ๊ถ๊ธํ ์ ๋ฌผ์ด๋ณด์ธ์."]]
return chat_history
# ๋ฒ์ญ๋ด
def translate_bot(output_conditions, output_language, input_text):
if input_text =='':
return ''
else:
if output_conditions == '':
output_conditions=''
else:
output_conditions='๋ฒ์ญํ ๋์ ์กฐ๊ฑด์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.'+output_conditions
completion=client.chat.completions.create(
model='gpt-3.5-turbo',
messages=[
{'role':'system','content':'๋น์ ์ ๋ฒ์ญ๊ฐ์
๋๋ค. ์
๋ ฅํ ์ธ์ด๋ฅผ ๋ค๋ฅธ ์ค๋ช
์์ด ๊ณง๋ฐ๋ก {0}๋ก ๋ฒ์ญํด์ ์๋ ค์ฃผ์ธ์. ๋ฒ์ญ์ด ๋ถ๊ฐ๋ฅํ ์ธ์ด๋ผ๋ฉด ๋ฒ์ญ์ด ๋ถ๊ฐ๋ฅํ๋ค๊ณ ๋งํ ํ ๊ทธ ์ด์ ๋ฅผ ์ค๋ช
ํด ์ฃผ์ธ์.{1}'.format(output_language,output_conditions)},
{'role':'user','content':input_text}
]
)
return completion.choices[0].message.content
# ์์ค๋ด
def novel_bot(model, temperature, detail):
completion=client.chat.completions.create(
model=model,
temperature=temperature,
messages=[
{'role':'system','content':'๋น์ ์ ์์ค๊ฐ ์
๋๋ค. ์์ฒญํ๋ ์กฐ๊ฑด์ ๋ง์ถฐ ์์ค์ ์์ฑํด ์ฃผ์ธ์'},
{'role':'user','content':detail}
]
)
return completion.choices[0].message.content
with gr.Blocks(theme=gr.themes.Default()) as app:
with gr.Tab('์๋ด๋ด'):
gr.Markdown(
value="""
# <center> ์๋ด๋ด </center>
<center>ํค์ด๋งํธ ์๋ด๋ด์
๋๋ค. ๋งํธ์์ ํํดํ๋ ์ํ๊ณผ ๊ด๋ จ๋ ์ง๋ฌธ์ ๋ต๋ณ๋๋ฆฝ๋๋ค.</center>
"""
)
cb_chatbot=gr.Chatbot(
value=[[None, '์๋
ํ์ธ์. ๊ฒฝ์ ์ ๋ฌธ๊ธฐ์์
๋๋ค. ๊ถ๊ธํ ์ ๋ฌผ์ด๋ณด์ธ์.']],
show_label=False
)
with gr.Row():
cb_user_input=gr.Text(
lines=1,
placeholder='์
๋ ฅ ์ฐฝ',
container=False,
scale=9
)
cb_send_btn=gr.Button(
value='๋ณด๋ด๊ธฐ',
scale=1,
variant='primary'
)
with gr.Row():
gr.Button(value='๋๋๋ฆฌ๊ธฐ').click(fn=counseling_bot_undo, inputs=cb_chatbot,outputs=cb_chatbot)
gr.Button(value='์ด๊ธฐํ').click(fn=counseling_bot_reset, inputs=cb_chatbot,outputs=cb_chatbot)
cb_send_btn.click(fn=counseling_bot_chat, inputs=[cb_user_input, cb_chatbot],outputs=[cb_user_input, cb_chatbot])
cb_user_input.submit(fn=counseling_bot_chat, inputs=[cb_user_input,cb_chatbot],outputs=[cb_user_input,cb_chatbot])
with gr.Tab('๋ฒ์ญ๋ด'):
gr.Markdown(
value="""
#<center>๋ฒ์ญ๋ด</center>
<center>๋ค๊ตญ์ด ๋ฒ์ญ๋ด์
๋๋ค.</center>
"""
)
with gr.Row():
tb_output_conditions=gr.Text(
label='๋ฒ์ญ ์กฐ๊ฑด',
placeholder="์์: ์์ฐ์ค๋ฝ๊ฒ",
lines=1,
max_lines=3,
)
tb_output_language=gr.Dropdown(
label='์ถ๋ ฅ ์ธ์ด',
choices=['ํ๊ตญ์ด','์์ด','์ผ๋ณธ์ด','์ค๊ตญ์ด'],
value='ํ๊ตญ์ด',
allow_custom_value=True,
interactive=True
)
tb_submit=gr.Button(
value='๋ฒ์ญํ๊ธฐ',
variant='primary'
)
with gr.Row():
tb_input_text=gr.Text(
placeholder='๋ฒ์ญํ ๋ด์ฉ์ ์ ์ด ์ฃผ์ธ์.',
lines=10,
max_lines=20,
show_copy_button=True,
label=''
)
tb_output_text=gr.Text(
lines=10,
max_lines=20,
show_copy_button=True,
label='',
interactive=False
)
tb_submit.click(
fn=translate_bot,
inputs=[tb_output_conditions,
tb_output_language,
tb_input_text],
outputs=tb_output_text
)
with gr.Tab('์์ค๋ด'):
gr.Markdown(
value="""
# <center>์์ค๋ด</center>
<center>์์ค์ ์์ฑํด ์ฃผ๋ ๋ด์
๋๋ค.</center>
"""
)
with gr.Accordion(label='์ฌ์ฉ์ ์ค์ '):
with gr.Row():
with gr.Column(scale=1):
nb_model=gr.Dropdown(
label='๋ชจ๋ธ ์ ํ',
choices=['gpt-3.5-turbo','gpt-3.5-turbo-16k'],
value='gpt-3.5-turbo',
interactive=True
)
nb_temperature=gr.Slider(
label='์ฐฝ์์ฑ',
info='์ซ์๊ฐ ๋์ ์๋ก ์ฐฝ์์ ',
minimum=0,
maximum=2,
step=0.1,
value=1,
interactive=True
)
nb_detail=gr.Text(
container=False,
placeholder='์์ค์ ์ธ๋ถ์ ์ธ ์ค์ ์ ์์ฑํฉ๋๋ค.',
lines=8,
scale=4
)
nb_submit=gr.Button(
value='์์ฑํ๊ธฐ',
variant='primary'
)
nb_output=gr.Text(
label='',
placeholder='์ด๊ณณ์ ์์ค์ ๋ด์ฉ์ด ์ถ๋ ฅ๋ฉ๋๋ค.',
lines=10,
max_lines=200,
show_copy_button=True
)
nb_submit.click(
fn=novel_bot,
inputs=[nb_model, nb_temperature, nb_detail],
outputs=nb_output
)
app.launch() |