ecyht2 commited on
Commit
f719e02
·
verified ·
1 Parent(s): 6ff2711

Upload app.py

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