Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,96 +1,72 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
-
from
|
| 3 |
-
from huggingface_hub import hf_hub_download
|
| 4 |
import torch
|
| 5 |
import pickle
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
-
import
|
| 8 |
-
import
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
device = torch.device("cpu")
|
| 12 |
|
| 13 |
-
# category
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
category = pickle.load(f)
|
| 17 |
-
print("β
category.pkl λ‘λ μ±κ³΅.")
|
| 18 |
-
except FileNotFoundError:
|
| 19 |
-
print("β Error: category.pkl νμΌμ μ°Ύμ μ μμ΅λλ€.")
|
| 20 |
-
sys.exit(1)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
super().__init__()
|
| 29 |
-
# μ μνλ ꡬ쑰 κ·Έλλ‘ λ³΅μν΄μΌ ν¨
|
| 30 |
-
self.bert = BertModel.from_pretrained("skt/kobert-base-v1")
|
| 31 |
-
self.classifier = torch.nn.Linear(768, len(category))
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# λͺ¨λΈ λ‘λ
|
| 49 |
-
try:
|
| 50 |
-
model_path = hf_hub_download(repo_id=HF_MODEL_REPO_ID, filename=HF_MODEL_FILENAME)
|
| 51 |
-
print(f"β
λͺ¨λΈ νμΌ λ€μ΄λ‘λ μ±κ³΅: {model_path}")
|
| 52 |
-
|
| 53 |
-
state_dict = torch.load(model_path, map_location=device)
|
| 54 |
-
model = BertForSequenceClassification.from_pretrained(
|
| 55 |
-
"skt/kobert-base-v1",
|
| 56 |
-
num_labels=len(category),
|
| 57 |
-
state_dict=state_dict,
|
| 58 |
-
)
|
| 59 |
-
model.to(device)
|
| 60 |
model.eval()
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
@app.get("/")
|
| 68 |
-
def root(
|
| 69 |
-
|
| 70 |
-
client_port = request.client.port
|
| 71 |
-
return {
|
| 72 |
-
"message": "Text Classification API is running!",
|
| 73 |
-
"client_ip": client_host,
|
| 74 |
-
"client_port": client_port
|
| 75 |
-
}
|
| 76 |
|
| 77 |
-
# μμΈ‘ API
|
| 78 |
@app.post("/predict")
|
| 79 |
-
async def
|
| 80 |
-
|
| 81 |
-
text
|
| 82 |
-
print("request date", data);
|
| 83 |
-
if not text:
|
| 84 |
-
return {"error": "No text provided", "classification": "null"}
|
| 85 |
-
|
| 86 |
-
encoded = tokenizer.encode_plus(
|
| 87 |
-
text, max_length=64, padding='max_length', truncation=True, return_tensors='pt'
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
with torch.no_grad():
|
| 91 |
-
outputs = model(**encoded)
|
| 92 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 93 |
-
predicted = torch.argmax(probs, dim=1).item()
|
| 94 |
-
|
| 95 |
-
label = list(category.keys())[predicted]
|
| 96 |
-
return {"text": text, "classification": label}
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
+
from pydantic import BaseModel
|
|
|
|
| 3 |
import torch
|
| 4 |
import pickle
|
| 5 |
+
import gluonnlp as nlp
|
| 6 |
+
import numpy as np
|
| 7 |
import os
|
| 8 |
+
from kobert_tokenizer import KoBERTTokenizer
|
| 9 |
+
from model import BERTClassifier
|
| 10 |
+
from dataset import BERTDataset
|
| 11 |
+
from transformers import BertModel
|
| 12 |
+
import logging
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
device = torch.device("cpu")
|
| 16 |
|
| 17 |
+
# β
category λ‘λ
|
| 18 |
+
with open("category.pkl", "rb") as f:
|
| 19 |
+
category = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# β
vocab λ‘λ
|
| 22 |
+
with open("vocab.pkl", "rb") as f:
|
| 23 |
+
vocab = pickle.load(f)
|
| 24 |
|
| 25 |
+
# β
ν ν¬λμ΄μ
|
| 26 |
+
tokenizer = KoBERTTokenizer.from_pretrained('skt/kobert-base-v1')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# β
λͺ¨λΈ λ‘λ
|
| 29 |
+
model = BERTClassifier(
|
| 30 |
+
BertModel.from_pretrained('skt/kobert-base-v1'),
|
| 31 |
+
dr_rate=0.5,
|
| 32 |
+
num_classes=len(category)
|
| 33 |
+
)
|
| 34 |
+
model.load_state_dict(torch.load("textClassifierModel.pt", map_location=device))
|
| 35 |
+
model.to(device)
|
| 36 |
+
model.eval()
|
| 37 |
|
| 38 |
+
# β
λ°μ΄ν°μ
μμ±μ νμν νλΌλ―Έν°
|
| 39 |
+
max_len = 64
|
| 40 |
+
batch_size = 32
|
| 41 |
|
| 42 |
+
# β
μμΈ‘ ν¨μ
|
| 43 |
+
def predict(predict_sentence):
|
| 44 |
+
data = [predict_sentence, '0']
|
| 45 |
+
dataset_another = [data]
|
| 46 |
+
another_test = BERTDataset(dataset_another, 0, 1, tokenizer, vocab, max_len, True, False)
|
| 47 |
+
test_dataLoader = torch.utils.data.DataLoader(another_test, batch_size=batch_size, num_workers=0)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
model.eval()
|
| 50 |
+
for batch_id, (token_ids, valid_length, segment_ids, label) in enumerate(test_dataLoader):
|
| 51 |
+
token_ids = token_ids.long().to(device)
|
| 52 |
+
segment_ids = segment_ids.long().to(device)
|
| 53 |
+
|
| 54 |
+
out = model(token_ids, valid_length, segment_ids)
|
| 55 |
+
test_eval = []
|
| 56 |
+
for i in out:
|
| 57 |
+
logits = i.detach().cpu().numpy()
|
| 58 |
+
test_eval.append(list(category.keys())[np.argmax(logits)])
|
| 59 |
+
return test_eval[0]
|
| 60 |
|
| 61 |
+
# β
μλν¬μΈνΈ μ μ
|
| 62 |
+
class InputText(BaseModel):
|
| 63 |
+
text: str
|
| 64 |
|
| 65 |
@app.get("/")
|
| 66 |
+
def root():
|
| 67 |
+
return {"message": "Text Classification API (KoBERT)"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
| 69 |
@app.post("/predict")
|
| 70 |
+
async def predict_route(item: InputText):
|
| 71 |
+
result = predict(item.text)
|
| 72 |
+
return {"text": item.text, "classification": result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|