Spaces:
Sleeping
Sleeping
Download src/predict.py from LeeChang1/ESTR_Review_TT: direct link, hf CLI and curl.
- Browser
- Download file 4.76 kB
-
https://huggingface.co/spaces/LeeChang1/ESTR_Review_TT/resolve/main/src/predict.py
- Command line
-
hf download hf://spaces/LeeChang1/ESTR_Review_TT/src/predict.py
-
curl -L -o predict.py https://huggingface.co/spaces/LeeChang1/ESTR_Review_TT/resolve/main/src/predict.py
4.76 kB
| # -*- coding: utf-8 -*- | |
| """ | |
| [์ต์ข ํด๊ฒฐ์ฑ ] Local GPU Direct Prediction Module | |
| - HF Inference API (์ธ๋ถ ํต์ )๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๋ค. | |
| - Space์ ํ ๋น๋ GPU๋ฅผ ์ง์ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ๋ก๋ํ๊ณ ์ถ๋ก ํฉ๋๋ค. | |
| - ์ฅ์ : 404 ์ค๋ฅ ์์, ์ถ๊ฐ ๋น์ฉ ์์, ๊ฐ์ฅ ๋น ๋ฅธ ์๋. | |
| """ | |
| import torch | |
| import numpy as np | |
| import os | |
| from transformers import AutoTokenizer, AutoConfig | |
| # [์ค์] ์ฐ๋ฆฌ๊ฐ ๋ง๋ ์ปค์คํ ๋ชจ๋ธ ํด๋์ค๋ฅผ ์ํฌํธํด์ผ ํฉ๋๋ค. | |
| from src.mtl_modeling import BertForESTR_MTL | |
| from src.labelspace import id2label, coords_to_label | |
| # ๋ชจ๋ธ ID (Hub์์ ๋ค์ด๋ก๋ํ ๊ฒฝ๋ก) | |
| HUB_MODEL_ID = "LeeChang1/estr_mtl" | |
| def softmax(x): | |
| e = np.exp(x - np.max(x, axis=-1, keepdims=True)) | |
| return e / e.sum(axis=-1, keepdims=True) | |
| class ESTRPredictor: | |
| def __init__(self, hf_token=None): | |
| """ | |
| ๋ชจ๋ธ์ ๋ก์ปฌ ๋ฉ๋ชจ๋ฆฌ(GPU)๋ก ์ง์ ๋ก๋ํฉ๋๋ค. | |
| """ | |
| # 1. GPU ์ฌ์ฉ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ (Space๊ฐ T4๋ผ๋ฉด cuda๊ฐ ์กํ) | |
| self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print(f"โ ESTRPredictor ์ด๊ธฐํ (Device: {self.device})") | |
| print(f"โฌ๏ธ ๋ชจ๋ธ ๋ค์ด๋ก๋ ๋ฐ ๋ก๋ ์ค: {HUB_MODEL_ID}...") | |
| try: | |
| # 2. ํ ํฌ๋์ด์ ๋ฐ ์ค์ ๋ก๋ (ํ ํฐ์ ๋ฃ์ด์ฃผ์ด์ผ Private ๋ชจ๋ธ๋ ๋ค์ด ๊ฐ๋ฅ) | |
| self.tokenizer = AutoTokenizer.from_pretrained(HUB_MODEL_ID, token=hf_token) | |
| self.config = AutoConfig.from_pretrained(HUB_MODEL_ID, token=hf_token) | |
| # 3. [ํต์ฌ] ์ปค์คํ ๋ชจ๋ธ(BertForESTR_MTL) ๋ก๋ | |
| # AutoModelForSequenceClassification์ ์ฐ๋ฉด REG ํค๋๊ฐ ๋ ์๊ฐ๋๋ค. | |
| self.model = BertForESTR_MTL.from_pretrained( | |
| HUB_MODEL_ID, | |
| config=self.config, | |
| token=hf_token | |
| ) | |
| # 4. ๋ชจ๋ธ์ GPU๋ก ์ด๋ (์๋ ํฅ์) | |
| self.model.to(self.device) | |
| self.model.eval() # ํ๊ฐ ๋ชจ๋๋ก ์ ํ (ํ์ต ์๋) | |
| print("โ ๋ชจ๋ธ ๋ก๋ ์๋ฃ! (Ready for Inference)") | |
| except Exception as e: | |
| print(f"โ ๋ชจ๋ธ ๋ก๋ ์คํจ: {e}") | |
| raise RuntimeError(f"๋ชจ๋ธ์ ๋ก๋ํ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.\n{e}") | |
| def predict(self, texts): | |
| """ | |
| ์ ๋ ฅ ํ ์คํธ๋ฅผ ๋ฐ์ ๋ก์ปฌ GPU๋ก ๊ณ์ฐ ํ ๊ฒฐ๊ณผ ๋ฐํ | |
| """ | |
| if isinstance(texts, str): texts = [texts] | |
| print(f"๐ Model (Local): {len(texts)}๊ฐ ๋ฌธ์ฅ ๋ถ์ ์์...") | |
| try: | |
| # 1. ํ ํฌ๋์ด์ง (GPU๋ก ์ ์ก) | |
| inputs = self.tokenizer( | |
| texts, | |
| return_tensors="pt", | |
| padding=True, | |
| truncation=True, | |
| max_length=128 | |
| ).to(self.device) | |
| # 2. ์ถ๋ก (Gradient ๊ณ์ฐ ๋ -> ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ ๋ฐ ์๋ ํฅ์) | |
| with torch.no_grad(): | |
| # ๋ชจ๋ธ ์คํ -> (loss, logits_cls, ...), logits_reg | |
| outputs = self.model(**inputs) | |
| # ํํ์ ์ฒซ ๋ฒ์งธ ์์์์ logits_cls ์ถ์ถ | |
| logits_cls = outputs[0].logits | |
| # ํํ์ ๋ ๋ฒ์งธ ์์๊ฐ logits_reg | |
| logits_reg = outputs[1] | |
| # ๊ฒฐ๊ณผ๋ฅผ CPU๋ก ๊ฐ์ ธ์์ Numpy๋ก ๋ณํ | |
| logits_cls = logits_cls.cpu().numpy() | |
| logits_reg = logits_reg.cpu().numpy() | |
| # 3. ๊ฒฐ๊ณผ ํ์ฒ๋ฆฌ (Post-processing) | |
| probs_cls = softmax(logits_cls) | |
| # ํ๊ท ๊ฐ ๋ฒ์ ์ ํ (-1 ~ 1) | |
| coords_reg = np.clip(logits_reg, -1.0, 1.0) | |
| results = [] | |
| for i, text in enumerate(texts): | |
| # CLS ๊ฒฐ๊ณผ (๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๋ผ๋ฒจ) | |
| cls_idx = np.argmax(probs_cls[i]) | |
| estr_label = id2label[cls_idx] | |
| # ์ ์ฒด ํ๋ฅ ๋งต | |
| estr_probs = {id2label[idx]: float(probs_cls[i, idx]) for idx in range(4)} | |
| # REG ๊ฒฐ๊ณผ (์ขํ) | |
| v, a = float(coords_reg[i, 0]), float(coords_reg[i, 1]) | |
| estr_from_coords = coords_to_label(v, a) | |
| results.append({ | |
| "text": text, | |
| "estr_label": estr_label, | |
| "estr_probs": estr_probs, | |
| "valence": v, | |
| "arousal": a, | |
| "estr_from_coords": estr_from_coords | |
| }) | |
| print(f"โ ๋ถ์ ์๋ฃ ({len(results)}๊ฑด)") | |
| return results | |
| except Exception as e: | |
| print(f"โ ์ถ๋ก ์ค ์ค๋ฅ ๋ฐ์: {e}") | |
| raise e |