Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from EnYa32/NLPSentimentAnalysis: direct link, hf CLI and curl.
- Browser
- Download file 2.55 kB
-
https://huggingface.co/spaces/EnYa32/NLPSentimentAnalysis/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/EnYa32/NLPSentimentAnalysis/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/EnYa32/NLPSentimentAnalysis/resolve/main/src/streamlit_app.py
2.55 kB
| import re | |
| from pathlib import Path | |
| import joblib | |
| import streamlit as st | |
| # ------------------------- | |
| # Page config | |
| # ------------------------- | |
| st.set_page_config( | |
| page_title='Movie Review Sentiment (LinearSVC + TF-IDF)', | |
| page_icon='🎬', | |
| layout='centered' | |
| ) | |
| st.title('🎬 Movie Review Sentiment Analysis') | |
| st.write( | |
| 'Classify Rotten Tomatoes review phrases into 5 sentiment classes using a trained ' | |
| 'LinearSVC + TF-IDF pipeline.' | |
| ) | |
| # ------------------------- | |
| # Paths (HF friendly) | |
| # ------------------------- | |
| BASE_DIR = Path(__file__).resolve().parent | |
| MODEL_PATH = BASE_DIR / 'sentiment_pipeline.pkl' | |
| LABELS_PATH = BASE_DIR / 'label_names.pkl' | |
| # ------------------------- | |
| # Helpers | |
| # ------------------------- | |
| def clean_text(text: str) -> str: | |
| if not isinstance(text, str): | |
| return '' | |
| text = text.lower() | |
| text = re.sub(r'<[^>]+>', ' ', text) | |
| text = re.sub(r'\d+', ' ', text) | |
| text = re.sub(r'[\n\r]', ' ', text) | |
| text = re.sub(r"[^a-z\s']", ' ', text) | |
| text = re.sub(r'\s+', ' ', text) | |
| return text.strip() | |
| def load_model(): | |
| if not MODEL_PATH.exists(): | |
| raise FileNotFoundError( | |
| f'Model not found: {MODEL_PATH.name}. Put it in the repo root (same folder as app.py).' | |
| ) | |
| return joblib.load(MODEL_PATH) | |
| def load_labels(): | |
| if LABELS_PATH.exists(): | |
| return joblib.load(LABELS_PATH) | |
| return { | |
| 0: 'negative', | |
| 1: 'somewhat negative', | |
| 2: 'neutral', | |
| 3: 'somewhat positive', | |
| 4: 'positive' | |
| } | |
| model = load_model() | |
| label_names = load_labels() | |
| # ------------------------- | |
| # UI | |
| # ------------------------- | |
| st.subheader('Enter a phrase') | |
| default_text = 'An intermittently pleasing but mostly routine effort.' | |
| user_text = st.text_area('Movie review phrase', value=default_text, height=120) | |
| col1, col2 = st.columns([1, 1]) | |
| with col1: | |
| run = st.button('Predict', type='primary') | |
| with col2: | |
| st.write('') | |
| if run: | |
| cleaned = clean_text(user_text) | |
| if cleaned.strip() == '': | |
| st.warning('Please enter some text (not empty).') | |
| else: | |
| pred = int(model.predict([cleaned])[0]) | |
| label = label_names.get(pred, str(pred)) | |
| st.success(f'Prediction: **{pred} — {label}**') | |
| with st.expander('Show cleaned text'): | |
| st.code(cleaned) | |
| st.markdown('---') | |
| st.caption( | |
| 'Model: TF-IDF (1–2 grams) + LinearSVC | Labels: 0=negative, 1=somewhat negative, ' | |
| '2=neutral, 3=somewhat positive, 4=positive' | |
| ) |