File size: 2,032 Bytes
9234bd3
 
 
 
 
 
 
 
 
 
 
 
 
f2de50e
 
3a2997a
53637e0
1fbbb31
f2de50e
9234bd3
 
 
 
 
 
 
 
 
 
2defe66
 
9234bd3
 
 
 
c5ecc2d
f2de50e
 
aeb7c06
9234bd3
 
 
 
 
2defe66
9234bd3
2defe66
9234bd3
 
 
 
 
 
 
 
 
 
084831c
 
2defe66
 
 
084831c
2defe66
084831c
 
 
2defe66
084831c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import streamlit as lit
import torch
from transformers import BartForConditionalGeneration, PreTrainedTokenizerFast

@lit.cache(allow_output_mutation = True)
def loadModels():
  repository = "rycont/biblify"
  _model = BartForConditionalGeneration.from_pretrained(repository)
  _tokenizer = PreTrainedTokenizerFast.from_pretrained(repository)
  
  print("Loaded :)")
  return _model, _tokenizer

lit.title("์„ฑ๊ฒฝ๋งํˆฌ ์ƒ์„ฑ๊ธฐ")
lit.caption("ํ•œ ๋ฌธ์žฅ์„ ๊ฐ€์žฅ ์ž˜ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋‹ค๋ฉด ์•„๋ž˜ ๋งํฌ๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š”")
lit.caption("https://main-biblify-space-rycont.endpoint.ainize.ai/")

model, tokenizer = loadModels()

MAX_LENGTH = 128

def biblifyWithBeams(beam, tokens, attention_mask):
  generated = model.generate(
    input_ids = torch.Tensor([ tokens ]).to(torch.int64),
    attention_mask = torch.Tensor([ attentionMasks ]).to(torch.int64),
    num_beams = beam,
    max_length = MAX_LENGTH,
    eos_token_id=tokenizer.eos_token_id,
    bad_words_ids=[[tokenizer.unk_token_id]]
  )[0]
  
  return tokenizer.decode(
    generated,
  ).replace('<s>', '').replace('</s>', '')

with lit.form("gen"):
  text_input = lit.text_input("๋ฌธ์žฅ ์ž…๋ ฅ")
  submitted = lit.form_submit_button("์ƒ์„ฑ")

if len(text_input.strip()) > 0:
  print(text_input)
  
  text_input = "<s>" + text_input + "</s>"
  
  tokens = tokenizer.encode(text_input)
  tokenLength = len(tokens)
  
  attentionMasks = [ 1 ] * tokenLength + [ 0 ] * (MAX_LENGTH - tokenLength)
  tokens = tokens + [ tokenizer.pad_token_id ] * (MAX_LENGTH - tokenLength)
  
  results = []
  
  for i in range(10)[5:]:
    generated = biblifyWithBeams(
      i + 1,
      tokens,
      attentionMasks
    )
    if generated in results:
       print("์ค‘๋ณต๋จ")
       continue
       
    results.append(generated)
     
    with lit.expander(str(len(results)) + "๋ฒˆ์งธ ๊ฒฐ๊ณผ (" + str(i +1) + ")", True):
      lit.write(generated)
      print(generated)
     
    lit.caption("๋ฐ " + str(5 - len(results)) + " ๊ฐœ์˜ ์ค‘๋ณต๋œ ๊ฒฐ๊ณผ")