import requests import gradio as gr from transformers import pipeline 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"] def __call__(self, inputs: str): return SentimentAnalysisTool.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 = SentimentAnalysisTool.get_prediction(SentimentAnalysisTool.model_id_3) prediction = classifier(review) print(prediction) return SentimentAnalysisTool.parse_output(prediction)