Update app.py
Browse files
app.py
CHANGED
@@ -75,36 +75,39 @@ kor_model = AutoModelForSequenceClassification.from_pretrained(
|
|
75 |
)
|
76 |
|
77 |
|
78 |
-
def builder(
|
79 |
percent_kor, percent_eng = 0, 0
|
80 |
-
text_list =
|
81 |
|
82 |
|
83 |
# [ output_1 ]
|
84 |
-
if
|
85 |
-
pred = LANGUAGE.predict_lang(
|
86 |
if '__label__en' in pred[0]:
|
87 |
-
|
88 |
idx = pred[0].index('__label__en')
|
89 |
-
|
90 |
if '__label__ko' in pred[0]:
|
91 |
-
|
92 |
idx = pred[0].index('__label__ko')
|
93 |
-
|
|
|
|
|
|
|
94 |
|
95 |
-
if
|
96 |
model = eng_model
|
97 |
tokenizer = eng_tokenizer
|
98 |
if percent_eng==0: percent_eng=1
|
99 |
|
100 |
-
if
|
101 |
model = kor_model
|
102 |
tokenizer = kor_tokenizer
|
103 |
if percent_kor==0: percent_kor=1
|
104 |
|
105 |
|
106 |
# [ output_2 ]
|
107 |
-
inputs = tokenized_data(tokenizer,
|
108 |
model.eval()
|
109 |
with torch.no_grad():
|
110 |
logits = model(input_ids=inputs['input_ids'],
|
@@ -139,7 +142,7 @@ def builder(lang, text):
|
|
139 |
output_analysis.append( (word, None) )
|
140 |
|
141 |
|
142 |
-
return [ {'Kor': percent_kor, 'Eng': percent_eng
|
143 |
{id2label[1]: output[0][1].item(), id2label[0]: output[0][0].item()},
|
144 |
output_analysis ]
|
145 |
|
@@ -154,7 +157,7 @@ def builder(lang, text):
|
|
154 |
|
155 |
|
156 |
|
157 |
-
demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Default', 'Eng', 'Kor']), "
|
158 |
outputs=[ gr.Label(num_top_classes=3, label='Lang'),
|
159 |
gr.Label(num_top_classes=2, label='Result'),
|
160 |
gr.HighlightedText(label="Analysis", combine_adjacent=False)
|
@@ -162,8 +165,44 @@ demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Default', 'Eng', 'Kor'
|
|
162 |
# outputs='label',
|
163 |
title=title, description=description, examples=examples)
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
if __name__ == "__main__":
|
167 |
# print(examples)
|
168 |
demo.launch()
|
169 |
-
|
|
|
75 |
)
|
76 |
|
77 |
|
78 |
+
def builder(Lang, Text):
|
79 |
percent_kor, percent_eng = 0, 0
|
80 |
+
text_list = Text.split(' ')
|
81 |
|
82 |
|
83 |
# [ output_1 ]
|
84 |
+
if Lang == 'Default':
|
85 |
+
pred = LANGUAGE.predict_lang(Text)
|
86 |
if '__label__en' in pred[0]:
|
87 |
+
Lang = 'Eng'
|
88 |
idx = pred[0].index('__label__en')
|
89 |
+
p_eng = pred[1][idx]
|
90 |
if '__label__ko' in pred[0]:
|
91 |
+
Lang = 'Kor'
|
92 |
idx = pred[0].index('__label__ko')
|
93 |
+
p_kor = pred[1][idx]
|
94 |
+
# Normalize Percentage
|
95 |
+
percent_kor = p_kor / (p_kor+p_eng)
|
96 |
+
percent_eng = p_eng / (p_kor+p_eng)
|
97 |
|
98 |
+
if Lang == 'Eng':
|
99 |
model = eng_model
|
100 |
tokenizer = eng_tokenizer
|
101 |
if percent_eng==0: percent_eng=1
|
102 |
|
103 |
+
if Lang == 'Kor':
|
104 |
model = kor_model
|
105 |
tokenizer = kor_tokenizer
|
106 |
if percent_kor==0: percent_kor=1
|
107 |
|
108 |
|
109 |
# [ output_2 ]
|
110 |
+
inputs = tokenized_data(tokenizer, Text)
|
111 |
model.eval()
|
112 |
with torch.no_grad():
|
113 |
logits = model(input_ids=inputs['input_ids'],
|
|
|
142 |
output_analysis.append( (word, None) )
|
143 |
|
144 |
|
145 |
+
return [ {'Kor': percent_kor, 'Eng': percent_eng},
|
146 |
{id2label[1]: output[0][1].item(), id2label[0]: output[0][0].item()},
|
147 |
output_analysis ]
|
148 |
|
|
|
157 |
|
158 |
|
159 |
|
160 |
+
demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Default', 'Eng', 'Kor']), gr.Textbox(placeholder="๋ฆฌ๋ทฐ๋ฅผ ์
๋ ฅํ์์ค.")],
|
161 |
outputs=[ gr.Label(num_top_classes=3, label='Lang'),
|
162 |
gr.Label(num_top_classes=2, label='Result'),
|
163 |
gr.HighlightedText(label="Analysis", combine_adjacent=False)
|
|
|
165 |
# outputs='label',
|
166 |
title=title, description=description, examples=examples)
|
167 |
|
168 |
+
with gr.Blocks() as demo1:
|
169 |
+
gr.Markdown(
|
170 |
+
"""
|
171 |
+
<h1 align="center">
|
172 |
+
Movie Review Score Discriminator
|
173 |
+
</h1>
|
174 |
+
""")
|
175 |
+
|
176 |
+
with gr.Accordion("Open for More!"):
|
177 |
+
gr.Markdown(
|
178 |
+
"""
|
179 |
+
๋ด์ฉ์ ์์ง ๋ฐ๊พธ์ง ์์์ (ํ์๋ง ์ฐธ๊ณ )
|
180 |
+
๋ฌธ์ ์ : ํด๋ฆฌ์ด ํด๋ฆญ์ด ์ํ๋๋๋ก ์๋จ
|
181 |
+
It is a program that classifies whether it is positive or negative by entering movie reviews. \
|
182 |
+
You can choose between the Korean version and the English version. \
|
183 |
+
It also provides a version called ""Default"", which determines whether it is Korean or English and predicts it.
|
184 |
+
""")
|
185 |
+
|
186 |
+
|
187 |
+
with gr.Row():
|
188 |
+
with gr.Column():
|
189 |
+
inputs_1 = gr.inputs.Dropdown(['Default', 'Eng', 'Kor'])
|
190 |
+
inputs_2 = gr.Textbox(placeholder="๋ฆฌ๋ทฐ๋ฅผ ์
๋ ฅํ์์ค.")
|
191 |
+
with gr.Row():
|
192 |
+
btn2 = gr.Button("ํด๋ฆฌ์ด")
|
193 |
+
btn = gr.Button("์ ์ถํ๊ธฐ")
|
194 |
+
with gr.Column():
|
195 |
+
output_1 = gr.Label(num_top_classes=3, label='Lang')
|
196 |
+
output_2 = gr.Label(num_top_classes=2, label='Result')
|
197 |
+
output_3 = gr.HighlightedText(label="Analysis", combine_adjacent=False) \
|
198 |
+
.style(color_map={"+++": "#CF0000", "++": "#FF3232", "+": "#FFD4D4", "---": "#0004FE", "--": "#4C47FF", "-": "#BEBDFF"})
|
199 |
+
|
200 |
+
btn.click(fn=builder, inputs=[inputs_1, inputs_2], outputs=[output_1, output_2, output_3])
|
201 |
+
gr.Examples(examples, inputs=[inputs_1, inputs_2])
|
202 |
+
|
203 |
+
|
204 |
|
205 |
if __name__ == "__main__":
|
206 |
# print(examples)
|
207 |
demo.launch()
|
208 |
+
demo1.launch()
|