Commit ·
868047f
1
Parent(s): 934e563
Upload 2 files
Browse files- dtos.py +10 -0
- handler.py +8 -12
dtos.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class SentimentAnalysisRequestDto(BaseModel):
|
| 6 |
+
reviews: List[str]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class SentimentAnalysisResponseDto(BaseModel):
|
| 10 |
+
scores: List[int]
|
handler.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from typing import Dict, List, Any
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class EndpointHandler():
|
|
@@ -11,20 +12,15 @@ class EndpointHandler():
|
|
| 11 |
self.sentiment_model = AutoModelForSequenceClassification.from_pretrained("Christian2903/amazon-review-sentiment-analysis").to(self.device)
|
| 12 |
|
| 13 |
|
| 14 |
-
def __call__(self, reviews:
|
| 15 |
-
|
| 16 |
-
Args:
|
| 17 |
-
data (:obj:):
|
| 18 |
-
includes the input data and the parameters for the inference.
|
| 19 |
-
Return:
|
| 20 |
-
A :obj:`list`:. The object returned should be a list of one list like [[{"label": 0.9939950108528137}]] containing :
|
| 21 |
-
- "label": A string representing what the label/class is. There can be multiple labels.
|
| 22 |
-
- "score": A score between 0 and 1 describing how confident the model is for this label/class.
|
| 23 |
-
"""
|
| 24 |
|
| 25 |
inputs = self.tokenizer(reviews, return_tensors="pt", truncation=True, padding="max_length", max_length=256).to(self.device)
|
| 26 |
outputs = self.sentiment_model(**inputs)
|
| 27 |
logits = outputs.logits.detach()
|
| 28 |
predicted_scores = [max(min(int(score + 0.5),5),1) for score in logits]
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import Dict, List, Any
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
import torch
|
| 4 |
+
from dtos import SentimentAnalysisRequestDto, SentimentAnalysisResponseDto
|
| 5 |
|
| 6 |
|
| 7 |
class EndpointHandler():
|
|
|
|
| 12 |
self.sentiment_model = AutoModelForSequenceClassification.from_pretrained("Christian2903/amazon-review-sentiment-analysis").to(self.device)
|
| 13 |
|
| 14 |
|
| 15 |
+
def __call__(self, reviews: SentimentAnalysisRequestDto) -> SentimentAnalysisResponseDto:
|
| 16 |
+
reviews = [review for review in reviews.reviews]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
inputs = self.tokenizer(reviews, return_tensors="pt", truncation=True, padding="max_length", max_length=256).to(self.device)
|
| 19 |
outputs = self.sentiment_model(**inputs)
|
| 20 |
logits = outputs.logits.detach()
|
| 21 |
predicted_scores = [max(min(int(score + 0.5),5),1) for score in logits]
|
| 22 |
+
|
| 23 |
+
response = SentimentAnalysisResponseDto(
|
| 24 |
+
scores=predicted_scores
|
| 25 |
+
)
|
| 26 |
+
return response
|