likhonhfai commited on
Commit
c8a0486
·
verified ·
1 Parent(s): b2f04a3

Update app.py to use rule-based sentiment analysis and remove transformers dependency

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -1,23 +1,34 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Load a small sentiment analysis pipeline
5
- classifier = pipeline("sentiment-analysis")
 
6
 
7
  def analyze_sentiment(text):
8
- result = classifier(text)[0]
9
- label = result["label"]
10
- score = result["score"]
11
- return f"{label} ({score:.2f})"
 
 
 
 
 
 
 
 
 
12
 
13
- # Create Gradio interface
14
- iface = gr.Interface(
15
  fn=analyze_sentiment,
16
- inputs=gr.Textbox(lines=5, placeholder="Enter text here...", label="Input Text"),
17
- outputs=gr.Textbox(label="Sentiment"),
18
- title="Tiny AI Sentiment Analyzer",
19
- description="Enter a text snippet and get its sentiment (positive/negative) using a small Hugging Face model."
 
 
 
20
  )
21
 
22
  if __name__ == "__main__":
23
- iface.launch()
 
1
  import gradio as gr
 
2
 
3
+ # Simple rule-based sentiment analysis using sets of positive and negative words
4
+ positive_words = {"good", "great", "happy", "fantastic", "excellent", "love", "awesome", "amazing", "wonderful", "like", "nice"}
5
+ negative_words = {"bad", "terrible", "sad", "horrible", "awful", "hate", "angry", "poor", "disappointing", "worst"}
6
 
7
  def analyze_sentiment(text):
8
+ words = text.lower().split()
9
+ pos_count = sum(word in positive_words for word in words)
10
+ neg_count = sum(word in negative_words for word in words)
11
+ if pos_count > neg_count:
12
+ sentiment = "Positive"
13
+ score = pos_count / (pos_count + neg_count) if (pos_count + neg_count) > 0 else 0
14
+ elif neg_count > pos_count:
15
+ sentiment = "Negative"
16
+ score = neg_count / (pos_count + neg_count) if (pos_count + neg_count) > 0 else 0
17
+ else:
18
+ sentiment = "Neutral"
19
+ score = 0.5 if (pos_count + neg_count) > 0 else 0.5
20
+ return sentiment, round(score, 2)
21
 
22
+ interface = gr.Interface(
 
23
  fn=analyze_sentiment,
24
+ inputs=gr.Textbox(label="Enter text"),
25
+ outputs=[
26
+ gr.Textbox(label="Sentiment"),
27
+ gr.Number(label="Score")
28
+ ],
29
+ title="Rule-based Sentiment Analysis",
30
+ description="A tiny AI model that performs simple sentiment analysis using a list of positive and negative words."
31
  )
32
 
33
  if __name__ == "__main__":
34
+ interface.launch()