Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import re
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
from transformers import DistilBertModel
|
7 |
+
from tokenization_kobert import KoBertTokenizer
|
8 |
+
|
9 |
+
class SanctiMoly(nn.Module):
|
10 |
+
""" Holy Moly News BERT """
|
11 |
+
|
12 |
+
def __init__(self, freeze_bert = True):
|
13 |
+
super(SanctiMoly, self).__init__()
|
14 |
+
self.encoder = bert_model
|
15 |
+
# FC-BN-Tanh
|
16 |
+
self.linear = nn.Sequential(nn.Linear(768, 1024),
|
17 |
+
nn.BatchNorm1d(1024),
|
18 |
+
nn.Tanh(),
|
19 |
+
nn.Dropout(),
|
20 |
+
nn.Linear(1024, 768),
|
21 |
+
nn.BatchNorm1d(768),
|
22 |
+
nn.Tanh(),
|
23 |
+
nn.Dropout(),
|
24 |
+
nn.Linear(768, 120)
|
25 |
+
)
|
26 |
+
# self.softmax = nn.LogSoftmax(dim=-1)
|
27 |
+
|
28 |
+
if freeze_bert == True:
|
29 |
+
for param in self.encoder.parameters():
|
30 |
+
param.requires_grad = False
|
31 |
+
else:
|
32 |
+
for param in self.encoder.parameters():
|
33 |
+
param.requires_grad = True
|
34 |
+
|
35 |
+
|
36 |
+
def forward(self, input_ids, input_length):
|
37 |
+
# calculate attention mask
|
38 |
+
attn_mask = torch.arange(input_ids.size(1)).to(device)
|
39 |
+
attn_mask = attn_mask[None, :] < input_length[:, None]
|
40 |
+
|
41 |
+
enc_o = self.encoder(input_ids, attn_mask)
|
42 |
+
|
43 |
+
output = self.linear(enc_o.last_hidden_state[:, 0, :])
|
44 |
+
# print(output.shape)
|
45 |
+
return output
|
46 |
+
|
47 |
+
@st.cache(allow_output_mutation=True)
|
48 |
+
def get_model():
|
49 |
+
bert_model = DistilBertModel.from_pretrained('monologg/distilkobert')
|
50 |
+
tokenizer = KoBertTokenizer.from_pretrained('monologg/distilkobert')
|
51 |
+
|
52 |
+
model = SanctiMoly(freeze_bert=False)
|
53 |
+
checkpoint = torch.load("./model.pt", map_location=device)
|
54 |
+
model.load_state_dict(checkpoint['model_state_dict'])
|
55 |
+
|
56 |
+
return model, tokenizer
|
57 |
+
|
58 |
+
model, tokenizer = get_model()
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
class RegexSubstitution(object):
|
64 |
+
"""Regex substitution class for transform"""
|
65 |
+
def __init__(self, regex, sub=''):
|
66 |
+
if isinstance(regex, re.Pattern):
|
67 |
+
self.regex = regex
|
68 |
+
else:
|
69 |
+
self.regex = re.compile(regex)
|
70 |
+
self.sub = sub
|
71 |
+
def __call__(self, target):
|
72 |
+
if isinstance(target, list):
|
73 |
+
return [self.regex.sub(self.sub, self.regex.sub(self.sub, string)) for string in target]
|
74 |
+
else:
|
75 |
+
return self.regex.sub(self.sub, self.regex.sub(self.sub, target))
|
76 |
+
def i2ym(fl):
|
77 |
+
return (str(fl // 12 + 2009), str(fl % 12 + 1))
|
78 |
+
|
79 |
+
default_text = '''์ง๋ณ๊ด๋ฆฌ์ฒญ์ 23์ผ ์ง๋ฐฉ์์น๋จ์ฒด๊ฐ ๋ณด๊ฑด๋น๊ตญ๊ณผ ํ์ ์์ด ๋จ๋
์ผ๋ก ์ธํ๋ฃจ์์(๋
๊ฐ) ๋ฐฑ์ ์ ์ข
์ค๋จ์ ๊ฒฐ์ ํด์๋ ์ ๋๋ค๋ ์
์ฅ์ ๋ฐํ๋ค.
|
80 |
+
์ง๋ณ์ฒญ์ ์ด๋ ์ฐธ๊ณ ์๋ฃ๋ฅผ ๋ฐฐํฌํ๊ณ โํฅํ ์ ์ฒด ๊ตญ๊ฐ ์๋ฐฉ์ ์ข
์ฌ์
์ด ์ฐจ์ง ์์ด ์งํ๋๋๋ก ์ง์์ฒด๊ฐ ์์ฒด์ ์ผ๋ก ์ ์ข
์ ๋ณด ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ์ง ์๋๋ก ์๋ด๋ฅผ ํ๋คโ๊ณ ์ค๋ช
ํ๋ค.
|
81 |
+
๋
๊ฐ๋ฐฑ์ ์ ์ ์ข
ํ ํ ๊ณ ๋ น์ธต์ ์ค์ฌ์ผ๋ก ์ ๊ตญ์์ ์ฌ๋ง์๊ฐ ์๋ฐ๋ฅด์ ์์ธ ์๋ฑํฌ๊ตฌ๋ณด๊ฑด์๋ ์ ๋ , ๊ฒฝ๋ถ ํฌํญ์๋ ์ด๋ ๊ด๋ด ์๋ฃ๊ธฐ๊ด์ ์ ์ข
์ ๋ณด๋ฅํด๋ฌ๋ผ๋ ๊ณต๋ฌธ์ ๋ด๋ ค๋ณด๋๋ค. ์ด๋ ์๋ฐฉ์ ์ข
๊ณผ ์ฌ๋ง ๊ฐ ์ง์ ์ ์ฐ๊ด์ฑ์ด ๋ฎ์ ์ ์ข
์ ์ค๋จํ ์ํฉ์ ์๋๋ผ๋ ์ง๋ณ์ฒญ์ ํ๋จ๊ณผ๋ ๋ค๋ฅธ ๊ฒ์ด๋ค.
|
82 |
+
์ง๋ณ์ฒญ์ ์ง๋ 21์ผ ์ ๋ฌธ๊ฐ ๋ฑ์ด ์ฐธ์ฌํ โ์๋ฐฉ์ ์ข
ํผํด์กฐ์ฌ๋ฐโ์ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก ๋
๊ฐ ์๋ฐฉ์ ์ข
์ฌ์
์ ์ผ์ ๋๋ก ์งํํ๊ธฐ๋ก ํ๋ค. ํนํ ๊ณ ๋ น ์ด๋ฅด์ ๊ณผ ์ด๋ฆฐ์ด, ์์ ๋ถ ๋ฑ ๋
๊ฐ ๊ณ ์ํ๊ตฐ์ ๋ฐฑ์ ์ ์ ์ข
ํ์ง ์์์ ๋ ํฉ๋ณ์ฆ ํผํด๊ฐ ํด ์ ์๋ค๋ฉด์ ์ ์ข
์ ๋
๋ คํ๋ค. ํ์ง๋ง ์ ์ข
์ฌ์
์ ์ง ๋ฐํ ์ดํ์๋ ์ฌ๋ง ๋ณด๊ณ ๊ฐ ์๋ฐ๋ฅด์ ์ง๋ณ์ฒญ์ ์ด๋ โ์๋ฐฉ์ ์ข
ํผํด์กฐ์ฌ๋ฐ ํ์โ์ โ์๋ฐฉ์ ์ข
์ ๋ฌธ์์ํโ๋ฅผ ๊ฐ์ตํด ๋
๊ฐ๋ฐฑ์ ๊ณผ ์ฌ๋ง ๊ฐ ๊ด๋ จ์ฑ, ์ ์ข
์ฌ์
์ ์ง ์ฌ๋ถ ๋ฑ์ ๋ํด ๋ค์ ๊ฒฐ๋ก ๋ด๋ฆฌ๊ธฐ๋ก ํ๋ค. ํ์ ๊ฒฐ๊ณผ๋ ์ด๋ ์คํ 7์ ๋์ด ๋ฐํ๋ ์์ ์ด๋ค.
|
83 |
+
'''
|
84 |
+
|
85 |
+
|
86 |
+
st.title("Date prediction")
|
87 |
+
text = st.text_area("Input news :", value=default_text)
|
88 |
+
st.markdown("## Original News Data")
|
89 |
+
st.write(text)
|
90 |
+
st.markdown("## Predict Date")
|
91 |
+
col1, col2 = st.columns(2)
|
92 |
+
if text:
|
93 |
+
with st.spinner('processing..'):
|
94 |
+
text = RegexSubstitution(r'\([^()]+\)|[<>\'"โณโฒโกโ ]')(text)
|
95 |
+
encoded_dict = tokenizer(
|
96 |
+
text=[text],
|
97 |
+
add_special_tokens=True,
|
98 |
+
max_length=512,
|
99 |
+
truncation=True,
|
100 |
+
return_tensors='pt',
|
101 |
+
return_length=True
|
102 |
+
)
|
103 |
+
input_ids = encoded_dict['input_ids']
|
104 |
+
input_ids_len = encoded_dict['length']
|
105 |
+
|
106 |
+
pred = model(input_ids, input_ids_len)
|
107 |
+
|
108 |
+
_, indices = torch.topk(pred, 3)
|
109 |
+
pred_print = []
|
110 |
+
for i in indices.squeeze(0):
|
111 |
+
year, month = i2ym(i.item()))
|
112 |
+
pred_print.append(year+"-"+month)
|
113 |
+
st.write(", ".join(pred_print))
|