shayankhan7 commited on
Commit
ed55257
·
verified ·
1 Parent(s): 3f968f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -1,32 +1,26 @@
1
  import streamlit as st
 
2
 
3
- # Sample positive and negative word lists
4
- positive_words = ["good", "great", "happy", "excellent", "love", "wonderful", "amazing", "nice", "awesome"]
5
- negative_words = ["bad", "terrible", "sad", "hate", "horrible", "awful", "worst", "angry", "disappointed"]
 
6
 
7
- def analyze_sentiment(text):
8
- text = text.lower()
9
- pos_count = sum(word in text for word in positive_words)
10
- neg_count = sum(word in text for word in negative_words)
11
-
12
- if pos_count > neg_count:
13
- return "Positive 😊", pos_count, neg_count
14
- elif neg_count > pos_count:
15
- return "Negative 😞", pos_count, neg_count
16
- else:
17
- return "Neutral 😐", pos_count, neg_count
18
 
19
  # Streamlit UI
20
- st.title("Simple Sentiment Analyzer (No ML) 🧠")
21
- st.write("This app detects sentiment using rule-based logic (no ML model).")
22
 
23
- text_input = st.text_area("Enter your sentence here:")
24
 
25
- if st.button("Analyze"):
26
- if text_input.strip() == "":
27
- st.warning("Please enter some text!")
28
  else:
29
- sentiment, pos, neg = analyze_sentiment(text_input)
30
- st.subheader("Result:")
31
- st.write(f"**Sentiment:** {sentiment}")
32
- st.write(f"Positive Words: {pos}, Negative Words: {neg}")
 
 
 
1
  import streamlit as st
2
+ from transformers import pipeline
3
 
4
+ # Load sentiment-analysis pipeline from Hugging Face
5
+ @st.cache_resource
6
+ def load_model():
7
+ return pipeline("sentiment-analysis")
8
 
9
+ sentiment_pipeline = load_model()
 
 
 
 
 
 
 
 
 
 
10
 
11
  # Streamlit UI
12
+ st.title("Smart Sentiment Analyzer 🤖")
13
+ st.markdown("Analyze your sentence using a real AI model (DistilBERT).")
14
 
15
+ text = st.text_area("Enter your sentence:")
16
 
17
+ if st.button("Analyze Sentiment"):
18
+ if text.strip() == "":
19
+ st.warning("Please enter some text.")
20
  else:
21
+ result = sentiment_pipeline(text)[0]
22
+ label = result["label"]
23
+ score = round(result["score"] * 100, 2)
24
+
25
+ st.success(f"**Sentiment:** {label}")
26
+ st.info(f"**Confidence:** {score}%")