Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines789101110111213141516151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768186970717220 unmodified lines939495459697982 unmodified lines1011021035455561041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341352 unmodified lines1381391406768697071727374757677787980818283848514114214336 unmodified lines1801811821286 unmodified linesimport osimport sys # ์ค๋ฅ ์ ์๋น์ค ์ข
๋ฃ๋ฅผ ์ํด sys ๋ชจ๋ ์ํฌํธ
# transformers์ AutoTokenizer๋ง ์ฌ์ฉํฉ๋๋ค.from transformers import AutoTokenizer # BertModel, BertForSequenceClassification ๋ฑ์ ์ด์ ์ง์ ํ์ ์์ต๋๋ค.# transformers์ AutoTokenizer ๋ฐ BertModel ์ํฌํธfrom transformers import AutoTokenizer, BertModel # BertModel ์ํฌํธ ์ถ๊ฐfrom torch.utils.data import Dataset, DataLoaderimport logging # ๋ก๊น
๋ชจ๋ ์ํฌํธ ์ ์งfrom huggingface_hub import hf_hub_download # hf_hub_download ์ํฌํธ ์ ์ง# collections ๋ชจ๋์ ๋ ์ด์ ํ์ ์์ ์ ์์ง๋ง, ํน์ ๋ชฐ๋ผ ์ ์งํฉ๋๋ค.import collections import collections # collections ๋ชจ๋ ์ํฌํธ ์ ์ง
# --- 1. BERTClassifier ๋ชจ๋ธ ํด๋์ค ์ ์ ---# ์ด ํด๋์ค๋ ๋ชจ๋ธ์ ์ํคํ
์ฒ๋ฅผ ์ ์ํฉ๋๋ค.class BERTClassifier(torch.nn.Module): def __init__(self, bert, hidden_size = 768, num_classes=5, # ๋ถ๋ฅํ ํด๋์ค ์ (category ๋์
๋๋ฆฌ ํฌ๊ธฐ์ ์ผ์น) dr_rate=None, params=None): super(BERTClassifier, self).__init__() self.bert = bert self.dr_rate = dr_rate
self.classifier = torch.nn.Linear(hidden_size , num_classes) if dr_rate: self.dropout = torch.nn.Dropout(p=dr_rate)
def gen_attention_mask(self, token_ids, valid_length): attention_mask = torch.zeros_like(token_ids) for i, v in enumerate(valid_length): attention_mask[i][:v] = 1 return attention_mask.float()
def forward(self, token_ids, valid_length, segment_ids): attention_mask = self.gen_attention_mask(token_ids, valid_length)
_, pooler = self.bert(input_ids=token_ids, token_type_ids=segment_ids.long(), attention_mask=attention_mask.float().to(token_ids.device), return_dict=False)
if self.dr_rate: out = self.dropout(pooler) else: out = pooler return self.classifier(out)
# --- 2. BERTDataset ํด๋์ค ์ ์ ---# ์ด ํด๋์ค๋ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ธ ์
๋ ฅ ํ์์ผ๋ก ๋ณํํฉ๋๋ค.class BERTDataset(Dataset): def __init__(self, dataset, sent_idx, label_idx, bert_tokenizer, vocab, max_len, pad, pair): # nlp.data.BERTSentenceTransform์ ํ ํฌ๋์ด์ ํจ์๋ฅผ ๋ฐ์ต๋๋ค. # AutoTokenizer์ tokenize ๋ฉ์๋๋ฅผ ์ง์ ์ ๋ฌํฉ๋๋ค. transform = nlp.data.BERTSentenceTransform( bert_tokenizer, max_seq_length=max_len, vocab=vocab, pad=pad, pair=pair ) self.sentences = [transform([i[sent_idx]]) for i in dataset] self.labels = [np.int32(i[label_idx]) for i in dataset]
def __getitem__(self, i): return (self.sentences[i] + (self.labels[i],))
def __len__(self): return len(self.labels)
# --- 1. FastAPI ์ฑ ๋ฐ ์ ์ญ ๋ณ์ ์ค์ ---# --- 3. FastAPI ์ฑ ๋ฐ ์ ์ญ ๋ณ์ ์ค์ ---app = FastAPI()device = torch.device("cpu") # Hugging Face Spaces์ ๋ฌด๋ฃ ํฐ์ด๋ ์ฃผ๋ก CPU๋ฅผ ์ฌ์ฉํฉ๋๋ค.
20 unmodified linesprint("ํ ํฌ๋์ด์ ๋ก๋ ์ฑ๊ณต.")
# โ
๋ชจ๋ธ ๋ก๋ (Hugging Face Hub์์ ๋ค์ด๋ก๋)# textClassifierModel.pt ํ์ผ์ ์ด๋ฏธ ๊ฒฝ๋ํ๋ '์์ ํ ๋ชจ๋ธ ๊ฐ์ฒด'๋ผ๊ณ ๊ฐ์ ํ๊ณ ์ง์ ๋ก๋ํฉ๋๋ค.try: HF_MODEL_REPO_ID = "hiddenFront/TextClassifier" # ์ฌ์ฉ์๋์ ์ค์ Hugging Face ์ ์ฅ์ ID HF_MODEL_FILENAME = "textClassifierModel.pt" # Hugging Face Hub์ ์
๋ก๋ํ ํ์ผ ์ด๋ฆ๊ณผ ์ผ์นํด์ผ ํฉ๋๋ค.2 unmodified lines print(f"๋ชจ๋ธ ํ์ผ์ด '{model_path}'์ ์ฑ๊ณต์ ์ผ๋ก ๋ค์ด๋ก๋๋์์ต๋๋ค.")
# --- ์์ ๋ ํต์ฌ ๋ถ๋ถ --- # ๊ฒฝ๋ํ๋ ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์ง์ ๋ก๋ํฉ๋๋ค. # ์ด ํ์ผ์ ์ด๋ฏธ PyTorch ๋ชจ๋ธ ๊ฐ์ฒด(์์ํ๋ ๋ชจ๋ธ ํฌํจ)์ด๋ฏ๋ก ๋ฐ๋ก ๋ก๋ํ์ฌ ์ฌ์ฉํฉ๋๋ค. model = torch.load(model_path, map_location=device) # 1. BertModel.from_pretrained๋ฅผ ์ฌ์ฉํ์ฌ ๊ธฐ๋ณธ BERT ๋ชจ๋ธ์ ๋ก๋ํฉ๋๋ค. # ์ด๋ ๊ฒ ํ๋ฉด ๋ชจ๋ธ์ ์ํคํ
์ฒ์ ์ฌ์ ํ์ต๋ ๊ฐ์ค์น๊ฐ ๋ก๋๋ฉ๋๋ค. bert_base_model = BertModel.from_pretrained('skt/kobert-base-v1')
# 2. BERTClassifier ์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค. # ์ฌ๊ธฐ์ num_classes๋ category ๋์
๋๋ฆฌ์ ํฌ๊ธฐ์ ์ผ์นํด์ผ ํฉ๋๋ค. model = BERTClassifier( bert_base_model, dr_rate=0.5, # ํ์ต ์ ์ฌ์ฉ๋ dr_rate ๊ฐ์ผ๋ก ๋ณ๊ฒฝํ์ธ์. num_classes=len(category) )
# 3. ๋ค์ด๋ก๋๋ ํ์ผ์์ state_dict๋ฅผ ๋ก๋ํฉ๋๋ค. # ์ด ํ์ผ์ ์ฌ์ฉ์๋์ ๊ฒฝ๋ํ๋ ๋ชจ๋ธ์ ๊ฐ์ค์น๋ง ํฌํจํ๊ณ ์์ต๋๋ค. loaded_state_dict = torch.load(model_path, map_location=device) # 4. ๋ก๋๋ state_dict์ ํค๋ฅผ ์กฐ์ ํ๊ณ ๋ชจ๋ธ์ ์ ์ฉํฉ๋๋ค. # 'module.' ์ ๋์ฌ๊ฐ ๏ฟฝ๏ฟฝ๏ฟฝ์ด์๋ ๊ฒฝ์ฐ ์ ๊ฑฐํ๋ ๋ก์ง์ ํฌํจํฉ๋๋ค. new_state_dict = collections.OrderedDict() for k, v in loaded_state_dict.items(): name = k if name.startswith('module.'): name = name[7:] new_state_dict[name] = v model.load_state_dict(new_state_dict) # --- ์์ ๋ ํต์ฌ ๋ถ๋ถ ๋ ---
model.to(device) # ๋ชจ๋ธ์ ๋๋ฐ์ด์ค๋ก ์ด๋ model.eval() # ์ถ๋ก ๋ชจ๋๋ก ์ค์ print("๋ชจ๋ธ ๋ก๋ ์ฑ๊ณต.")
2 unmodified lines sys.exit(1) # ๋ชจ๋ธ ๋ก๋ ์คํจ ์ ์๋น์ค ์์ํ์ง ์์
# --- 2. BERTDataset ํด๋์ค ์ ์ (dataset.py์์ ์ฎ๊ฒจ์ด) ---# ์ด ํด๋์ค๋ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ธ ์
๋ ฅ ํ์์ผ๋ก ๋ณํํฉ๋๋ค.class BERTDataset(Dataset): def __init__(self, dataset, sent_idx, label_idx, bert_tokenizer, vocab, max_len, pad, pair): # nlp.data.BERTSentenceTransform์ ํ ํฌ๋์ด์ ํจ์๋ฅผ ๋ฐ์ต๋๋ค. # AutoTokenizer์ tokenize ๋ฉ์๋๋ฅผ ์ง์ ์ ๋ฌํฉ๋๋ค. transform = nlp.data.BERTSentenceTransform( bert_tokenizer, max_seq_length=max_len, vocab=vocab, pad=pad, pair=pair ) self.sentences = [transform([i[sent_idx]]) for i in dataset] self.labels = [np.int32(i[label_idx]) for i in dataset]
def __getitem__(self, i): return (self.sentences[i] + (self.labels[i],))
def __len__(self): return len(self.labels)
# โ
๋ฐ์ดํฐ์
์์ฑ์ ํ์ํ ํ๋ผ๋ฏธํฐmax_len = 64batch_size = 3236 unmodified linesasync def predict_route(item: InputText): result = predict(item.text) return {"text": item.text, "classification": result}
app.py+83-29