File size: 1,439 Bytes
c2f577f
1c02c6e
 
e7e4d1a
c2f577f
e7e4d1a
c2f577f
 
 
 
 
 
 
60da487
e7e4d1a
1c02c6e
cc1edab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7e4d1a
cc1edab
 
e7e4d1a
1c02c6e
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
44
45
46
47
import requests
import gradio as gr
from transformers import pipeline
from transformers import Tool

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

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

    outputs = ["json"]
    
    def __call__(self, inputs: str): 
        return predicto(str)
        
    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 predicto(review):
            classifier = get_prediction(model_id_3)
            prediction = classifier(review)
            print(prediction)
            return parse_output(prediction)