atharva98 commited on
Commit
a36b09d
·
verified ·
1 Parent(s): 7f594f2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import gradio as gr
3
+ from textblob import TextBlob
4
+
5
+
6
+ def sentiment_analysis(text: str) -> str:
7
+ """Analyze the sentiment of the given text
8
+
9
+ Args:
10
+ text (str): The text to analyze
11
+
12
+ Returns:
13
+ str: A JSON string containing polarity, subjectivity and assessment
14
+ """
15
+ blob = TextBlob(text)
16
+ sentiment = blob.sentiment
17
+
18
+ result = {
19
+ # -1 (negative) to 1(positive)
20
+ "polarity": round(sentiment.polarity, 2),
21
+ "subjectivity": round(sentiment.subjectivity, 2),
22
+ "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
23
+ }
24
+ return json.dumps(result)
25
+ # return f"Sentiment: {result['assessment']}"
26
+
27
+
28
+ # Create a Gradio instance
29
+
30
+ demo = gr.Interface(
31
+ fn=sentiment_analysis,
32
+ inputs=gr.Textbox(placeholder="Enter the text to analyze..."),
33
+ outputs=gr.Textbox(),
34
+ title="Text Sentiment Analysis",
35
+ description="Analyse the sentiment of the text using TextBlob",
36
+ )
37
+
38
+
39
+ # Launch the gradio instance and MCP server
40
+ if __name__ == "__main__":
41
+ demo.launch(mcp_server=True)