gidae commited on
Commit
6cbf490
โ€ข
1 Parent(s): 092b935

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +187 -0
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()