Skip to content

Commit 7311c2a

Browse files
authored
Merge pull request #99 from bayesways/mcp-server-fix
fix mcp server to return json
2 parents 4727092 + e3d6ce7 commit 7311c2a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

units/en/unit2/gradio-server.mdx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,36 @@ pip install "gradio[mcp]" textblob
3131
Create a new file called `app.py` with the following code:
3232

3333
```python
34+
import json
3435
import gradio as gr
3536
from textblob import TextBlob
3637

37-
def sentiment_analysis(text: str) -> dict:
38+
def sentiment_analysis(text: str) -> str:
3839
"""
3940
Analyze the sentiment of the given text.
4041
4142
Args:
4243
text (str): The text to analyze
4344
4445
Returns:
45-
dict: A dictionary containing polarity, subjectivity, and assessment
46+
str: A JSON string containing polarity, subjectivity, and assessment
4647
"""
4748
blob = TextBlob(text)
4849
sentiment = blob.sentiment
4950

50-
return {
51+
result = {
5152
"polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
5253
"subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
5354
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
5455
}
5556

57+
return json.dumps(result)
58+
5659
# Create the Gradio interface
5760
demo = gr.Interface(
5861
fn=sentiment_analysis,
5962
inputs=gr.Textbox(placeholder="Enter text to analyze..."),
60-
outputs=gr.JSON(),
63+
outputs=gr.Textbox(), # Changed from gr.JSON() to gr.Textbox()
6164
title="Text Sentiment Analysis",
6265
description="Analyze the sentiment of text using TextBlob"
6366
)

0 commit comments

Comments
 (0)