Spaces:
Runtime error
Runtime error
dk-davidekim
commited on
Commit
โข
2220c11
1
Parent(s):
4c192ae
Upload 7 files (#1)
Browse files- Upload 7 files (741d4a729598033269d9e07f115b9871b0511257)
- .gitattributes +1 -0
- LICENSE +21 -0
- README.md +2 -11
- ballad_word.csv +3 -0
- main.py +198 -0
- pages/beta.py +312 -0
- requirements.txt +0 -6
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
ballad_word.csv filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2022 happyFinal
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,12 +1,3 @@
|
|
1 |
-
|
2 |
-
title: HappyFinal Streamlit
|
3 |
-
emoji: ๐จ
|
4 |
-
colorFrom: red
|
5 |
-
colorTo: yellow
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.15.2
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
|
12 |
-
|
|
|
1 |
+
# streamlit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
https://happyfinal-streamlit-main-cjyf11.streamlit.app/
|
ballad_word.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ccbc1980b49a3efe1bc291f04987d6d523fe0366621ae473e9a5162d103aa738
|
3 |
+
size 1852955
|
main.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_lottie import st_lottie
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
+
import re
|
7 |
+
|
8 |
+
# Page Config
|
9 |
+
st.set_page_config(
|
10 |
+
page_title="๋
ธ๋ ๊ฐ์ฌ nํ์",
|
11 |
+
page_icon="๐",
|
12 |
+
layout="wide"
|
13 |
+
)
|
14 |
+
|
15 |
+
### Model
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained("wumusill/final_project_kogpt2")
|
17 |
+
|
18 |
+
@st.cache(show_spinner=False)
|
19 |
+
def load_model():
|
20 |
+
model = AutoModelForCausalLM.from_pretrained("wumusill/final_project_kogpt2")
|
21 |
+
return model
|
22 |
+
|
23 |
+
model = load_model()
|
24 |
+
|
25 |
+
# Class : Dict ์ค๋ณต ํค ์ถ๋ ฅ
|
26 |
+
class poem(object):
|
27 |
+
def __init__(self,letter):
|
28 |
+
self.letter = letter
|
29 |
+
|
30 |
+
def __str__(self):
|
31 |
+
return self.letter
|
32 |
+
|
33 |
+
def __repr__(self):
|
34 |
+
return "'"+self.letter+"'"
|
35 |
+
|
36 |
+
|
37 |
+
def n_line_poem(input_letter):
|
38 |
+
|
39 |
+
# ๋์ ๋ฒ์น ์ฌ์
|
40 |
+
dooeum = {"๋ผ":"๋", "๋ฝ":"๋", "๋":"๋", "๋":"๋ ", "๋":"๋จ", "๋":"๋ฉ", "๋":"๋ญ",
|
41 |
+
"๋":"๋ด", "๋ญ":"๋", "๋":"์ฝ", "๋ต":"์ฝ", "๋ฅ":"์", "๋":"์", "๋
":"์ฌ",
|
42 |
+
"๋ ค":"์ฌ", "๋
":"์ญ", "๋ ฅ":"์ญ", "๋
":"์ฐ", "๋ จ":"์ฐ", "๋
":"์ด", "๋ ฌ":"์ด",
|
43 |
+
"๋
":"์ผ", "๋ ด":"์ผ", "๋ ต":"์ฝ", "๋
":"์", "๋ น":"์", "๋
":"์", "๋ก":"์",
|
44 |
+
"๋ก":"๋
ธ", "๋ก":"๋
น", "๋ก ":"๋
ผ", "๋กฑ":"๋", "๋ขฐ":"๋", "๋จ":"์", "๋ฃ":"์",
|
45 |
+
"๋ฃก":"์ฉ", "๋ฃจ":"๋", "๋ด":"์ ", "๋ฅ":"์ ", "๋ต":"์ก", "๋ฅ":"์ก", "๋ฅ":"์ค",
|
46 |
+
"๋ฅ ":"์จ", "๋ฅญ":"์ต", "๋ฅต":"๋", "๋ฆ":"๋ ", "๋ฆ":"๋ฅ", "๋":"์ด", "๋ฆฌ":"์ด",
|
47 |
+
"๋ฆฐ":'์ธ', '๋ฆผ':'์', '๋ฆฝ':'์
'}
|
48 |
+
# ๊ฒฐ๊ณผ๋ฌผ์ ๋ด์ list
|
49 |
+
res_l = []
|
50 |
+
|
51 |
+
# ํ ๊ธ์์ฉ ์ธ๋ฑ์ค์ ํจ๊ป ๊ฐ์ ธ์ด
|
52 |
+
for idx, val in enumerate(input_letter):
|
53 |
+
# ๋์ ๋ฒ์น ์ ์ฉ
|
54 |
+
if val in dooeum.keys():
|
55 |
+
val = dooeum[val]
|
56 |
+
|
57 |
+
|
58 |
+
while True:
|
59 |
+
# ๋ง์ฝ idx ๊ฐ 0 ์ด๋ผ๋ฉด == ์ฒซ ๊ธ์
|
60 |
+
if idx == 0:
|
61 |
+
# ์ฒซ ๊ธ์ ์ธ์ฝ๋ฉ
|
62 |
+
input_ids = tokenizer.encode(
|
63 |
+
val, add_special_tokens=False, return_tensors="pt")
|
64 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ : {input_ids}\n") # 2์ฐจ์ ํ
์
|
65 |
+
|
66 |
+
# ์ฒซ ๊ธ์ ์ธ์ฝ๋ฉ ๊ฐ์ผ๋ก ๋ฌธ์ฅ ์์ฑ
|
67 |
+
output_sequence = model.generate(
|
68 |
+
input_ids=input_ids,
|
69 |
+
do_sample=True, max_length=42,
|
70 |
+
min_length=5, temperature=0.9, repetition_penalty=1.5,
|
71 |
+
no_repeat_ngram_size=2)[0]
|
72 |
+
# print("์ฒซ ๊ธ์ ์ธ์ฝ๋ฉ ํ generate ๊ฒฐ๊ณผ:", output_sequence, "\n") # tensor
|
73 |
+
|
74 |
+
# ์ฒซ ๊ธ์๊ฐ ์๋๋ผ๋ฉด
|
75 |
+
else:
|
76 |
+
# ํ ์์
|
77 |
+
input_ids = tokenizer.encode(
|
78 |
+
val, add_special_tokens=False, return_tensors="pt")
|
79 |
+
# print(f"{idx}๋ฒ ์งธ ๊ธ์ ์ธ์ฝ๋ฉ : {input_ids} \n")
|
80 |
+
|
81 |
+
# ์ข๋ ๋งค๋๋ฌ์ด ์ผํ์๋ฅผ ์ํด ์ด์ ์ธ์ฝ๋ฉ๊ณผ ์ง๊ธ ์ธ์ฝ๋ฉ ์ฐ๊ฒฐ
|
82 |
+
link_with_pre_sentence = torch.cat((generated_sequence, input_ids[0]), 0)
|
83 |
+
link_with_pre_sentence = torch.reshape(link_with_pre_sentence, (1, len(link_with_pre_sentence)))
|
84 |
+
# print(f"์ด์ ํ
์์ ์ฐ๊ฒฐ๋ ํ
์ {link_with_pre_sentence} \n")
|
85 |
+
|
86 |
+
# ์ธ์ฝ๋ฉ ๊ฐ์ผ๋ก ๋ฌธ์ฅ ์์ฑ
|
87 |
+
output_sequence = model.generate(
|
88 |
+
input_ids=link_with_pre_sentence,
|
89 |
+
do_sample=True, max_length=42,
|
90 |
+
min_length=5, temperature=0.9, repetition_penalty=1.5,
|
91 |
+
no_repeat_ngram_size=2)[0]
|
92 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ ํ generate : {output_sequence}")
|
93 |
+
|
94 |
+
# ์์ฑ๋ ๋ฌธ์ฅ ๋ฆฌ์คํธ๋ก ๋ณํ (์ธ์ฝ๋ฉ ๋์ด์๊ณ , ์์ฑ๋ ๋ฌธ์ฅ ๋ค๋ก padding ์ด ์๋ ์ํ)
|
95 |
+
generated_sequence = output_sequence.tolist()
|
96 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ ๋ฆฌ์คํธ : {generated_sequence} \n")
|
97 |
+
|
98 |
+
# padding index ์๊น์ง slicing ํจ์ผ๋ก์จ padding ์ ๊ฑฐ, padding์ด ์์ ์๋ ์๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด๋ฌธ ํ์ธ ํ ์ ๊ฑฐ
|
99 |
+
if tokenizer.pad_token_id in generated_sequence:
|
100 |
+
generated_sequence = generated_sequence[:generated_sequence.index(tokenizer.pad_token_id)]
|
101 |
+
|
102 |
+
generated_sequence = torch.tensor(generated_sequence)
|
103 |
+
# print(f"{idx}๋ฒ ์ธ์ฝ๋ฉ ๋ฆฌ์คํธ ํจ๋ฉ ์ ๊ฑฐ ํ ๋ค์ ํ
์ : {generated_sequence} \n")
|
104 |
+
|
105 |
+
# ์ฒซ ๊ธ์๊ฐ ์๋๋ผ๋ฉด, generate ๋ ์์ ๋ง ๊ฒฐ๊ณผ๋ฌผ list์ ๋ค์ด๊ฐ ์ ์๊ฒ ์ ๋ฌธ์ฅ์ ๋ํ ์ธ์ฝ๋ฉ ๊ฐ ์ ๊ฑฐ
|
106 |
+
# print(generated_sequence)
|
107 |
+
if idx != 0:
|
108 |
+
# ์ด์ ๋ฌธ์ฅ์ ๊ธธ์ด ์ดํ๋ก ์ฌ๋ผ์ด์ฑํด์ ์ ๋ฌธ์ฅ ์ ๊ฑฐ
|
109 |
+
generated_sequence = generated_sequence[len_sequence:]
|
110 |
+
|
111 |
+
len_sequence = len(generated_sequence)
|
112 |
+
# print("len_seq", len_sequence)
|
113 |
+
|
114 |
+
# ์์ ๊ทธ๋๋ก ๋ฑ์ผ๋ฉด ๋ค์ ํด์, ์๋๋ฉด while๋ฌธ ํ์ถ
|
115 |
+
if len_sequence > 1:
|
116 |
+
break
|
117 |
+
|
118 |
+
# ๊ฒฐ๊ณผ๋ฌผ ๋ฆฌ์คํธ์ ๋ด๊ธฐ
|
119 |
+
res_l.append(generated_sequence)
|
120 |
+
|
121 |
+
poem_dict = {}
|
122 |
+
|
123 |
+
for letter, res in zip(input_letter, res_l):
|
124 |
+
decode_res = tokenizer.decode(res, clean_up_tokenization_spaces=True, skip_special_tokens=True)
|
125 |
+
poem_dict[poem(letter)] = decode_res
|
126 |
+
|
127 |
+
return poem_dict
|
128 |
+
|
129 |
+
###
|
130 |
+
|
131 |
+
# Image(.gif)
|
132 |
+
@st.cache(show_spinner=False)
|
133 |
+
def load_lottieurl(url: str):
|
134 |
+
r = requests.get(url)
|
135 |
+
if r.status_code != 200:
|
136 |
+
return None
|
137 |
+
return r.json()
|
138 |
+
|
139 |
+
lottie_url = "https://assets7.lottiefiles.com/private_files/lf30_fjln45y5.json"
|
140 |
+
|
141 |
+
lottie_json = load_lottieurl(lottie_url)
|
142 |
+
st_lottie(lottie_json, speed=1, height=200, key="initial")
|
143 |
+
|
144 |
+
|
145 |
+
# Title
|
146 |
+
row0_spacer1, row0_1, row0_spacer2, row0_2, row0_spacer3 = st.columns(
|
147 |
+
(0.01, 2, 0.05, 0.5, 0.01)
|
148 |
+
)
|
149 |
+
|
150 |
+
with row0_1:
|
151 |
+
st.markdown("# ํ๊ธ ๋
ธ๋ ๊ฐ์ฌ nํ์โ")
|
152 |
+
st.markdown("### ๐ฆ๋ฉ์์ด์ฌ์์ฒ๋ผ AIS7๐ฆ - ํ์ด๋ ํ๋ก์ ํธ")
|
153 |
+
|
154 |
+
with row0_2:
|
155 |
+
st.write("")
|
156 |
+
st.write("")
|
157 |
+
st.write("")
|
158 |
+
st.subheader("1์กฐ - ํดํ๋ฆฌ")
|
159 |
+
st.write("์ด์งํ, ์ต์ง์, ๊ถ์ํฌ, ๋ฌธ์ข
ํ, ๊ตฌ์ํ, ๊น์์ค")
|
160 |
+
|
161 |
+
st.write('---')
|
162 |
+
|
163 |
+
# Explanation
|
164 |
+
row1_spacer1, row1_1, row1_spacer2 = st.columns((0.01, 0.01, 0.01))
|
165 |
+
|
166 |
+
with row1_1:
|
167 |
+
st.markdown("### nํ์ ๊ฐ์ด๋๋ผ์ธ")
|
168 |
+
st.markdown("1. ํ๋จ์ ์๋ ํ
์คํธ๋ฐ์ 5์ ์ดํ ํ๊ธ ๋จ์ด๋ฅผ ๋ฃ์ด์ฃผ์ธ์")
|
169 |
+
st.markdown("2. 'nํ์ ์ ์ํ๊ธฐ' ๋ฒํผ์ ํด๋ฆญํด์ฃผ์ธ์")
|
170 |
+
|
171 |
+
st.write('---')
|
172 |
+
|
173 |
+
# Model & Input
|
174 |
+
row2_spacer1, row2_1, row2_spacer2= st.columns((0.01, 0.01, 0.01))
|
175 |
+
|
176 |
+
# Word Input
|
177 |
+
with row2_1:
|
178 |
+
word_input = st.text_input(
|
179 |
+
"nํ์์ ์ฌ์ฉํ ํ๊ธ ๋จ์ด๋ฅผ ์ ๊ณ ๋ฒํผ์ ๋๋ฌ์ฃผ์ธ์.(์ต๋ 5์) ๐",
|
180 |
+
placeholder='ํ๊ธ ๋จ์ด๋ฅผ ์
๋ ฅํด์ฃผ์ธ์',
|
181 |
+
max_chars=5
|
182 |
+
)
|
183 |
+
word_input = re.sub("[^๊ฐ-ํฃ]", "", word_input)
|
184 |
+
|
185 |
+
if st.button('nํ์ ์ ์ํ๊ธฐ'):
|
186 |
+
if word_input == "":
|
187 |
+
st.error("์จ์ ํ ํ๊ธ ๋จ์ด๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์!")
|
188 |
+
|
189 |
+
else:
|
190 |
+
st.write("nํ์ ๋จ์ด : ", word_input)
|
191 |
+
with st.spinner('์ ์ ๊ธฐ๋ค๋ ค์ฃผ์ธ์...'):
|
192 |
+
result = n_line_poem(word_input)
|
193 |
+
st.success('์๋ฃ๋์ต๋๋ค!')
|
194 |
+
for r in result:
|
195 |
+
st.write(f'{r} : {result[r]}')
|
196 |
+
|
197 |
+
|
198 |
+
|
pages/beta.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 |
+
|
requirements.txt
CHANGED
@@ -1,13 +1,7 @@
|
|
1 |
pandas
|
2 |
# numpy
|
3 |
streamlit
|
4 |
-
# plotly
|
5 |
-
# seaborn
|
6 |
-
# matplotlib
|
7 |
requests
|
8 |
streamlit_lottie
|
9 |
-
# streamlit_folium
|
10 |
-
# koreanize_matplotlib
|
11 |
-
# tensorflow
|
12 |
torch
|
13 |
transformers
|
|
|
1 |
pandas
|
2 |
# numpy
|
3 |
streamlit
|
|
|
|
|
|
|
4 |
requests
|
5 |
streamlit_lottie
|
|
|
|
|
|
|
6 |
torch
|
7 |
transformers
|