File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change @@ -31,33 +31,36 @@ pip install "gradio[mcp]" textblob
3131Create a new file called ` app.py ` with the following code:
3232
3333``` python
34+ import json
3435import gradio as gr
3536from 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
5760demo = 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)
You can’t perform that action at this time.
0 commit comments