File size: 1,576 Bytes
081c5aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Imports
import sys
# sys.path.insert(0, '../src/')
# sys.path.insert(0, '../src')
# sys.path.insert(0, 'src/')
# sys.path.insert(0, 'src')

from typing import Union
from src.utils import preprocess
from fastapi import FastAPI
from transformers import AutoModelForSequenceClassification,AutoTokenizer, AutoConfig
import numpy as np
#convert logits to probabilities
from scipy.special import softmax

# Config

app = FastAPI()
#/docs, page to see auto-generated API documentation

#loading ML/DL components
tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
model_path = f"Junr-syl/tweet_sentiments_analysis"
config = AutoConfig.from_pretrained(model_path)
config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
model = AutoModelForSequenceClassification.from_pretrained(model_path)

# Endpoints
@app.get("/")
def read_root():
    "Home endpoint"
    return {"greeting": "Hello World..!", 
            "cohort": "2",
            }

@app.post("/predict")
def predict(text:str):
    "prediction endpoint, classifying tweets"
    
    text = preprocess(text)

    # PyTorch-based models
    encoded_input = tokenizer(text, return_tensors='pt')
    output = model(**encoded_input)
    scores = output[0][0].detach().numpy()
    scores = softmax(scores)

    #Process scores
    ranking = np.argsort(scores)
    ranking = ranking[::-1]  
    predicted_label = config.id2label[ranking[0]]
    predicted_score = scores[ranking[0]]


    return {"text":text,
            "predicted_label":predicted_label,
            "confidence_score":predicted_score
            }