mdj1412 commited on
Commit
c239d0a
โ€ข
1 Parent(s): 1280cc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -14
app.py CHANGED
@@ -75,36 +75,39 @@ kor_model = AutoModelForSequenceClassification.from_pretrained(
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
- percent_eng = pred[1][idx]
90
  if '__label__ko' in pred[0]:
91
- lang = 'Kor'
92
  idx = pred[0].index('__label__ko')
93
- percent_kor = pred[1][idx]
 
 
 
94
 
95
- if lang == 'Eng':
96
  model = eng_model
97
  tokenizer = eng_tokenizer
98
  if percent_eng==0: percent_eng=1
99
 
100
- if lang == 'Kor':
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, text)
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, 'Other': 1-(percent_kor+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']), "text"],
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
- # demo3.launch()
 
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()