Igor Pavlov commited on
Commit
9f42e99
·
1 Parent(s): 279a905

Deploy final MCP-based sentiment + sarcasm analyzer

Browse files
Files changed (4) hide show
  1. .gitignore +0 -0
  2. README.md +1 -1
  3. app.py +33 -13
  4. requirements.txt +3 -2
.gitignore ADDED
Binary file (16 Bytes). View file
 
README.md CHANGED
@@ -8,7 +8,7 @@ sdk_version: 5.35.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
- short_description: Sentiment analysis tool using Gradio and TextBlob, with MCP
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ short_description: Sentiment + Sarcasm Analyzer tool using Gradio, DistilBERT with MCP
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,35 +1,55 @@
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
  """
15
- blob = TextBlob(text)
16
- sentiment = blob.sentiment
 
 
17
 
18
  result = {
19
- "polarity": round(sentiment.polarity, 2),
20
- "subjectivity": round(sentiment.subjectivity, 2),
21
- "assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
 
22
  }
23
 
24
  return json.dumps(result)
25
 
26
  demo = gr.Interface(
27
- fn=sentiment_analysis,
28
  inputs=gr.Textbox(placeholder="Enter text to analyze..."),
29
- outputs=gr.Textbox(), # Use gr.Textbox() instead of gr.JSON()
30
- title="Text Sentiment Analysis",
31
- description="Analyze the sentiment of text using TextBlob"
 
 
 
 
 
 
 
 
 
 
 
 
32
  )
33
 
 
34
  if __name__ == "__main__":
35
  demo.launch(mcp_server=True)
 
1
  import json
2
  import gradio as gr
3
+ from transformers import pipeline
4
 
5
+ # Load models (both CPU-friendly)
6
+ sentiment_classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
7
+ sarcasm_classifier = pipeline("text-classification", model="helinivan/english-sarcasm-detector")
8
+
9
+ def analyze_text(text: str) -> str:
10
  """
11
+ Analyze sentiment and detect sarcasm in the given text.
12
 
13
  Args:
14
  text (str): The text to analyze
15
 
16
  Returns:
17
+ str: A JSON string with sentiment and sarcasm info
18
  """
19
+ sentiment_result = sentiment_classifier(text)[0]
20
+ sarcasm_result = sarcasm_classifier(text)[0]
21
+ sarcasm_label = sarcasm_result["label"].lower()
22
+ sarcasm_score = round(sarcasm_result["score"], 3)
23
 
24
  result = {
25
+ "assessment": sentiment_result["label"].lower(), # positive / negative
26
+ "confidence": round(sentiment_result["score"], 3),
27
+ "sarcasm_detected": sarcasm_label == "sarcasm" or sarcasm_score > 0.9,
28
+ "sarcasm_confidence": sarcasm_score
29
  }
30
 
31
  return json.dumps(result)
32
 
33
  demo = gr.Interface(
34
+ fn=analyze_text,
35
  inputs=gr.Textbox(placeholder="Enter text to analyze..."),
36
+ outputs=gr.Textbox(),
37
+ title="Sentiment + Sarcasm Analyzer",
38
+ description=(
39
+ "This app performs sentiment analysis and sarcasm detection using CPU-compatible Hugging Face models. "
40
+ "Integrated with Hugging Face's MCP (Multimodal Client Protocol) for seamless agent-to-app communication.\n\n"
41
+ "⚙️ Models used:**\n\n"
42
+ " • `distilbert-base-uncased-finetuned-sst-2-english` — sentiment analysis\n\n"
43
+ " • `helinivan/english-sarcasm-detector` — sarcasm detection (fine-tuned BERT)\n\n"
44
+ "🧾 Output format:**\n\n"
45
+ " • `assessment`: Sentiment label (`\"positive\"` or `\"negative\"`)\n\n"
46
+ " • `confidence`: Sentiment model's confidence score\n\n"
47
+ " • `sarcasm_detected`: Boolean indicating if sarcasm was detected\n\n"
48
+ " • `sarcasm_confidence`: Confidence score from sarcasm classifier\n\n"
49
+ "🚩 Use the **Flag** button to report interesting or incorrect outputs (e.g., edge cases or sarcasm errors)."
50
+ )
51
  )
52
 
53
+
54
  if __name__ == "__main__":
55
  demo.launch(mcp_server=True)
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
- gradio[mcp]
2
- textblob
 
 
1
+ gradio
2
+ transformers
3
+ torch