File size: 1,337 Bytes
c2f577f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3875537
 
 
c2f577f
 
 
 
 
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
import requests

class SentimentAnalysisTool:
    name = "sentiment_analysis"
    description = "This tool analyses the sentiment of a given text input."

    inputs = ["text"]  # Adding an empty list for inputs

    outputs = ["json"]
    
    model_id_1 = "nlptown/bert-base-multilingual-uncased-sentiment"
    model_id_2 = "microsoft/deberta-xlarge-mnli"
    model_id_3 = "distilbert-base-uncased-finetuned-sst-2-english"
    model_id_4 = "lordtt13/emo-mobilebert"
    model_id_5 = "juliensimon/reviews-sentiment-analysis"
    model_id_6 = "sbcBI/sentiment_analysis_model"
    model_id_7 = "models/oliverguhr/german-sentiment-bert"

    def parse_output(output_json):  
        list_pred=[]
        for i in range(len(output_json[0])):
            label = output_json[0][i]['label']
            score = output_json[0][i]['score']
            list_pred.append((label, score))
        return list_pred
    
    def get_prediction(model_id):
        classifier = pipeline("text-classification", model=model_id, return_all_scores=True)
                 
    def predict(review):
            classifier = get_prediction(model_id_7)
            prediction = classifier(review)
            print(prediction)
            return parse_output(prediction)
    
    def __call__(self, inputs: str):
        return predict(str)