Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import joblib
|
| 4 |
+
from huggingface_hub import snapshot_download
|
| 5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 6 |
+
|
| 7 |
+
# Load the model and tokenizer from the repository
|
| 8 |
+
repo_id = "Makima57/sentiment-model-svc"
|
| 9 |
+
model_path = snapshot_download(repo_id=repo_id)
|
| 10 |
+
|
| 11 |
+
# Load saved SVC model
|
| 12 |
+
svc_model = joblib.load(f"{model_path}/svc_model.pkl")
|
| 13 |
+
|
| 14 |
+
# Load the saved TfidfVectorizer
|
| 15 |
+
vectorizer = TfidfVectorizer()
|
| 16 |
+
|
| 17 |
+
# Define prediction function
|
| 18 |
+
def predict_sentiment(text, model, vectorizer):
|
| 19 |
+
text_transformed = vectorizer.transform([text])
|
| 20 |
+
prediction = model.predict(text_transformed)
|
| 21 |
+
return "positive" if prediction == 1 else "negative"
|
| 22 |
+
|
| 23 |
+
# Streamlit app interface
|
| 24 |
+
st.title("IMDB Movie Review Sentiment Analyzer")
|
| 25 |
+
|
| 26 |
+
# Text input from user
|
| 27 |
+
user_input = st.text_area("Enter a movie review:", "")
|
| 28 |
+
|
| 29 |
+
# Predict sentiment when the user submits the input
|
| 30 |
+
if st.button("Analyze Sentiment"):
|
| 31 |
+
if user_input.strip() != "":
|
| 32 |
+
sentiment = predict_sentiment(user_input, svc_model, vectorizer)
|
| 33 |
+
st.write(f"The sentiment of the review is: **{sentiment}**")
|
| 34 |
+
else:
|
| 35 |
+
st.write("Please enter a valid review!")
|
| 36 |
+
|