danielle2003 commited on
Commit
98db479
Β·
1 Parent(s): e631481
Files changed (1) hide show
  1. app.py +72 -39
app.py CHANGED
@@ -1,50 +1,83 @@
1
  import streamlit as st
2
  import torch
3
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
- import torch.nn.functional as F
5
 
6
- # Load trained model & tokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  @st.cache_resource
8
  def load_model():
9
- model = AutoModelForSequenceClassification.from_pretrained("models/sentiment_model")
10
- tokenizer = AutoTokenizer.from_pretrained("models/sentiment_model")
11
- return model, tokenizer
12
 
13
- model, tokenizer = load_model()
 
14
 
15
  # Streamlit UI
16
- st.set_page_config(page_title="Sentiment Analyzer", page_icon="πŸ’¬", layout="wide")
17
-
18
- st.title("πŸ’¬ Sentiment Analyzer")
19
- st.write("Analyze the sentiment of any text! Enter a sentence below and get an instant analysis.")
20
-
21
- user_input = st.text_area("Enter your text:", "")
22
-
23
- if st.button("Analyze Sentiment"):
24
- if user_input:
25
- with st.spinner("Analyzing..."):
26
- inputs = tokenizer(user_input, return_tensors="pt", truncation=True, padding=True)
27
- outputs = model(**inputs)
28
- probs = F.softmax(outputs.logits, dim=-1)
29
- sentiment_index = torch.argmax(probs).item()
30
- confidence = round(probs[0][sentiment_index].item() * 100, 2)
31
-
32
- # Map index to label
33
- labels = ["Negative", "Neutral", "Positive"] # Adjust this based on your training labels
34
- sentiment = labels[sentiment_index]
35
-
36
- # Display result
37
- st.subheader("πŸ” Result")
38
- if sentiment == "Positive":
39
- st.success(f"😊 **Positive Sentiment** ({confidence}%)")
40
- elif sentiment == "Negative":
41
- st.error(f"😠 **Negative Sentiment** ({confidence}%)")
42
- else:
43
- st.warning(f"😐 **Neutral Sentiment** ({confidence}%)")
44
 
45
- else:
46
- st.warning("⚠️ Please enter some text.")
47
 
48
- st.markdown("---")
49
- st.markdown("πŸ”— Built with Streamlit | Model: DistilBERT (Fine-tuned)")
 
 
 
50
 
 
 
 
 
 
1
  import streamlit as st
2
  import torch
3
+ from transformers import pipeline
 
4
 
5
+ # Set device (GPU if available)
6
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ # Custom Streamlit Styles
9
+ st.markdown("""
10
+ <style>
11
+ /* Center everything */
12
+ .block-container {
13
+ max-width: 650px;
14
+ text-align: center;
15
+ }
16
+
17
+ /* Title styling */
18
+ .title {
19
+ font-size: 2.5rem;
20
+ font-weight: bold;
21
+ color: #FF4B4B;
22
+ text-shadow: 2px 2px 10px rgba(255, 75, 75, 0.5);
23
+ }
24
+
25
+ /* Text input styling */
26
+ .stTextArea textarea {
27
+ border-radius: 10px;
28
+ border: 2px solid #FF4B4B;
29
+ background-color: #1E1E1E;
30
+ color: white;
31
+ font-size: 16px;
32
+ }
33
+
34
+ /* Button styling */
35
+ div.stButton > button {
36
+ background-color: #FF4B4B;
37
+ color: white;
38
+ border-radius: 10px;
39
+ font-size: 18px;
40
+ padding: 10px 20px;
41
+ transition: 0.3s;
42
+ }
43
+ div.stButton > button:hover {
44
+ background-color: #E63E3E;
45
+ }
46
+
47
+ /* Result display */
48
+ .result {
49
+ font-size: 22px;
50
+ font-weight: bold;
51
+ color: #FF4B4B;
52
+ margin-top: 20px;
53
+ }
54
+ </style>
55
+ """, unsafe_allow_html=True)
56
+
57
+ # Load Model from Hugging Face
58
  @st.cache_resource
59
  def load_model():
60
+ model_name = "distilbert-base-uncased-finetuned-sst-2-english" # Replace with your actual HF model
61
+ classifier = pipeline("text-classification", model=model_name, tokenizer=model_name, device=0 if DEVICE == "cuda" else -1)
62
+ return classifier
63
 
64
+ # Initialize model
65
+ classifier = load_model()
66
 
67
  # Streamlit UI
68
+ st.markdown('<p class="title">Sentiment Analysis App πŸ’¬</p>', unsafe_allow_html=True)
69
+ st.write("Enter a review below and let AI analyze its sentiment! πŸš€")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ # User Input
72
+ text = st.text_area("Enter text:", "", height=150)
73
 
74
+ if st.button("Analyze"):
75
+ if text.strip():
76
+ result = classifier(text)[0]
77
+ sentiment = result['label']
78
+ confidence = result['score']
79
 
80
+ # Display sentiment result
81
+ st.markdown(f'<p class="result">Sentiment: {sentiment} ({confidence:.2%} confidence)</p>', unsafe_allow_html=True)
82
+ else:
83
+ st.warning("⚠️ Please enter some text!")