Commit
·
969cd78
1
Parent(s):
24d813e
Upload 6 files
Browse files- sentiment_analyzer/__pycache__/api.cpython-39.pyc +0 -0
- sentiment_analyzer/api.py +30 -0
- sentiment_analyzer/classifier/__pycache__/model.cpython-39.pyc +0 -0
- sentiment_analyzer/classifier/__pycache__/sentiment_classifier.cpython-39.pyc +0 -0
- sentiment_analyzer/classifier/model.py +56 -0
- sentiment_analyzer/classifier/sentiment_classifier.py +20 -0
sentiment_analyzer/__pycache__/api.cpython-39.pyc
ADDED
|
Binary file (1.27 kB). View file
|
|
|
sentiment_analyzer/api.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict
|
| 2 |
+
|
| 3 |
+
from fastapi import Depends, FastAPI
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
|
| 6 |
+
from .classifier.model import Model, get_model
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class SentimentRequest(BaseModel):
|
| 12 |
+
text: str
|
| 13 |
+
|
| 14 |
+
class SentimentResponse(BaseModel):
|
| 15 |
+
probabilities: Dict[str, float]
|
| 16 |
+
sentiment: str
|
| 17 |
+
confidence: float
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@app.post("/predict", response_model=SentimentResponse)
|
| 21 |
+
def predict(request: SentimentRequest, model: Model = Depends(get_model)):
|
| 22 |
+
sentiment, confidence, probabilities = model.predict(request.text)
|
| 23 |
+
return SentimentResponse(
|
| 24 |
+
sentiment=sentiment, confidence=confidence, probabilities=probabilities
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@app.get("/")
|
| 29 |
+
def read_root():
|
| 30 |
+
return {"TrueFoundry": "Project"}
|
sentiment_analyzer/classifier/__pycache__/model.cpython-39.pyc
ADDED
|
Binary file (1.95 kB). View file
|
|
|
sentiment_analyzer/classifier/__pycache__/sentiment_classifier.cpython-39.pyc
ADDED
|
Binary file (1.2 kB). View file
|
|
|
sentiment_analyzer/classifier/model.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
from transformers import BertTokenizer
|
| 6 |
+
|
| 7 |
+
from .sentiment_classifier import SentimentClassifier
|
| 8 |
+
|
| 9 |
+
with open("config.json") as json_file:
|
| 10 |
+
config = json.load(json_file)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class Model:
|
| 14 |
+
def __init__(self):
|
| 15 |
+
|
| 16 |
+
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 17 |
+
|
| 18 |
+
self.tokenizer = BertTokenizer.from_pretrained(config["BERT_MODEL"])
|
| 19 |
+
|
| 20 |
+
classifier = SentimentClassifier(len(config["CLASS_NAMES"]))
|
| 21 |
+
classifier.load_state_dict(
|
| 22 |
+
torch.load(config["PRE_TRAINED_MODEL"], map_location=self.device)
|
| 23 |
+
)
|
| 24 |
+
classifier = classifier.eval()
|
| 25 |
+
self.classifier = classifier.to(self.device)
|
| 26 |
+
|
| 27 |
+
def predict(self, text):
|
| 28 |
+
encoded_text = self.tokenizer.encode_plus(
|
| 29 |
+
text,
|
| 30 |
+
max_length=config["MAX_SEQUENCE_LEN"],
|
| 31 |
+
add_special_tokens=True,
|
| 32 |
+
return_token_type_ids=False,
|
| 33 |
+
pad_to_max_length=True,
|
| 34 |
+
return_attention_mask=True,
|
| 35 |
+
return_tensors="pt",
|
| 36 |
+
)
|
| 37 |
+
input_ids = encoded_text["input_ids"].to(self.device)
|
| 38 |
+
attention_mask = encoded_text["attention_mask"].to(self.device)
|
| 39 |
+
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
probabilities = F.softmax(self.classifier(input_ids, attention_mask), dim=1)
|
| 42 |
+
confidence, predicted_class = torch.max(probabilities, dim=1)
|
| 43 |
+
predicted_class = predicted_class.cpu().item()
|
| 44 |
+
probabilities = probabilities.flatten().cpu().numpy().tolist()
|
| 45 |
+
return (
|
| 46 |
+
config["CLASS_NAMES"][predicted_class],
|
| 47 |
+
confidence,
|
| 48 |
+
dict(zip(config["CLASS_NAMES"], probabilities)),
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
model = Model()
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def get_model():
|
| 56 |
+
return model
|
sentiment_analyzer/classifier/sentiment_classifier.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
from torch import nn
|
| 4 |
+
from transformers import BertModel
|
| 5 |
+
|
| 6 |
+
with open("config.json") as json_file:
|
| 7 |
+
config = json.load(json_file)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class SentimentClassifier(nn.Module):
|
| 11 |
+
def __init__(self, n_classes):
|
| 12 |
+
super(SentimentClassifier, self).__init__()
|
| 13 |
+
self.bert = BertModel.from_pretrained(config["BERT_MODEL"])
|
| 14 |
+
self.drop = nn.Dropout(p=0.3)
|
| 15 |
+
self.out = nn.Linear(self.bert.config.hidden_size, n_classes)
|
| 16 |
+
|
| 17 |
+
def forward(self, input_ids, attention_mask):
|
| 18 |
+
_, pooled_output = self.bert(input_ids=input_ids, attention_mask=attention_mask, return_dict=False)
|
| 19 |
+
output = self.drop(pooled_output)
|
| 20 |
+
return self.out(output)
|