Instructions to use OneclickAI/LSTM_GUE_test_Model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use OneclickAI/LSTM_GUE_test_Model with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://OneclickAI/LSTM_GUE_test_Model") - Notebooks
- Google Colab
- Kaggle
Download test.py from OneclickAI/LSTM_GUE_test_Model: direct link, hf CLI and curl.
- Browser
- Download file 3.77 kB
-
https://huggingface.co/OneclickAI/LSTM_GUE_test_Model/resolve/main/test.py
- Command line
-
hf download hf://OneclickAI/LSTM_GUE_test_Model/test.py
-
curl -L -o test.py https://huggingface.co/OneclickAI/LSTM_GUE_test_Model/resolve/main/test.py
3.77 kB
| # 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") |