|
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() |