Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,19 @@
|
|
|
|
|
| 1 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 6 |
-
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 7 |
-
|
| 8 |
-
# ๊ฐ์ ์ ๋ฐ๋ผ prefix ๋ฌธ์ฅ ์์ฑ
|
| 9 |
-
if emotion == "happy":
|
| 10 |
-
prefix = "์ค๋์ ๊ธฐ๋ถ์ด ์ข์์. "
|
| 11 |
-
elif emotion == "sad":
|
| 12 |
-
prefix = "์ฌํ ๊ธฐ๋ถ์ด์์. "
|
| 13 |
-
elif emotion == "angry":
|
| 14 |
-
prefix = "ํ๊ฐ ์น๋ฐ์ด ์ค๋ฅด๋ ๊ธฐ๋ถ์ด์์. "
|
| 15 |
-
else:
|
| 16 |
-
prefix = "์ค๋์ ๊ธฐ๋ถ์ด ์ด์ํด์. "
|
| 17 |
-
|
| 18 |
-
# prefix๋ฅผ ํ ํฌ๋์ด์งํ์ฌ ์
๋ ฅ ์ํ์ค ์์ฑ
|
| 19 |
-
input_sequence = tokenizer.encode(prefix, return_tensors="pt")
|
| 20 |
-
|
| 21 |
-
# ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ํ
์คํธ ์์ฑ
|
| 22 |
-
output = model.generate(
|
| 23 |
-
input_sequence,
|
| 24 |
-
max_length=max_length,
|
| 25 |
-
num_return_sequences=num_samples,
|
| 26 |
-
temperature=temperature,
|
| 27 |
-
pad_token_id=tokenizer.eos_token_id
|
| 28 |
-
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
if emotion.lower() in ['happy', 'sad', 'angry']:
|
| 40 |
-
break
|
| 41 |
-
else:
|
| 42 |
-
print("์
๋ ฅ๋ ๊ฐ์ ์ด ์๋ชป๋์์ต๋๋ค. ๋ค์ ์
๋ ฅํด์ฃผ์ธ์.")
|
| 43 |
-
except EOFError:
|
| 44 |
-
print("\n์ฌ์ฉ์ ์
๋ ฅ์ด ์ข
๋ฃ๋์์ต๋๋ค. ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค.")
|
| 45 |
-
return
|
| 46 |
-
|
| 47 |
-
# ๋น ๋ฌธ์์ด์ด ์
๋ ฅ๋์์ ๋
|
| 48 |
-
if not emotion:
|
| 49 |
-
print("๊ฐ์ ์ ์
๋ ฅํด์ฃผ์ธ์.")
|
| 50 |
-
|
| 51 |
-
# ์ผ๊ธฐ ์์ฑ
|
| 52 |
-
diary_entries = generate_diary(emotion)
|
| 53 |
# ์์ฑ๋ ์ผ๊ธฐ ์ถ๋ ฅ
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
print(f"{i}. {entry}")
|
| 57 |
-
|
| 58 |
-
if __name__ == "__main__":
|
| 59 |
-
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 3 |
+
st.title("์๋ ์ผ๊ธฐ ์์ฑ๊ธฐ")
|
| 4 |
|
| 5 |
+
keywords = st.text_input("5๊ฐ์ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์ (์ผํ๋ก ๊ตฌ๋ถ)", "")
|
| 6 |
+
keyword_list = [kw.strip() for kw in keywords.split(",")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
if len(keyword_list) == 5 and st.button("์ผ๊ธฐ ์ฐ๊ธฐ"):
|
| 9 |
+
# ๋ชจ๋ธ ๋ฐ ํ ํฌ๋์ด์ ๋ก๋
|
| 10 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 11 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 12 |
+
|
| 13 |
+
# ํค์๋ ๊ธฐ๋ฐ fine-tuning
|
| 14 |
+
input_ids = tokenizer.encode(" ".join(keyword_list), return_tensors="pt")
|
| 15 |
+
output = model.generate(input_ids, max_length=500, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95, num_beams=5)
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# ์์ฑ๋ ์ผ๊ธฐ ์ถ๋ ฅ
|
| 18 |
+
diary = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 19 |
+
st.write(diary)
|
|
|
|
|
|
|
|
|
|
|
|