File size: 3,769 Bytes
39c2220 |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# test.py (์ค๋ฅ ์์ ์ต์ข
์ฝ๋)
import numpy as np
import tensorflow as tf
from tensorflow import keras
# from_pretrained_keras ๋์ hf_hub_download๋ฅผ ์ฌ์ฉํฉ๋๋ค.
from huggingface_hub import hf_hub_download
print("TensorFlow ๋ฒ์ :", tf.__version__)
# 1. Hugging Face Hub์์ ๋ชจ๋ธ ํ์ผ ๋ค์ด๋ก๋ ํ ๋ก๋
REPO_ID = "OneclickAI/LSTM_GUE_test_Model"
print(f"\n'{REPO_ID}' ์ ์ฅ์์์ ๋ชจ๋ธ ํ์ผ์ ์์น๋ฅผ ํ์ธํฉ๋๋ค...")
try:
# 1๋จ๊ณ: hf_hub_download๋ก ํ์ผ์ ๋ก์ปฌ ์บ์ ๊ฒฝ๋ก๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
# ํ์ผ์ด ์ด๋ฏธ ๋ค์ด๋ก๋ ๋์๋ค๋ฉด, ๋ค์ด๋ก๋๋ฅผ ์๋ตํ๊ณ ๊ฒฝ๋ก๋ง ์ฆ์ ๋ฐํํฉ๋๋ค.
print("LSTM ๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ ์ค...")
lstm_model_path = hf_hub_download(repo_id=REPO_ID, filename="lstm_model.keras")
print(f"LSTM ๋ชจ๋ธ ํ์ผ ์์น: {lstm_model_path}")
print("GRU ๋ชจ๋ธ ๊ฒฝ๋ก ํ์ธ ์ค...")
gru_model_path = hf_hub_download(repo_id=REPO_ID, filename="gru_model.keras")
print(f"GRU ๋ชจ๋ธ ํ์ผ ์์น: {gru_model_path}")
# 2๋จ๊ณ: ๋ค์ด๋ก๋๋ ํ์ผ ๊ฒฝ๋ก๋ฅผ Keras์ ํ์ค load_model ํจ์๋ก ์ง์ ๋ก๋ํฉ๋๋ค.
print("\nKeras๋ก ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค...")
lstm_model = keras.models.load_model(lstm_model_path)
gru_model = keras.models.load_model(gru_model_path)
print("๋ชจ๋ธ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค.")
except Exception as e:
print(f"๋ชจ๋ธ ๋ก๋ฉ ์ค ์ค๋ฅ ๋ฐ์: {e}")
print("์ธํฐ๋ท ์ฐ๊ฒฐ ๋ฐ ์ ์ฅ์ ID, ํ์ผ๋ช
์ ํ์ธํด์ฃผ์ธ์.")
exit()
# IMDB ๋ฐ์ดํฐ์
์ ๋จ์ด ์ธ๋ฑ์ค ๋ก๋ ('๋จ์ด': ์ ์)
word_index = keras.datasets.imdb.get_word_index()
# 2. ์์ธกํ ๋ฆฌ๋ทฐ ๋ฌธ์ฅ ์ ์
review1 = "This movie was fantastic and wonderful. I really enjoyed it."
review2 = "It was a complete waste of time. The plot was terrible and the acting was bad."
# 3. ๋ฌธ์ฅ ์ ์ฒ๋ฆฌ ํจ์
def preprocess_text(text, word_index, maxlen=256):
"""
ํ
์คํธ๋ฅผ ๋ชจ๋ธ์ด ์ดํดํ ์ ์๋ ์ ์ ์ํ์ค๋ก ๋ณํํ๊ณ ํจ๋ฉํฉ๋๋ค.
"""
# ๋ฌธ์ฅ์ ์๋ฌธ์๋ก ๋ณํํ๊ณ ๋จ์ด ๋จ์๋ก ๋ถํ
tokens = text.lower().split()
# ๊ฐ ๋จ์ด๋ฅผ ์ ์ ์ธ๋ฑ์ค๋ก ๋ณํ (word_index์ ์์ผ๋ฉด 2๋ฒ ์ธ๋ฑ์ค'<unk>' ์ฌ์ฉ)
token_indices = [word_index.get(word, 2) for word in tokens]
# ์ํ์ค ํจ๋ฉ
padded_sequence = keras.preprocessing.sequence.pad_sequences([token_indices], maxlen=maxlen)
return padded_sequence
# 4. ๋ชจ๋ธ ์์ธก ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ ํจ์
def predict_review(review_text, model, model_name):
"""
์ ์ฒ๋ฆฌ๋ ํ
์คํธ๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฑ ๋ถ์์ ์ํํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
"""
# ๋ฌธ์ฅ ์ ์ฒ๋ฆฌ
processed_review = preprocess_text(review_text, word_index)
# ์์ธก ์ํ
prediction = model.predict(processed_review, verbose=0) # ์์ธก ์ ๋ก๊ทธ ์ถ๋ ฅ์ ๋
positive_probability = prediction[0][0] * 100
print(f"--- {model_name} ๋ชจ๋ธ ์์ธก ๊ฒฐ๊ณผ ---")
print(f"๋ฆฌ๋ทฐ: '{review_text}'")
print(f"๊ธ์ ํ๋ฅ : {positive_probability:.2f}%")
if positive_probability > 50:
print("๊ฒฐ๊ณผ: ๊ธ์ ์ ์ธ ๋ฆฌ๋ทฐ์
๋๋ค.")
else:
print("๊ฒฐ๊ณผ: ๋ถ์ ์ ์ธ ๋ฆฌ๋ทฐ์
๋๋ค.")
print("-" * 30)
# 5. ๊ฐ ๋ฆฌ๋ทฐ์ ๋ํด ๋ ๋ชจ๋ธ๋ก ์์ธก ์ํ
print("\n" + "="*40)
print("์ฒซ ๋ฒ์งธ ๋ฆฌ๋ทฐ ์์ธก ์์")
print("="*40)
predict_review(review1, lstm_model, "LSTM")
predict_review(review1, gru_model, "GRU")
print("\n" + "="*40)
print("๋ ๋ฒ์งธ ๋ฆฌ๋ทฐ ์์ธก ์์")
print("="*40)
predict_review(review2, lstm_model, "LSTM")
predict_review(review2, gru_model, "GRU") |