Upload app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,6 @@ import pandas as pd
|
|
9 |
import torch
|
10 |
|
11 |
|
12 |
-
|
13 |
id2label = {0: "NEGATIVE", 1: "POSITIVE"}
|
14 |
label2id = {"NEGATIVE": 0, "POSITIVE": 1}
|
15 |
|
@@ -78,7 +77,10 @@ kor_model = AutoModelForSequenceClassification.from_pretrained(
|
|
78 |
|
79 |
def builder(lang, text):
|
80 |
percent_kor, percent_eng = 0, 0
|
|
|
|
|
81 |
|
|
|
82 |
if lang == 'Any':
|
83 |
pred = LANGUAGE.predict_lang(text)
|
84 |
if '__label__en' in pred[0]:
|
@@ -101,6 +103,7 @@ def builder(lang, text):
|
|
101 |
if percent_kor==0: percent_kor=1
|
102 |
|
103 |
|
|
|
104 |
inputs = tokenized_data(tokenizer, text)
|
105 |
model.eval()
|
106 |
with torch.no_grad():
|
@@ -111,25 +114,45 @@ def builder(lang, text):
|
|
111 |
output = m(logits)
|
112 |
# print(logits, output)
|
113 |
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
return [ {'Kor': percent_kor, 'Eng': percent_eng, 'Other': 1-(percent_kor+percent_eng)},
|
|
|
|
|
|
|
|
|
117 |
return id2label[prediction.item()]
|
118 |
|
119 |
|
120 |
-
|
121 |
-
demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Any', 'Eng', 'Kor']), "text"],
|
122 |
-
outputs=[ gr.Label(num_top_classes=3, label='Lang'), gr.Label(num_top_classes=2, label='Result') ],
|
123 |
-
# outputs='label',
|
124 |
-
title=title, description=description, examples=examples)
|
125 |
-
|
126 |
-
|
127 |
# demo3 = gr.Interface.load("models/mdj1412/movie_review_score_discriminator_eng", inputs="text", outputs="text",
|
128 |
# title=title, theme="peach",
|
129 |
# allow_flagging="auto",
|
130 |
# description=description, examples=examples)
|
131 |
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
if __name__ == "__main__":
|
134 |
# print(examples)
|
135 |
demo.launch()
|
|
|
9 |
import torch
|
10 |
|
11 |
|
|
|
12 |
id2label = {0: "NEGATIVE", 1: "POSITIVE"}
|
13 |
label2id = {"NEGATIVE": 0, "POSITIVE": 1}
|
14 |
|
|
|
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 == 'Any':
|
85 |
pred = LANGUAGE.predict_lang(text)
|
86 |
if '__label__en' in pred[0]:
|
|
|
103 |
if percent_kor==0: percent_kor=1
|
104 |
|
105 |
|
106 |
+
# [ output_2 ]
|
107 |
inputs = tokenized_data(tokenizer, text)
|
108 |
model.eval()
|
109 |
with torch.no_grad():
|
|
|
114 |
output = m(logits)
|
115 |
# print(logits, output)
|
116 |
|
117 |
+
|
118 |
+
# [ output_3 ]
|
119 |
+
output_analysis = []
|
120 |
+
for word in text_list:
|
121 |
+
tokenized_word = tokenized_data(tokenizer, word)
|
122 |
+
with torch.no_grad():
|
123 |
+
logit = model(input_ids=tokenized_word['input_ids'],
|
124 |
+
attention_mask=tokenized_word['attention_mask']).logits
|
125 |
+
word_output = m(logit)
|
126 |
+
if word_output[0][1] > 0.95:
|
127 |
+
output_analysis.append( (word, '+') )
|
128 |
+
elif word_output[0][1] < 0.05:
|
129 |
+
output_analysis.append( (word, '-') )
|
130 |
+
else:
|
131 |
+
output_analysis.append( (word, None) )
|
132 |
|
133 |
+
return [ {'Kor': percent_kor, 'Eng': percent_eng, 'Other': 1-(percent_kor+percent_eng)},
|
134 |
+
{id2label[1]: output[0][1].item(), id2label[0]: output[0][0].item()},
|
135 |
+
output_analysis ]
|
136 |
+
|
137 |
+
# prediction = torch.argmax(logits, axis=1)
|
138 |
return id2label[prediction.item()]
|
139 |
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
# demo3 = gr.Interface.load("models/mdj1412/movie_review_score_discriminator_eng", inputs="text", outputs="text",
|
142 |
# title=title, theme="peach",
|
143 |
# allow_flagging="auto",
|
144 |
# description=description, examples=examples)
|
145 |
|
146 |
|
147 |
+
|
148 |
+
demo = gr.Interface(builder, inputs=[gr.inputs.Dropdown(['Any', 'Eng', 'Kor']), "text"],
|
149 |
+
outputs=[ gr.Label(num_top_classes=3, label='Lang'),
|
150 |
+
gr.Label(num_top_classes=2, label='Result'),
|
151 |
+
gr.HighlightedText(label="Analysis", combine_adjacent=False).style(color_map={"+": "red", "-": "green"}) ],
|
152 |
+
# outputs='label',
|
153 |
+
title=title, description=description, examples=examples)
|
154 |
+
|
155 |
+
|
156 |
if __name__ == "__main__":
|
157 |
# print(examples)
|
158 |
demo.launch()
|