Download app.py from IMDBSentimentalAnalysis/IMDB_Sentimrntal_Analysis: direct link, hf CLI and curl.
- Browser
- Download file 2.32 kB
-
https://huggingface.co/IMDBSentimentalAnalysis/IMDB_Sentimrntal_Analysis/resolve/main/app.py
- Command line
-
hf download hf://IMDBSentimentalAnalysis/IMDB_Sentimrntal_Analysis/app.py
-
curl -L -o app.py https://huggingface.co/IMDBSentimentalAnalysis/IMDB_Sentimrntal_Analysis/resolve/main/app.py
2.32 kB
| import streamlit as st | |
| import pandas as pd | |
| import re | |
| from sklearn.feature_extraction.text import CountVectorizer | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.metrics import accuracy_score | |
| from sklearn.preprocessing import LabelEncoder | |
| # Streamlit Config | |
| st.set_page_config(page_title="IMDB Sentiment Analysis", layout="wide") | |
| st.title("๐ฌ IMDB Sentiment Analysis") | |
| # Load Dataset | |
| def load_data(): | |
| df = pd.read_csv(r"C:\Users\pavilion\Desktop\INNOMATICS_PROJECTS(SL)\IMDB_SENTIMENTAL_ANALYSIS\IMDB Dataset.csv") | |
| return df | |
| df = load_data() | |
| # Display Dataset | |
| st.write("### ๐ Dataset") | |
| st.dataframe(df) | |
| # Data Cleaning Function | |
| def clean_text(text): | |
| text = re.sub(r'<.*?>', '', text) # Remove HTML tags | |
| text = re.sub(r'[^a-zA-Z\s]', '', text) # Keep only letters | |
| return text.lower().strip() | |
| df["review"] = df["review"].apply(clean_text) | |
| st.write("### ๐ Cleaned Dataset") | |
| st.dataframe(df) | |
| # Feature Extraction & Model Training | |
| vectorizer = CountVectorizer(stop_words="english", max_features=10000) # Limit features | |
| X = vectorizer.fit_transform(df["review"]) # Keep it sparse | |
| label_encoder = LabelEncoder() | |
| y = label_encoder.fit_transform(df["sentiment"]) | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Train Model | |
| model = RandomForestClassifier(n_estimators=100, random_state=42) | |
| model.fit(X_train, y_train) | |
| y_pred = model.predict(X_test) | |
| # Model Accuracy | |
| accuracy = accuracy_score(y_test, y_pred) | |
| st.write(f"### ๐ฏ Model Accuracy: `{accuracy:.2f}`") | |
| # User Input for Prediction | |
| st.write("### โ๏ธ Enter Your Movie Review Below") | |
| text_input = st.text_area("Write your review here...") | |
| if st.button("๐ฎ Predict Sentiment"): | |
| if text_input.strip(): | |
| transformed_text = vectorizer.transform([clean_text(text_input)]) # Keep sparse | |
| prediction = model.predict(transformed_text) | |
| if prediction[0] == 1: | |
| st.success("๐ **Positive Review!**") | |
| st.balloons() | |
| else: | |
| st.error("โ๏ธ **Negative Review!**") | |
| st.snow() | |
| else: | |
| st.warning("โ ๏ธ Please enter a review before predicting.") | |