|
|
|
|
|
import numpy as np
|
|
import tensorflow as tf
|
|
from tensorflow import keras
|
|
|
|
from huggingface_hub import hf_hub_download
|
|
|
|
print("TensorFlow ๋ฒ์ :", tf.__version__)
|
|
|
|
|
|
REPO_ID = "OneclickAI/LSTM_GUE_test_Model"
|
|
print(f"\n'{REPO_ID}' ์ ์ฅ์์์ ๋ชจ๋ธ ํ์ผ์ ์์น๋ฅผ ํ์ธํฉ๋๋ค...")
|
|
|
|
try:
|
|
|
|
|
|
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}")
|
|
|
|
|
|
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()
|
|
|
|
|
|
word_index = keras.datasets.imdb.get_word_index()
|
|
|
|
|
|
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."
|
|
|
|
|
|
def preprocess_text(text, word_index, maxlen=256):
|
|
"""
|
|
ํ
์คํธ๋ฅผ ๋ชจ๋ธ์ด ์ดํดํ ์ ์๋ ์ ์ ์ํ์ค๋ก ๋ณํํ๊ณ ํจ๋ฉํฉ๋๋ค.
|
|
"""
|
|
|
|
tokens = text.lower().split()
|
|
|
|
|
|
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
|
|
|
|
|
|
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)
|
|
|
|
|
|
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") |