Spaces:
Sleeping
Sleeping
Jonghyun Moon
commited on
Commit
ยท
26f786b
1
Parent(s):
9842d55
deploy streamlit
Browse files- .gitignore +129 -0
- app.py +312 -0
- ballad_word.csv +0 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
pip-wheel-metadata/
|
24 |
+
share/python-wheels/
|
25 |
+
*.egg-info/
|
26 |
+
.installed.cfg
|
27 |
+
*.egg
|
28 |
+
MANIFEST
|
29 |
+
|
30 |
+
# PyInstaller
|
31 |
+
# Usually these files are written by a python script from a template
|
32 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
33 |
+
*.manifest
|
34 |
+
*.spec
|
35 |
+
|
36 |
+
# Installer logs
|
37 |
+
pip-log.txt
|
38 |
+
pip-delete-this-directory.txt
|
39 |
+
|
40 |
+
# Unit test / coverage reports
|
41 |
+
htmlcov/
|
42 |
+
.tox/
|
43 |
+
.nox/
|
44 |
+
.coverage
|
45 |
+
.coverage.*
|
46 |
+
.cache
|
47 |
+
nosetests.xml
|
48 |
+
coverage.xml
|
49 |
+
*.cover
|
50 |
+
*.py,cover
|
51 |
+
.hypothesis/
|
52 |
+
.pytest_cache/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
target/
|
76 |
+
|
77 |
+
# Jupyter Notebook
|
78 |
+
.ipynb_checkpoints
|
79 |
+
|
80 |
+
# IPython
|
81 |
+
profile_default/
|
82 |
+
ipython_config.py
|
83 |
+
|
84 |
+
# pyenv
|
85 |
+
.python-version
|
86 |
+
|
87 |
+
# pipenv
|
88 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
89 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
90 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
91 |
+
# install all needed dependencies.
|
92 |
+
#Pipfile.lock
|
93 |
+
|
94 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
95 |
+
__pypackages__/
|
96 |
+
|
97 |
+
# Celery stuff
|
98 |
+
celerybeat-schedule
|
99 |
+
celerybeat.pid
|
100 |
+
|
101 |
+
# SageMath parsed files
|
102 |
+
*.sage.py
|
103 |
+
|
104 |
+
# Environments
|
105 |
+
.env
|
106 |
+
.venv
|
107 |
+
env/
|
108 |
+
venv/
|
109 |
+
ENV/
|
110 |
+
env.bak/
|
111 |
+
venv.bak/
|
112 |
+
|
113 |
+
# Spyder project settings
|
114 |
+
.spyderproject
|
115 |
+
.spyproject
|
116 |
+
|
117 |
+
# Rope project settings
|
118 |
+
.ropeproject
|
119 |
+
|
120 |
+
# mkdocs documentation
|
121 |
+
/site
|
122 |
+
|
123 |
+
# mypy
|
124 |
+
.mypy_cache/
|
125 |
+
.dmypy.json
|
126 |
+
dmypy.json
|
127 |
+
|
128 |
+
# Pyre type checker
|
129 |
+
.pyre/
|
app.py
ADDED
@@ -0,0 +1,312 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import requests
|
3 |
+
import streamlit as st
|
4 |
+
from streamlit_lottie import st_lottie
|
5 |
+
import torch
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
7 |
+
import re
|
8 |
+
|
9 |
+
# Page Config
|
10 |
+
st.set_page_config(
|
11 |
+
page_title="๋
ธ๋ ๊ฐ์ฌ nํ์ Beta",
|
12 |
+
page_icon="๐",
|
13 |
+
layout="wide"
|
14 |
+
)
|
15 |
+
# st.text(os.listdir(os.curdir))
|
16 |
+
|
17 |
+
### Model
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained("wumusill/final_project_kogpt2")
|
19 |
+
|
20 |
+
@st.cache(show_spinner=False)
|
21 |
+
def load_model():
|
22 |
+
model = AutoModelForCausalLM.from_pretrained("wumusill/final_project_kogpt2")
|
23 |
+
return model
|
24 |
+
|
25 |
+
model = load_model()
|
26 |
+
|
27 |
+
@st.cache(show_spinner=False)
|
28 |
+
def get_word():
|
29 |
+
word = pd.read_csv("ballad_word.csv", encoding="cp949")
|
30 |
+
return word
|
31 |
+
|
32 |
+
|
33 |
+
word = get_word()
|
34 |
+
|
35 |
+
|
36 |
+
one = word[word["0"].str.startswith("ํ")].sample(1).values[0][0]
|
37 |
+
# st.header(type(one))
|
38 |
+
# st.header(one)
|
39 |
+
|
40 |
+
|
41 |
+
# Class : Dict ์ค๋ณต ํค ์ถ๋ ฅ
|
42 |
+
class poem(object):
|
43 |
+
def __init__(self,letter):
|
44 |
+
self.letter = letter
|
45 |
+
|
46 |
+
def __str__(self):
|
47 |
+
return self.letter
|
48 |
+
|
49 |
+
def __repr__(self):
|
50 |
+
return "'"+self.letter+"'"
|
51 |
+
|
52 |
+
|
53 |
+
def beta_poem(input_letter):
|
54 |
+
# ๋์ ๋ฒ์น ์ฌ์
|
55 |
+
dooeum = {"๋ผ":"๋", "๋ฝ":"๋", "๋":"๋", "๋":"๋ ", "๋":"๋จ", "๋":"๋ฉ", "๋":"๋ญ",
|
56 |
+
"๋":"๋ด", "๋ญ":"๋", "๋":"์ฝ", "๋ต":"์ฝ", "๋ฅ":"์", "๋":"์", "๋
":"์ฌ",
|
57 |
+
"๋ ค":"์ฌ", "๋
":"์ญ", "๋ ฅ":"์ญ", "๋
":"์ฐ", "๋ จ":"์ฐ", "๋
":"์ด", "๋ ฌ":"์ด",
|
58 |
+
"๋
":"์ผ", "๋ ด":"์ผ", "๋ ต":"์ฝ", "๋
":"์", "๋ น":"์", "๋
":"์", "๋ก":"์",
|
59 |
+
"๋ก":"๋
ธ", "๋ก":"๋
น", "๋ก ":"๋
ผ", "๋กฑ":"๋", "๋ขฐ":"๋", "๋จ":"์", "๋ฃ":"์",
|
60 |
+
"๋ฃก":"์ฉ", "๋ฃจ":"๋", "๋ด":"์ ", "๋ฅ":"์ ", "๋ต":"์ก", "๋ฅ":"์ก", "๋ฅ":"์ค",
|
61 |
+
"๋ฅ ":"์จ", "๋ฅญ":"์ต", "๋ฅต":"๋", "๋ฆ":"๋ ", "๋ฆ":"๋ฅ", "๋":"์ด", "๋ฆฌ":"์ด",
|
62 |
+
"๋ฆฐ":'์ธ', '๋ฆผ':'์', '๋ฆฝ':'์
'}
|
63 |
+
# ๊ฒฐ๊ณผ๋ฌผ์ ๋ด์ list
|
64 |
+
res_l = []
|
65 |
+
len_sequence = 0
|
66 |
+
|
67 |
+
# ํ ๊ธ์์ฉ ์ธ๋ฑ์ค์ ํจ๊ป ๊ฐ์ ธ์ด
|
68 |
+
for idx, val in enumerate(input_letter):
|
69 |
+
# ๋์ ๋ฒ์น ์ ์ฉ
|
70 |
+
if val in dooeum.keys():
|
71 |
+
val = dooeum[val]
|
72 |
+
|
73 |
+
# ๋ฐ๋ผ๋์ ์๋ ๋จ์ด ์ ์ฉ
|
74 |
+
try:
|
75 |
+
one = word[word["0"].str.startswith(val)].sample(1).values[0][0]
|
76 |
+
# st.text(one)
|
77 |
+
except:
|
78 |
+
one = val
|
79 |
+
|
80 |
+
# ์ข๋ ๋งค๋๋ฌ์ด ์ผํ์๋ฅผ ์ํด ์ด์ ๋ฌธ์ฅ์ด๋ ํ์ฌ ์์ ์ฐ๊ฒฐ
|
81 |
+
# ์ดํ generate ๋ ๋ฌธ์ฅ์์ ์ด์ ๋ฌธ์ฅ์ ๋ํ ๋ฐ์ดํฐ ์ ๊ฑฐ
|
82 |
+
link_with_pre_sentence = (" ".join(res_l)+ " " + one + " " if idx != 0 else one).strip()
|
83 |
+
# print(link_with_pre_sentence)
|
84 |
+
|
85 |
+
# ์ฐ๊ฒฐ๋ ๋ฌธ์ฅ์ ์ธ์ฝ๋ฉ
|
86 |
+
input_ids = tokenizer.encode(link_with_pre_sentence, add_special_tokens=False, return_tensors="pt")
|
87 |
+
|
88 |
+
# ์ธ์ฝ๋ฉ ๊ฐ์ผ๋ก ๋ฌธ์ฅ ์์ฑ
|
89 |
+
output_sequence = model.generate(
|
90 |
+
input_ids=input_ids,
|
91 |
+
do_sample=True,
|
92 |
+
max_length=42,
|
93 |
+
min_length=len_sequence + 2,
|
94 |
+
temperature=0.9,
|
95 |
+
repetition_penalty=1.5,
|
96 |
+
no_repeat_ngram_size=2)
|
97 |
+
|
98 |
+
# ์์ฑ๋ ๋ฌธ์ฅ ๋ฆฌ์คํธ๋ก ๋ณํ (์ธ์ฝ๋ฉ ๋์ด์๊ณ , ์์ฑ๋ ๋ฌธ์ฅ ๋ค๋ก padding ์ด ์๋ ์ํ)
|
99 |
+
generated_sequence = output_sequence.tolist()[0]
|
100 |
+
|
101 |
+
# padding index ์๊น์ง slicing ํจ์ผ๋ก์จ padding ์ ๊ฑฐ, padding์ด ์์ ์๋ ์๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด๋ฌธ ํ์ธ ํ ์ ๊ฑฐ
|
102 |
+
# ์ฌ์ฉํ generated_sequence ๊ฐ 5๋ณด๋ค ์งง์ผ๋ฉด ๊ฐ์ ์ ์ผ๋ก ๊ธธ์ด๋ฅผ 8๋ก ํด์ค๋ค...
|
103 |
+
if tokenizer.pad_token_id in generated_sequence:
|
104 |
+
check_index = generated_sequence.index(tokenizer.pad_token_id)
|
105 |
+
check_index = check_index if check_index-len_sequence > 3 else len_sequence + 8
|
106 |
+
generated_sequence = generated_sequence[:check_index]
|
107 |
+
|
108 |
+
word_encode = tokenizer.encode(one, add_special_tokens=False, return_tensors="pt").tolist()[0][0]
|
109 |
+
split_index = len(generated_sequence) - 1 - generated_sequence[::-1].index(word_encode)
|
110 |
+
|
111 |
+
# ์ฒซ ๊ธ์๊ฐ ์๋๋ผ๋ฉด, generate ๋ ์์ ๋ง ๊ฒฐ๊ณผ๋ฌผ list์ ๋ค์ด๊ฐ ์ ์๊ฒ ์ ๋ฌธ์ฅ์ ๋ํ ์ธ์ฝ๋ฉ ๊ฐ ์ ๊ฑฐ
|
112 |
+
generated_sequence = generated_sequence[split_index:]
|
113 |
+
|
114 |
+
# print(tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True, skip_special_tokens=True))
|
115 |
+
# ๋ค์ ์์ ์ ์ํด ๊ธธ์ด ๊ฐฑ์
|
116 |
+
len_sequence += len([elem for elem in generated_sequence if elem not in(tokenizer.all_special_ids)])
|
117 |
+
# ๊ฒฐ๊ณผ๋ฌผ ๋์ฝ๋ฉ
|
118 |
+
decoded_sequence = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True, skip_special_tokens=True)
|
119 |
+
|
120 |
+
# ๊ฒฐ๊ณผ๋ฌผ ๋ฆฌ์คํธ์ ๋ด๊ธฐ
|
121 |
+
res_l.append(decoded_sequence)
|
122 |
+
|
123 |
+
poem_dict = {"Type":"beta"}
|
124 |
+
|
125 |
+
for letter, res in zip(input_letter, res_l):
|
126 |
+
# decode_res = tokenizer.decode(res, clean_up_tokenization_spaces=True, skip_special_tokens=True)
|
127 |
+
poem_dict[poem(letter)] = res
|
128 |
+
|
129 |
+
return poem_dict
|
130 |
+
|
131 |
+
def alpha_poem(input_letter):
|
132 |
+
|
133 |
+
# ๋์ ๋ฒ์น ์ฌ์
|
134 |
+
dooeum = {"๋ผ":"๋", "๋ฝ":"๋", "๋":"๋", "๋":"๋ ", "๋":"๋จ", "๋":"๋ฉ", "๋":"๋ญ",
|
135 |
+
"๋":"๋ด", "๋ญ":"๋", "๋":"์ฝ", "๋ต":"์ฝ", "๋ฅ":"์", "๋":"์", "๋
":"์ฌ",
|
136 |
+
"๋ ค":"์ฌ", "๋
":"์ญ", "๋ ฅ":"์ญ", "๋
":"์ฐ", "๋ จ":"์ฐ", "๋
":"์ด", "๋ ฌ":"์ด",
|
137 |
+
"๋
":"์ผ", "๋ ด":"์ผ", "๋ ต":"์ฝ", "๋
":"์", "๋ น":"์", "๋
":"์", "๋ก":"์",
|
138 |
+
"๋ก":"๋
ธ", "๋ก":"๋
น", "๋ก ":"๋
ผ", "๋กฑ":"๋", "๋ขฐ":"๋", "๋จ":"์", "๋ฃ":"์",
|
139 |
+
"๋ฃก":"์ฉ", "๋ฃจ":"๋", "๋ด":"์ ", "๋ฅ":"์ ", "๋ต":"์ก", "๋ฅ":"์ก", "๋ฅ":"์ค",
|
140 |
+
"๋ฅ ":"์จ", "๋ฅญ":"์ต", "๋ฅต":"๋", "๋ฆ":"๋ ", "๋ฆ":"๋ฅ", "๋":"์ด", "๋ฆฌ":"์ด",
|
141 |
+
"๋ฆฐ":'์ธ', '๋ฆผ':'์', '๋ฆฝ':'์
'}
|
142 |
+
# ๊ฒฐ๊ณผ๋ฌผ์ ๋ด์ list
|
143 |
+
res_l = []
|
144 |
+
|
145 |
+
# ํ ๊ธ์์ฉ ์ธ๋ฑ์ค์ ํจ๊ป ๊ฐ์ ธ์ด
|
146 |
+
for idx, val in enumerate(input_letter):
|
147 |
+
# ๋์ ๋ฒ์น ์ ์ฉ
|
148 |
+
if val in dooeum.keys():
|
149 |
+
val = dooeum[val]
|
150 |
+
|
151 |
+
|
152 |
+
while True:
|
153 |
+
# ๋ง์ฝ idx ๊ฐ 0 ์ด๋ผ๋ฉด == ์ฒซ ๊ธ์
|
154 |
+
if idx == 0:
|
155 |
+
# ์ฒซ ๊ธ์ ์ธ์ฝ๋ฉ
|
156 |
+
input_ids = tokenizer.encode(
|
157 |
+
val, add_special_tokens=False, return_tensors="pt")
|
158 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ : {input_ids}\n") # 2์ฐจ์ ํ
์
|
159 |
+
|
160 |
+
# ์ฒซ ๊ธ์ ์ธ์ฝ๋ฉ ๊ฐ์ผ๋ก ๋ฌธ์ฅ ์์ฑ
|
161 |
+
output_sequence = model.generate(
|
162 |
+
input_ids=input_ids,
|
163 |
+
do_sample=True,
|
164 |
+
max_length=42,
|
165 |
+
min_length=5,
|
166 |
+
temperature=0.9,
|
167 |
+
repetition_penalty=1.7,
|
168 |
+
no_repeat_ngram_size=2)[0]
|
169 |
+
# print("์ฒซ ๊ธ์ ์ธ์ฝ๋ฉ ํ generate ๊ฒฐ๊ณผ:", output_sequence, "\n") # tensor
|
170 |
+
|
171 |
+
# ์ฒซ ๊ธ์๊ฐ ์๋๋ผ๋ฉด
|
172 |
+
else:
|
173 |
+
# ํ ์์
|
174 |
+
input_ids = tokenizer.encode(
|
175 |
+
val, add_special_tokens=False, return_tensors="pt")
|
176 |
+
# print(f"{idx}๋ฒ ์งธ ๊ธ์ ์ธ์ฝ๋ฉ : {input_ids} \n")
|
177 |
+
|
178 |
+
# ์ข๋ ๋งค๋๋ฌ์ด ์ผํ์๋ฅผ ์ํด ์ด์ ์ธ์ฝ๋ฉ๊ณผ ์ง๊ธ ์ธ์ฝ๋ฉ ์ฐ๊ฒฐ
|
179 |
+
link_with_pre_sentence = torch.cat((generated_sequence, input_ids[0]), 0)
|
180 |
+
link_with_pre_sentence = torch.reshape(link_with_pre_sentence, (1, len(link_with_pre_sentence)))
|
181 |
+
# print(f"์ด์ ํ
์์ ์ฐ๊ฒฐ๋ ํ
์ {link_with_pre_sentence} \n")
|
182 |
+
|
183 |
+
# ์ธ์ฝ๋ฉ ๊ฐ์ผ๋ก ๋ฌธ์ฅ ์์ฑ
|
184 |
+
output_sequence = model.generate(
|
185 |
+
input_ids=link_with_pre_sentence,
|
186 |
+
do_sample=True,
|
187 |
+
max_length=42,
|
188 |
+
min_length=5,
|
189 |
+
temperature=0.9,
|
190 |
+
repetition_penalty=1.7,
|
191 |
+
no_repeat_ngram_size=2)[0]
|
192 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ ํ generate : {output_sequence}")
|
193 |
+
|
194 |
+
# ์์ฑ๋ ๋ฌธ์ฅ ๋ฆฌ์คํธ๋ก ๋ณํ (์ธ์ฝ๋ฉ ๋์ด์๊ณ , ์์ฑ๋ ๋ฌธ์ฅ ๋ค๋ก padding ์ด ์๋ ์ํ)
|
195 |
+
generated_sequence = output_sequence.tolist()
|
196 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ ๋ฆฌ์คํธ : {generated_sequence} \n")
|
197 |
+
|
198 |
+
# padding index ์๊น์ง slicing ํจ์ผ๋ก์จ padding ์ ๊ฑฐ, padding์ด ์์ ์๋ ์๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด๋ฌธ ํ์ธ ํ ์ ๊ฑฐ
|
199 |
+
if tokenizer.pad_token_id in generated_sequence:
|
200 |
+
generated_sequence = generated_sequence[:generated_sequence.index(tokenizer.pad_token_id)]
|
201 |
+
|
202 |
+
generated_sequence = torch.tensor(generated_sequence)
|
203 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ ๋ฆฌ์คํธ ํจ๋ฉ ์ ๊ฑฐ ํ ๋ค์ ํ
์ : {generated_sequence} \n")
|
204 |
+
|
205 |
+
# ์ฒซ ๊ธ์๊ฐ ์๋๋ผ๋ฉด, generate ๋ ์์ ๋ง ๊ฒฐ๊ณผ๋ฌผ list์ ๋ค์ด๊ฐ ์ ์๊ฒ ์ ๋ฌธ์ฅ์ ๋ํ ์ธ์ฝ๋ฉ ๊ฐ ์ ๊ฑฐ
|
206 |
+
# print(generated_sequence)
|
207 |
+
if idx != 0:
|
208 |
+
# ์ด์ ๋ฌธ์ฅ์ ๊ธธ์ด ์ดํ๋ก ์ฌ๋ผ์ด์ฑํด์ ์ ๋ฌธ์ฅ ์ ๊ฑฐ
|
209 |
+
generated_sequence = generated_sequence[len_sequence:]
|
210 |
+
|
211 |
+
len_sequence = len(generated_sequence)
|
212 |
+
# print("len_seq", len_sequence)
|
213 |
+
|
214 |
+
# ์์ ๊ทธ๋๋ก ๋ฑ์ผ๋ฉด ๋ค์ ํด์, ์๋๋ฉด while๋ฌธ ํ์ถ
|
215 |
+
if len_sequence > 1:
|
216 |
+
break
|
217 |
+
|
218 |
+
# ๊ฒฐ๊ณผ๋ฌผ ๋ฆฌ์คํธ์ ๋ด๊ธฐ
|
219 |
+
res_l.append(generated_sequence)
|
220 |
+
|
221 |
+
poem_dict = {"Type":"alpha"}
|
222 |
+
|
223 |
+
for letter, res in zip(input_letter, res_l):
|
224 |
+
decode_res = tokenizer.decode(res, clean_up_tokenization_spaces=True, skip_special_tokens=True)
|
225 |
+
poem_dict[poem(letter)] = decode_res
|
226 |
+
|
227 |
+
return poem_dict
|
228 |
+
|
229 |
+
# Image(.gif)
|
230 |
+
@st.cache(show_spinner=False)
|
231 |
+
def load_lottieurl(url: str):
|
232 |
+
r = requests.get(url)
|
233 |
+
if r.status_code != 200:
|
234 |
+
return None
|
235 |
+
return r.json()
|
236 |
+
|
237 |
+
lottie_url = "https://assets7.lottiefiles.com/private_files/lf30_fjln45y5.json"
|
238 |
+
|
239 |
+
lottie_json = load_lottieurl(lottie_url)
|
240 |
+
st_lottie(lottie_json, speed=1, height=200, key="initial")
|
241 |
+
|
242 |
+
|
243 |
+
# Title
|
244 |
+
row0_spacer1, row0_1, row0_spacer2, row0_2, row0_spacer3 = st.columns(
|
245 |
+
(0.01, 2, 0.05, 0.5, 0.01)
|
246 |
+
)
|
247 |
+
|
248 |
+
with row0_1:
|
249 |
+
st.markdown("# ํ๊ธ ๋
ธ๋ ๊ฐ์ฌ nํ์โ")
|
250 |
+
st.markdown("### ๐ฆ๋ฉ์์ด์ฌ์์ฒ๋ผ AIS7๐ฆ - ํ์ด๋ ํ๋ก์ ํธ")
|
251 |
+
|
252 |
+
with row0_2:
|
253 |
+
st.write("")
|
254 |
+
st.write("")
|
255 |
+
st.write("")
|
256 |
+
st.subheader("1์กฐ - ํดํ๋ฆฌ")
|
257 |
+
st.write("์ด์งํ, ์ต์ง์, ๊ถ์ํฌ, ๋ฌธ์ข
ํ, ๊ตฌ์ํ, ๊น์์ค")
|
258 |
+
|
259 |
+
st.write('---')
|
260 |
+
|
261 |
+
# Explanation
|
262 |
+
row1_spacer1, row1_1, row1_spacer2 = st.columns((0.01, 0.01, 0.01))
|
263 |
+
|
264 |
+
with row1_1:
|
265 |
+
st.markdown("### nํ์ ๊ฐ์ด๋๋ผ์ธ")
|
266 |
+
st.markdown("1. ํ๋จ์ ์๋ ํ
์คํธ๋ฐ์ 5์ ์ดํ ๋จ์ด๋ฅผ ๋ฃ์ด์ฃผ์ธ์")
|
267 |
+
st.markdown("2. 'nํ์ ์ ์ํ๊ธฐ' ๋ฒํผ์ ํด๋ฆญํด์ฃผ์ธ์")
|
268 |
+
st.markdown("* nํ์ ํ์
์ค์ \n"
|
269 |
+
" * Alpha ver. : ๋ชจ๋ธ์ด ์ฒซ ์์ ๋ถํฐ ์์ฑ\n"
|
270 |
+
" * Beta ver. : ์ฒซ ์์ ์ ๋ฐ์ดํฐ์
์์ ์ฐพ๊ณ , ๋ค์ ๋ถ๋ถ์ ์์ฑ")
|
271 |
+
|
272 |
+
st.write('---')
|
273 |
+
|
274 |
+
# Model & Input
|
275 |
+
row2_spacer1, row2_1, row2_spacer2= st.columns((0.01, 0.01, 0.01))
|
276 |
+
|
277 |
+
col1, col2 = st.columns(2)
|
278 |
+
|
279 |
+
# Word Input
|
280 |
+
with row2_1:
|
281 |
+
|
282 |
+
with col1:
|
283 |
+
genre = st.radio(
|
284 |
+
"nํ์ ํ์
์ ํ",
|
285 |
+
('Alpha', 'Beta(test์ค)'))
|
286 |
+
|
287 |
+
if genre == 'Alpha':
|
288 |
+
n_line_poem = alpha_poem
|
289 |
+
|
290 |
+
else:
|
291 |
+
n_line_poem = beta_poem
|
292 |
+
|
293 |
+
with col2:
|
294 |
+
word_input = st.text_input(
|
295 |
+
"nํ์์ ์ฌ์ฉํ ๋จ์ด๋ฅผ ์ ๊ณ ๋ฒํผ์ ๋๋ฌ์ฃผ์ธ์.(์ต๋ 5์) ๐",
|
296 |
+
placeholder='ํ๊ธ ๋จ์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์',
|
297 |
+
max_chars=5
|
298 |
+
)
|
299 |
+
word_input = re.sub("[^๊ฐ-ํฃ]", "", word_input)
|
300 |
+
|
301 |
+
if st.button('nํ์ ์ ์ํ๊ธฐ'):
|
302 |
+
if word_input == "":
|
303 |
+
st.error("์จ์ ํ ํ๊ธ ๋จ์ด๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์!")
|
304 |
+
|
305 |
+
else:
|
306 |
+
st.write("nํ์ ๋จ์ด : ", word_input)
|
307 |
+
with st.spinner('์ ์ ๊ธฐ๋ค๋ ค์ฃผ์ธ์...'):
|
308 |
+
result = n_line_poem(word_input)
|
309 |
+
st.success('์๋ฃ๋์ต๋๋ค!')
|
310 |
+
for r in result:
|
311 |
+
st.write(f'{r} : {result[r]}')
|
312 |
+
|
ballad_word.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
# numpy
|
3 |
+
streamlit
|
4 |
+
requests
|
5 |
+
streamlit_lottie
|
6 |
+
torch
|
7 |
+
transformers
|