jangmin commited on
Commit
d7957bc
·
verified ·
1 Parent(s): 57e23f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -7
app.py CHANGED
@@ -1,14 +1,15 @@
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
  """
@@ -20,18 +21,41 @@ def sentiment_analysis(text: str) -> str:
20
  "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
21
  "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
22
  }
23
-
24
- return json.dumps(result)
25
 
26
  # Create the Gradio interface
27
  demo = gr.Interface(
28
  fn=sentiment_analysis,
29
  inputs=gr.Textbox(placeholder="Enter text to analyze..."),
30
- outputs=gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
31
  title="Text Sentiment Analysis",
32
  description="Analyze the sentiment of text using TextBlob"
33
  )
34
 
35
- # Launch the interface and MCP server
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  if __name__ == "__main__":
37
- demo.launch(mcp_server=True)
 
 
 
 
1
  import json
2
  import gradio as gr
3
+ from gradio_mcp import enable_mcp
4
  from textblob import TextBlob
5
 
6
  def sentiment_analysis(text: str) -> str:
7
  """
8
  Analyze the sentiment of the given text.
9
+
10
  Args:
11
  text (str): The text to analyze
12
+
13
  Returns:
14
  str: A JSON string containing polarity, subjectivity, and assessment
15
  """
 
21
  "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
22
  "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
23
  }
24
+
25
+ return json.dumps(result, indent=2)
26
 
27
  # Create the Gradio interface
28
  demo = gr.Interface(
29
  fn=sentiment_analysis,
30
  inputs=gr.Textbox(placeholder="Enter text to analyze..."),
31
+ outputs=gr.Textbox(),
32
  title="Text Sentiment Analysis",
33
  description="Analyze the sentiment of text using TextBlob"
34
  )
35
 
36
+ # Enable MCP server with proper configuration
37
+ mcp_server = enable_mcp(
38
+ demo,
39
+ tool_config={
40
+ "sentiment_analysis": {
41
+ "description": "Analyze the sentiment of the given text. Returns polarity (-1 to 1), subjectivity (0 to 1), and overall assessment (positive/negative/neutral).",
42
+ "parameters": {
43
+ "type": "object",
44
+ "properties": {
45
+ "text": {
46
+ "type": "string",
47
+ "description": "The text to analyze for sentiment"
48
+ }
49
+ },
50
+ "required": ["text"]
51
+ }
52
+ }
53
+ }
54
+ )
55
+
56
+ # Launch the interface
57
  if __name__ == "__main__":
58
+ demo.launch(
59
+ server_name="0.0.0.0",
60
+ server_port=7860
61
+ )