Ayesha003 commited on
Commit
07d13ad
·
verified ·
1 Parent(s): 0bd2c96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -23
app.py CHANGED
@@ -1,28 +1,30 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
- from PIL import Image
 
4
 
5
- # Load sentiment analysis pipeline
6
  @st.cache_resource
7
  def load_model():
8
- return pipeline("sentiment-analysis")
 
 
 
9
 
10
- sentiment_analyzer = load_model()
11
 
12
  # Streamlit UI setup
13
  st.set_page_config(
14
  page_title="Mental Health Sentiment Insight",
15
  page_icon="🧠",
16
- layout="centered",
17
- initial_sidebar_state="expanded",
18
  )
19
 
20
- # --- Custom CSS for a modern look ---
21
  st.markdown("""
22
  <style>
23
  .main {
24
- background-color: #f0f2f6;
25
- padding: 20px;
26
  }
27
  .stTextInput>div>div>input {
28
  font-size: 16px;
@@ -42,23 +44,26 @@ st.markdown("""
42
  </style>
43
  """, unsafe_allow_html=True)
44
 
45
- # --- App Header ---
46
- st.markdown("## 🧠 Mental Health Sentiment Insight")
47
- st.markdown("Analyze your thoughts to understand your emotional state. This app uses a powerful AI model to detect **Positive**, **Negative**, or **Neutral** sentiment.")
48
 
49
- # --- User Input ---
50
- text_input = st.text_area("✍️ Write your thoughts here:", height=200, placeholder="Type something you're feeling...")
51
 
52
  if st.button("Analyze Sentiment"):
53
- if text_input.strip() == "":
54
  st.warning("Please enter some text to analyze.")
55
  else:
56
- with st.spinner("Analyzing..."):
57
- result = sentiment_analyzer(text_input)[0]
58
- label = result['label']
59
- score = result['score']
 
 
60
 
61
- # --- Custom Color Feedback ---
62
- color = "🟢" if label == "POSITIVE" else "🔴" if label == "NEGATIVE" else "🟡"
63
 
64
- st.success(f"{color} **Sentiment:** {label} \n\n **Confidence:** {score:.2%}")
 
 
 
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import torch.nn.functional as F
5
 
6
+ # Load model and tokenizer
7
  @st.cache_resource
8
  def load_model():
9
+ model_name = "distilbert-base-uncased-finetuned-sst-2-english"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
12
+ return tokenizer, model
13
 
14
+ tokenizer, model = load_model()
15
 
16
  # Streamlit UI setup
17
  st.set_page_config(
18
  page_title="Mental Health Sentiment Insight",
19
  page_icon="🧠",
20
+ layout="centered"
 
21
  )
22
 
23
+ # Custom UI
24
  st.markdown("""
25
  <style>
26
  .main {
27
+ background-color: #f4f6f9;
 
28
  }
29
  .stTextInput>div>div>input {
30
  font-size: 16px;
 
44
  </style>
45
  """, unsafe_allow_html=True)
46
 
47
+ st.title("🧠 Mental Health Sentiment Insight")
48
+ st.markdown("This app helps analyze your thoughts to detect **Positive** or **Negative** emotions using an AI model.")
 
49
 
50
+ text = st.text_area("✍️ Share your thoughts here:", height=180)
 
51
 
52
  if st.button("Analyze Sentiment"):
53
+ if not text.strip():
54
  st.warning("Please enter some text to analyze.")
55
  else:
56
+ with st.spinner("Analyzing sentiment..."):
57
+ # Tokenize and predict
58
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
59
+ outputs = model(**inputs)
60
+ probs = F.softmax(outputs.logits, dim=1)
61
+ confidence, predicted = torch.max(probs, dim=1)
62
 
63
+ label = model.config.id2label[predicted.item()]
64
+ score = confidence.item()
65
 
66
+ # Emoji based on label
67
+ emoji = "🟢" if label == "POSITIVE" else "🔴"
68
+
69
+ st.success(f"{emoji} **Sentiment**: {label}\n\n**Confidence**: {score:.2%}")