carolinacon commited on
Commit
122e16f
·
1 Parent(s): 728de76

Updated app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -4
app.py CHANGED
@@ -1,7 +1,8 @@
 
1
  import gradio as gr
2
  from textblob import TextBlob
3
 
4
- def sentiment_analysis(text: str) -> dict:
5
  """
6
  Analyze the sentiment of the given text.
7
 
@@ -9,22 +10,24 @@ def sentiment_analysis(text: str) -> dict:
9
  text (str): The text to analyze
10
 
11
  Returns:
12
- dict: A dictionary containing polarity, subjectivity, and assessment
13
  """
14
  blob = TextBlob(text)
15
  sentiment = blob.sentiment
16
 
17
- return {
18
  "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
19
  "subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
20
  "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
21
  }
22
 
 
 
23
  # Create the Gradio interface
24
  demo = gr.Interface(
25
  fn=sentiment_analysis,
26
  inputs=gr.Textbox(placeholder="Enter text to analyze..."),
27
- outputs=gr.JSON(),
28
  title="Text Sentiment Analysis",
29
  description="Analyze the sentiment of text using TextBlob"
30
  )
 
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
 
 
10
  text (str): The text to analyze
11
 
12
  Returns:
13
+ str: A JSON string containing polarity, subjectivity, and assessment
14
  """
15
  blob = TextBlob(text)
16
  sentiment = blob.sentiment
17
 
18
+ result = {
19
  "polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
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
  )