Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Title
|
| 6 |
+
st.title("π¬ Movie Review Sentiment Classifier")
|
| 7 |
+
|
| 8 |
+
# Load model from Hugging Face Hub
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
return pipeline("sentiment-analysis", model="Gamer-Dude-77/my-imdb-sentiment-model")
|
| 12 |
+
|
| 13 |
+
classifier = load_model()
|
| 14 |
+
|
| 15 |
+
# Text input
|
| 16 |
+
st.subheader("Enter a Review")
|
| 17 |
+
text_input = st.text_area("Type or paste your movie review below:", height=150)
|
| 18 |
+
|
| 19 |
+
# Prediction
|
| 20 |
+
if st.button("Analyze Sentiment"):
|
| 21 |
+
if text_input.strip():
|
| 22 |
+
results = classifier([text_input])
|
| 23 |
+
result = results[0]
|
| 24 |
+
|
| 25 |
+
st.write("### π Prediction Result")
|
| 26 |
+
if result["label"].upper() == "POSITIVE":
|
| 27 |
+
st.success(f"π Positive (Confidence: {result['score']:.4f})")
|
| 28 |
+
elif result["label"].upper() == "NEGATIVE":
|
| 29 |
+
st.error(f"π Negative (Confidence: {result['score']:.4f})")
|
| 30 |
+
else:
|
| 31 |
+
st.info(f"π Neutral (Confidence: {result['score']:.4f})")
|
| 32 |
+
else:
|
| 33 |
+
st.warning("β οΈ Please enter some text to analyze.")
|
| 34 |
+
|
| 35 |
+
# Batch testing
|
| 36 |
+
st.subheader("Batch Testing")
|
| 37 |
+
uploaded_file = st.file_uploader("Upload a .txt file with one review per line", type=["txt"])
|
| 38 |
+
|
| 39 |
+
if uploaded_file is not None:
|
| 40 |
+
lines = uploaded_file.read().decode("utf-8").splitlines()
|
| 41 |
+
if st.button("Analyze File"):
|
| 42 |
+
results = classifier(lines)
|
| 43 |
+
for review, res in zip(lines, results):
|
| 44 |
+
if res["label"].upper() == "POSITIVE":
|
| 45 |
+
st.success(f"Review: {review}\nβ π Positive ({res['score']:.4f})")
|
| 46 |
+
elif res["label"].upper() == "NEGATIVE":
|
| 47 |
+
st.error(f"Review: {review}\nβ π Negative ({res['score']:.4f})")
|
| 48 |
+
else:
|
| 49 |
+
st.info(f"Review: {review}\nβ π Neutral ({res['score']:.4f})")
|
| 50 |
+
st.markdown("---")
|