danielle2003 commited on
Commit
c178054
Β·
1 Parent(s): dab2c77
Files changed (2) hide show
  1. .github/workflows/ci-cd.yml +2 -0
  2. app.py +46 -8
.github/workflows/ci-cd.yml CHANGED
@@ -19,6 +19,8 @@ jobs:
19
  run: |
20
  pip install --upgrade pip
21
  pip install -r requirements.txt
 
 
22
 
23
  - name: Train Model
24
  run: python scripts/train.py
 
19
  run: |
20
  pip install --upgrade pip
21
  pip install -r requirements.txt
22
+ - name: Install dependencies
23
+ run: pip install transformers[torch] accelerate>=0.26.0
24
 
25
  - name: Train Model
26
  run: python scripts/train.py
app.py CHANGED
@@ -1,12 +1,50 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
 
3
 
4
- # Load trained model
5
- classifier = pipeline("text-classification", model="./models")
 
 
 
 
6
 
7
- st.title("Sentiment Analysis with DistilBERT")
8
- text = st.text_area("Enter a text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- if st.button("Predict"):
11
- result = classifier(text)
12
- st.write(result)
 
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