File size: 987 Bytes
d427365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import gradio as gr
from textblob import TextBlob


def sentiment_analysis(text: str) -> str:
    """
    Analysis of the sentiment behing a text
    Args:
        text(str): it's the text you want to make a sentiment analysis on
    Return a Json String, containing
        - polarity
        - subjectivity
        - assessment
    """
    blob = TextBlob(text)
    data = {
        "polarity": round(blob.sentiment.polarity, 2),
        "subjectivity" : round(blob.sentiment.subjectivity, 2),
         "assessment": "positive" if blob.sentiment.polarity > 0 else "negative" if blob.sentiment.polarity < 0 else "neutral"
    }
    return json.dumps(data)



demo = gr.Interface(fn = sentiment_analysis,
                    inputs= gr.Textbox(placeholder= "Enter your text, and our system will give you the sentiment"),
                    outputs= gr.Textbox(),
                    title= "Sentiment Analysis")



if __name__ == "__main__":
    demo.launch(mcp_server=True)