PrakashatShell commited on
Commit
1f4bf3a
·
verified ·
1 Parent(s): c3c73b4

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
+ def sentiment_analysis(text: str) -> str:
6
+ """
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
+ Dictionary containing:
15
+ - sentiment: Overall categorization (Positive, Negative, or Neutral)
16
+ - polarity_score: Numerical score from -1 (very negative) to 1 (very positive)
17
+ - text_analyzed: The original input text
18
+ """
19
+ blob = TextBlob(text)
20
+ sentiment = blob.sentiment
21
+
22
+ result = {
23
+ "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
24
+ "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
25
+ "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
26
+ }
27
+
28
+ return json.dumps(result)
29
+
30
+ # Create the Gradio interface
31
+ demo = gr.Interface(
32
+ fn=sentiment_analysis,
33
+ inputs=gr.Textbox(placeholder="Enter text to analyze..."),
34
+ outputs=gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
35
+ title="Text Sentiment Analysis",
36
+ description="Analyze the sentiment of text using TextBlob"
37
+ )
38
+
39
+ # Launch the interface and MCP server
40
+ if __name__ == "__main__":
41
+ demo.launch(mcp_server=True)