File size: 1,321 Bytes
77cac57
 
 
 
 
 
 
71b3117
 
 
77cac57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71b3117
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
import torch

@st.cache_resource
def load_model():
    model_name = "finiteautomata/bertweet-base-sentiment-analysis"
    cache_dir = "/app/hf_cache"  # Use the cache set in Dockerfile
    tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
    model = AutoModelForSequenceClassification.from_pretrained(model_name, cache_dir=cache_dir)
    return pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# Load model
sentiment_pipeline = load_model()

# Streamlit UI
st.title("📝 Text Sentiment Analyzer")
st.write("Enter your text and click **Analyze** to see if it's Positive, Neutral, or Negative.")

# Text input
user_input = st.text_area("Enter text here:", height=150)

# Analyze button
if st.button("Analyze"):
    if not user_input.strip():
        st.warning("Please enter some text.")
    else:
        result = sentiment_pipeline(user_input)[0]
        label = result["label"]

        if label == "NEG":
            sentiment = "Negative"
        elif label == "NEU":
            sentiment = "Neutral"
        elif label == "POS":
            sentiment = "Positive"
        else:
            sentiment = "Unknown"

        st.success(f"**Sentiment:** {sentiment}")