Spaces:
Sleeping
Sleeping
Commit
Β·
c178054
1
Parent(s):
dab2c77
require
Browse files- .github/workflows/ci-cd.yml +2 -0
- 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 |
-
|
|
|
|
|
3 |
|
4 |
-
# Load trained model
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|