Spaces:
Sleeping
Sleeping
# 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 | |
def read_root(): | |
"Home endpoint" | |
return {"greeting": "Hello World..!", | |
"cohort": "2", | |
} | |
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 | |
} |