File size: 1,727 Bytes
c2f577f
1c02c6e
 
e7e4d1a
c2f577f
e7e4d1a
c2f577f
5cb8034
c2f577f
 
 
 
cc1edab
 
 
 
 
 
 
 
5cb8034
 
17b66cc
 
 
cc1edab
 
 
 
 
 
17b66cc
cc1edab
17b66cc
 
 
 
 
 
 
c2f577f
17b66cc
 
c2f577f
17b66cc
ab6a84b
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."

    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 __call__(self, text: str): 
        return self.predicto(text)
        
    def parse_output(self, 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(self, model_id):
        classifier = pipeline("text-classification", model=model_id, return_all_scores=True)
        return classifier
    
    def predicto(self, review):
        classifier = self.get_prediction(self.model_id_3)
        prediction = classifier(review)
        print(prediction)
        return self.parse_output(prediction)

# Create an instance of the SentimentAnalysisTool class
sentiment_analysis_tool = SentimentAnalysisTool()

# Create the Gradio interface
#gr.Interface(fn=sentiment_analysis_tool, inputs=sentiment_analysis_tool.inputs, outputs=sentiment_analysis_tool.outputs).launch(share=True)