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