Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app (8).py +4 -0
- requirements (4).txt +5 -0
- sentiment_analysis.py +46 -0
- tool_config.json +5 -0
app (8).py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers.tools.base import launch_gradio_demo
|
| 2 |
+
from sentiment_analysis import SentimentAnalysisTool
|
| 3 |
+
|
| 4 |
+
launch_gradio_demo(SentimentAnalysisTool)
|
requirements (4).txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.29.0
|
| 2 |
+
gradio
|
| 3 |
+
#diffusers
|
| 4 |
+
#accelerate
|
| 5 |
+
torch
|
sentiment_analysis.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from transformers import Tool
|
| 5 |
+
|
| 6 |
+
class SentimentAnalysisTool(Tool):
|
| 7 |
+
name = "sentiment_analysis"
|
| 8 |
+
description = "This tool analyses the sentiment of a given text input."
|
| 9 |
+
|
| 10 |
+
inputs = ["text"] # Adding an empty list for inputs
|
| 11 |
+
outputs = ["json"]
|
| 12 |
+
|
| 13 |
+
model_id_1 = "nlptown/bert-base-multilingual-uncased-sentiment"
|
| 14 |
+
model_id_2 = "microsoft/deberta-xlarge-mnli"
|
| 15 |
+
model_id_3 = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 16 |
+
model_id_4 = "lordtt13/emo-mobilebert"
|
| 17 |
+
model_id_5 = "juliensimon/reviews-sentiment-analysis"
|
| 18 |
+
model_id_6 = "sbcBI/sentiment_analysis_model"
|
| 19 |
+
model_id_7 = "models/oliverguhr/german-sentiment-bert"
|
| 20 |
+
|
| 21 |
+
def __call__(self, inputs: str):
|
| 22 |
+
return self.predicto(inputs)
|
| 23 |
+
|
| 24 |
+
def parse_output(self, output_json):
|
| 25 |
+
list_pred = []
|
| 26 |
+
for i in range(len(output_json[0])):
|
| 27 |
+
label = output_json[0][i]['label']
|
| 28 |
+
score = output_json[0][i]['score']
|
| 29 |
+
list_pred.append((label, score))
|
| 30 |
+
return list_pred
|
| 31 |
+
|
| 32 |
+
def get_prediction(self, model_id):
|
| 33 |
+
classifier = pipeline("text-classification", model=model_id, return_all_scores=True)
|
| 34 |
+
return classifier
|
| 35 |
+
|
| 36 |
+
def predicto(self, review):
|
| 37 |
+
classifier = self.get_prediction(self.model_id_3)
|
| 38 |
+
prediction = classifier(review)
|
| 39 |
+
print(prediction)
|
| 40 |
+
return self.parse_output(prediction)
|
| 41 |
+
|
| 42 |
+
# Create an instance of the SentimentAnalysisTool class
|
| 43 |
+
sentiment_analysis_tool = SentimentAnalysisTool()
|
| 44 |
+
|
| 45 |
+
# Create the Gradio interface
|
| 46 |
+
gr.Interface(fn=sentiment_analysis_tool, inputs=sentiment_analysis_tool.inputs, outputs=sentiment_analysis_tool.outputs).launch()
|
tool_config.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"description": "Based on a text input this tool is able to analyse the sentiment of the given text. It returns a json with the sentiment.",
|
| 3 |
+
"name": "sentiment_analysis",
|
| 4 |
+
"tool_class": "sentiment_analysis.SentimentAnalysisTool"
|
| 5 |
+
}
|