Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import re
|
3 |
+
import streamlit as st
|
4 |
+
import pandas as pd
|
5 |
+
from transformers import PreTrainedTokenizerFast, DistilBertForSequenceClassification, BartForConditionalGeneration
|
6 |
+
|
7 |
+
from tokenization_kobert import KoBertTokenizer
|
8 |
+
from tokenizers import SentencePieceBPETokenizer
|
9 |
+
|
10 |
+
|
11 |
+
@st.cache(allow_output_mutation=True)
|
12 |
+
def get_topic():
|
13 |
+
model = DistilBertForSequenceClassification.from_pretrained(
|
14 |
+
'alex6095/SanctiMolyTopic', problem_type="multi_label_classification", num_labels=9)
|
15 |
+
model.eval()
|
16 |
+
tokenizer = KoBertTokenizer.from_pretrained('monologg/distilkobert')
|
17 |
+
return model, tokenizer
|
18 |
+
|
19 |
+
@st.cache(allow_output_mutation=True)
|
20 |
+
def get_date():
|
21 |
+
model = BartForConditionalGeneration.from_pretrained('alex6095/SanctiMoly-Bart')
|
22 |
+
model.eval()
|
23 |
+
tokenizer = PreTrainedTokenizerFast.from_pretrained('gogamza/kobart-summarization')
|
24 |
+
return model, tokenizer
|
25 |
+
|
26 |
+
class RegexSubstitution(object):
|
27 |
+
"""Regex substitution class for transform"""
|
28 |
+
def __init__(self, regex, sub=''):
|
29 |
+
if isinstance(regex, re.Pattern):
|
30 |
+
self.regex = regex
|
31 |
+
else:
|
32 |
+
self.regex = re.compile(regex)
|
33 |
+
self.sub = sub
|
34 |
+
def __call__(self, target):
|
35 |
+
if isinstance(target, list):
|
36 |
+
return [self.regex.sub(self.sub, self.regex.sub(self.sub, string)) for string in target]
|
37 |
+
else:
|
38 |
+
return self.regex.sub(self.sub, self.regex.sub(self.sub, target))
|
39 |
+
|
40 |
+
default_text = '''์ง๋ณ๊ด๋ฆฌ์ฒญ์ 23์ผ ์ง๋ฐฉ์์น๋จ์ฒด๊ฐ ๋ณด๊ฑด๋น๊ตญ๊ณผ ํ์ ์์ด ๋จ๋
์ผ๋ก ์ธํ๋ฃจ์์(๋
๊ฐ) ๋ฐฑ์ ์ ์ข
์ค๋จ์ ๊ฒฐ์ ํด์๋ ์ ๋๋ค๋ ์
์ฅ์ ๋ฐํ๋ค.
|
41 |
+
์ง๋ณ์ฒญ์ ์ด๋ ์ฐธ๊ณ ์๋ฃ๋ฅผ ๋ฐฐํฌํ๊ณ โํฅํ ์ ์ฒด ๊ตญ๊ฐ ์๋ฐฉ์ ์ข
์ฌ์
์ด ์ฐจ์ง ์์ด ์งํ๋๋๋ก ์ง์์ฒด๊ฐ ์์ฒด์ ์ผ๋ก ์ ์ข
์ ๋ณด ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ์ง ์๋๋ก ์๋ด๋ฅผ ํ๋คโ๊ณ ์ค๋ช
ํ๋ค.
|
42 |
+
๋
๊ฐ๋ฐฑ์ ์ ์ ์ข
ํ ํ ๊ณ ๋ น์ธต์ ์ค์ฌ์ผ๋ก ์ ๊ตญ์์ ์ฌ๋ง์๊ฐ ์๋ฐ๋ฅด์ ์์ธ ์๋ฑํฌ๊ตฌ๋ณด๊ฑด์๋ ์ ๋ , ๊ฒฝ๋ถ ํฌํญ์๋ ์ด๋ ๊ด๋ด ์๋ฃ๊ธฐ๊ด์ ์ ์ข
์ ๋ณด๋ฅํด๋ฌ๋ผ๋ ๊ณต๋ฌธ์ ๋ด๋ ค๋ณด๋๋ค. ์ด๋ ์๋ฐฉ์ ์ข
๊ณผ ์ฌ๋ง ๊ฐ ์ง์ ์ ์ฐ๊ด์ฑ์ด ๋ฎ์ ์ ์ข
์ ์ค๋จํ ์ํฉ์ ์๋๋ผ๋ ์ง๋ณ์ฒญ์ ํ๋จ๊ณผ๋ ๋ค๋ฅธ ๊ฒ์ด๋ค.
|
43 |
+
์ง๋ณ์ฒญ์ ์ง๋ 21์ผ ์ ๋ฌธ๊ฐ ๋ฑ์ด ์ฐธ์ฌํ โ์๋ฐฉ์ ์ข
ํผํด์กฐ์ฌ๋ฐโ์ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก ๋
๊ฐ ์๋ฐฉ์ ์ข
์ฌ์
์ ์ผ์ ๋๋ก ์งํํ๊ธฐ๋ก ํ๋ค. ํนํ ๊ณ ๋ น ์ด๋ฅด์ ๊ณผ ์ด๋ฆฐ์ด, ์์ ๋ถ ๋ฑ ๋
๊ฐ ๊ณ ์ํ๊ตฐ์ ๋ฐฑ์ ์ ์ ์ข
ํ์ง ์์์ ๋ ํฉ๋ณ์ฆ ํผํด๊ฐ ํด ์ ์๋ค๋ฉด์ ์ ์ข
์ ๋
๋ คํ๋ค. ํ์ง๋ง ์ ์ข
์ฌ์
์ ์ง ๋ฐํ ์ดํ์๋ ์ฌ๋ง ๋ณด๊ณ ๊ฐ ์๋ฐ๋ฅด์ ์ง๋ณ์ฒญ์ ์ด๋ โ์๋ฐฉ์ ์ข
ํผํด์กฐ์ฌ๋ฐ ํ์โ์ โ์๋ฐฉ์ ์ข
์ ๋ฌธ์์ํโ๋ฅผ ๊ฐ์ตํด ๋
๊ฐ๋ฐฑ์ ๊ณผ ์ฌ๋ง ๊ฐ ๊ด๋ จ์ฑ, ์ ์ข
์ฌ์
์ ์ง ์ฌ๋ถ ๋ฑ์ ๋ํด ๋ค์ ๊ฒฐ๋ก ๋ด๋ฆฌ๊ธฐ๋ก ํ๋ค. ํ์ ๊ฒฐ๊ณผ๋ ์ด๋ ์คํ 7์ ๋์ด ๋ฐํ๋ ์์ ์ด๋ค.
|
44 |
+
'''
|
45 |
+
topics_raw = ['IT/๊ณผํ', '๊ฒฝ์ ', '๋ฌธํ', '๋ฏธ์ฉ/๊ฑด๊ฐ', '์ฌํ', '์ํ', '์คํฌ์ธ ', '์ฐ์', '์ ์น']
|
46 |
+
|
47 |
+
|
48 |
+
#topic_model, topic_tokenizer = get_topic()
|
49 |
+
#date_model, date_tokenizer = get_date()
|
50 |
+
|
51 |
+
name = st.side_bar.selectbox('Model', ['Topic Classification', 'Date Prediction'])
|
52 |
+
if name == 'Topic Classification':
|
53 |
+
title = 'News Topic Classification'
|
54 |
+
model, tokenizer = get_topic()
|
55 |
+
elif name == 'Date Prediction':
|
56 |
+
title = 'News Date prediction'
|
57 |
+
model, tokenizer = get_date()
|
58 |
+
|
59 |
+
st.title(title)
|
60 |
+
|
61 |
+
text = st.text_area("Input news :", value=default_text)
|
62 |
+
st.markdown("## Original News Data")
|
63 |
+
st.write(text)
|
64 |
+
|
65 |
+
if name == 'Topic Classification':
|
66 |
+
st.markdown("## Predict Topic")
|
67 |
+
col1, col2 = st.columns(2)
|
68 |
+
if text:
|
69 |
+
with st.spinner('processing..'):
|
70 |
+
text = RegexSubstitution(r'\([^()]+\)|[<>\'"โณโฒโกโ ]')(text)
|
71 |
+
encoded_dict = tokenizer(
|
72 |
+
text=text,
|
73 |
+
add_special_tokens=True,
|
74 |
+
max_length=512,
|
75 |
+
truncation=True,
|
76 |
+
return_tensors='pt',
|
77 |
+
return_length=True
|
78 |
+
)
|
79 |
+
input_ids = encoded_dict['input_ids']
|
80 |
+
input_ids_len = encoded_dict['length'].unsqueeze(0)
|
81 |
+
attn_mask = torch.arange(input_ids.size(1))
|
82 |
+
attn_mask = attn_mask[None, :] < input_ids_len[:, None]
|
83 |
+
outputs = model(input_ids=input_ids, attention_mask=attn_mask)
|
84 |
+
_, preds = torch.max(outputs.logits, 1)
|
85 |
+
col1.write(topics_raw[preds.squeeze(0)])
|
86 |
+
softmax = torch.nn.Softmax(dim=1)
|
87 |
+
prob = softmax(outputs.logits).squeeze(0).detach()
|
88 |
+
chart_data = pd.DataFrame({
|
89 |
+
'Topic': topics_raw,
|
90 |
+
'Probability': prob
|
91 |
+
})
|
92 |
+
chart_data = chart_data.set_index('Topic')
|
93 |
+
col2.bar_chart(chart_data)
|
94 |
+
|
95 |
+
elif name == 'Date Prediction':
|
96 |
+
st.markdown("## Predict Date")
|
97 |
+
if text:
|
98 |
+
with st.spinner('processing..'):
|
99 |
+
text = RegexSubstitution(r'\([^()]+\)|[<>\'"โณโฒโกโ ]')(text)
|
100 |
+
raw_input_ids = tokenizer.encode(text)
|
101 |
+
input_ids = [tokenizer.bos_token_id] + \
|
102 |
+
raw_input_ids + [tokenizer.eos_token_id]
|
103 |
+
outputs = model.generate(torch.tensor([input_ids]),
|
104 |
+
early_stopping=True,
|
105 |
+
repetition_penalty=2.0,
|
106 |
+
do_sample=True, #์ํ๋ง ์ ๋ต ์ฌ์ฉ
|
107 |
+
max_length=50, # ์ต๋ ๋์ฝ๋ฉ ๊ธธ์ด๋ 50
|
108 |
+
top_k=50, # ํ๋ฅ ์์๊ฐ 50์ ๋ฐ์ธ ํ ํฐ์ ์ํ๋ง์์ ์ ์ธ
|
109 |
+
top_p=0.95, # ๋์ ํ๋ฅ ์ด 95%์ธ ํ๋ณด์งํฉ์์๋ง ์์ฑ
|
110 |
+
num_return_sequences=3 #3๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋์ฝ๋ฉํด๋ธ๋ค
|
111 |
+
)
|
112 |
+
for output in outputs:
|
113 |
+
pred_print = tokenizer.decode(output.squeeze().tolist(), skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
114 |
+
st.write(pred_print)
|