Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as lit
|
2 |
+
import torch
|
3 |
+
from transformers import BartForConditionalGeneration, PreTrainedTokenizerFast
|
4 |
+
|
5 |
+
@lit.cache(allow_output_mutation = True)
|
6 |
+
def loadModels():
|
7 |
+
repository = "rycont/biblify"
|
8 |
+
_model = BartForConditionalGeneration.from_pretrained(repository)
|
9 |
+
_tokenizer = PreTrainedTokenizerFast.from_pretrained(repository)
|
10 |
+
|
11 |
+
print("Loaded :)")
|
12 |
+
|
13 |
+
return _model, _tokenizer
|
14 |
+
|
15 |
+
model, tokenizer = loadModels()
|
16 |
+
|
17 |
+
lit.title("성경말투 생성기")
|
18 |
+
text_input = lit.text_area("문장 입력")
|
19 |
+
|
20 |
+
MAX_LENGTH = 128
|
21 |
+
|
22 |
+
def biblifyWithBeams(beam, tokens, attention_mask):
|
23 |
+
generated = model.generate(
|
24 |
+
input_ids = torch.Tensor([ tokens ]).to(torch.int64),
|
25 |
+
attention_mask = torch.Tensor([ attentionMasks ]).to(torch.int64),
|
26 |
+
num_beams = beam,
|
27 |
+
max_length = MAX_LENGTH,
|
28 |
+
eos_token_id=tokenizer.eos_token_id,
|
29 |
+
bad_words_ids=[[tokenizer.unk_token_id]]
|
30 |
+
)[0]
|
31 |
+
|
32 |
+
return tokenizer.decode(
|
33 |
+
generated,
|
34 |
+
).replace('<s>', '').replace('</s>', '')
|
35 |
+
|
36 |
+
if len(text_input.strip()) > 0:
|
37 |
+
print(text_input)
|
38 |
+
|
39 |
+
text_input = "<s>" + text_input + "</s>"
|
40 |
+
tokens = tokenizer.encode(text_input)
|
41 |
+
|
42 |
+
tokenLength = len(tokens)
|
43 |
+
attentionMasks = [ 1 ] * tokenLength + [ 0 ] * (MAX_LENGTH - tokenLength)
|
44 |
+
tokens = tokens + [ tokenizer.pad_token_id ] * (MAX_LENGTH - tokenLength)
|
45 |
+
|
46 |
+
results = []
|
47 |
+
|
48 |
+
for i in range(10)[5:]:
|
49 |
+
generated = biblifyWithBeams(
|
50 |
+
i + 1,
|
51 |
+
tokens,
|
52 |
+
attentionMasks
|
53 |
+
)
|
54 |
+
|
55 |
+
if generated in results:
|
56 |
+
print("중복됨")
|
57 |
+
continue
|
58 |
+
|
59 |
+
results.append(generated)
|
60 |
+
with lit.expander(str(len(results)) + "번째 결과 (" + str(i +1) + ")", True):
|
61 |
+
lit.write(generated)
|
62 |
+
lit.caption(
|
63 |
+
"및 " + str(5 - len(results)) + " 개의 중복된 결과")
|
64 |
+
|
65 |
+
|