CoolFace
Apppublic

Ashwin39/Diarify

sourceHugging Facelgpl-3.0updated 4y agoView on Hugging Face
0likes
api.py85 linesDownload Raw Back to root
1from flask import Flask2from flask_restful import Api,Resource,reqparse3from transformers import pipeline4classifier = pipeline("text-classification",model='bhadresh-savani/distilbert-base-uncased-emotion', top_k=1)5 6"""7This is a sentiment analysis API that uses the DistilBERT uncased emotion model from HuggingFace.8 9The DistilBERT uncased emotion model is a pre-trained model that is able to identify six different emotions in text data: sadness, joy, love, anger, fear, and surprise. 10The model has been trained on a large dataset of text data that has been annotated with these six emotion categories, so it is able to recognize patterns in the text that correspond to each of these emotions.11 12To use this API, send a post request to the /sentiment endpoint with a string in the body of the request. The API will return the sentiment of the string and the probability score of the sentiment.13"""14 15def analysis(content=""):16    try:17        prediction = classifier(content)18        label=prediction[0][0]["label"]19        score=prediction[0][0]["score"]20 21        return label,score22    except:23        return "Error","Error"24 25 26app=Flask((__name__))27api=Api(app)28print("APP active")29sentiment_Arg=reqparse.RequestParser()30sentiment_Arg.add_argument("string",type=str,help="Send string to be classified")31 32 33# Example post request:34# {"string":"I am happy"}35 36class sentiment(Resource):37    """38    This class is used to classify the sentiment of a string39    40    Usage:41    1. Get request to check if the API is active42    2. Post request to classify the sentiment of a string43 44    """45 46    def get(self):47 48        ''''49        This function is used to check if the API is active50            Returns:51                message: API active52        '''53 54        return {"message":"API active"}55 56    def post(self):57 58        '''59        This function is used to classify the sentiment of a string60 61            Accepts:62                string: string to be classified63            64            Returns:65                sentiment: sentiment of the string66                score: probabilty score of the sentiment67        68        '''69 70 71        try:72            args=sentiment_Arg.parse_args()73            print(args)74            string=args["string"]75            pred,score=analysis(string)76            return {"sentiment":str(pred),"score":str(score)} 77 78        except:79            return {"message":"Error"}80 81 82api.add_resource(sentiment,"/sentiment")83 84if __name__=="__main__":85    app.run(debug=True)