Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +41 -0
- README.md +63 -6
- app.py +52 -0
- pyproject.toml +19 -0
Dockerfile
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONUNBUFFERED=1 \
|
| 4 |
+
DEBIAN_FRONTEND=noninteractive \
|
| 5 |
+
PYTHONPATH=/app:/app/common:$PYTHONPATH
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
# System deps
|
| 10 |
+
RUN apt-get update && apt-get install -y \
|
| 11 |
+
git build-essential curl \
|
| 12 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 13 |
+
|
| 14 |
+
# Install uv
|
| 15 |
+
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
|
| 16 |
+
ENV PATH="/root/.local/bin:$PATH"
|
| 17 |
+
|
| 18 |
+
# Copy project metadata
|
| 19 |
+
COPY app.py .
|
| 20 |
+
COPY pyproject.toml .
|
| 21 |
+
COPY uv.lock .
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Install dependencies using uv, then export and install with pip to system
|
| 25 |
+
RUN uv sync --frozen --no-dev && \
|
| 26 |
+
uv pip install -e . --system
|
| 27 |
+
|
| 28 |
+
# --- Pre-download model to speed up startup ---
|
| 29 |
+
RUN python - <<EOF
|
| 30 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 31 |
+
|
| 32 |
+
model_id = "mishrabp/bert-base-uncased-tweet-sentiment-analysis"
|
| 33 |
+
AutoTokenizer.from_pretrained(model_id)
|
| 34 |
+
AutoModelForSequenceClassification.from_pretrained(model_id)
|
| 35 |
+
EOF
|
| 36 |
+
|
| 37 |
+
# --- Expose Hugging Face Space port ---
|
| 38 |
+
EXPOSE 7860
|
| 39 |
+
|
| 40 |
+
# --- Run Streamlit app ---
|
| 41 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
README.md
CHANGED
|
@@ -1,10 +1,67 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
-
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Tweet Sentiment Analyzer
|
| 3 |
+
emoji: 📝
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: true
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Tweet Sentiment Analyzer
|
| 12 |
+
|
| 13 |
+
This is a **Streamlit app** that uses a fine-tuned BERT model to classify the sentiment of tweets.
|
| 14 |
+
It predicts whether a tweet expresses **joy, sadness, anger, love, fear, or surprise**.
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
## How to Use
|
| 19 |
+
|
| 20 |
+
1. Enter one or multiple tweets in the text area (one per line).
|
| 21 |
+
2. Click **Analyze Sentiment**.
|
| 22 |
+
3. The app will display each tweet's predicted sentiment along with the confidence score.
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## Example
|
| 27 |
+
|
| 28 |
+
Tweet: I love spending time with my friends!
|
| 29 |
+
Sentiment: joy (0.95)
|
| 30 |
+
|
| 31 |
+
Tweet: I feel so sad about the news today.
|
| 32 |
+
Sentiment: sadness (0.88)
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
## Model Used
|
| 37 |
+
|
| 38 |
+
- **Hugging Face Model:** `mishrabp/bert-base-uncased-tweet-sentiment-analysis`
|
| 39 |
+
- **Base Model:** `bert-base-uncased`
|
| 40 |
+
- **Task:** Tweet Sentiment Analysis
|
| 41 |
+
- **Language:** English
|
| 42 |
+
|
| 43 |
+
---
|
| 44 |
+
|
| 45 |
+
## About
|
| 46 |
+
|
| 47 |
+
This app is useful for:
|
| 48 |
+
|
| 49 |
+
- Social media managers to monitor audience sentiment.
|
| 50 |
+
- Researchers analyzing public reactions.
|
| 51 |
+
- Companies tracking customer feedback.
|
| 52 |
+
- Quick, automated sentiment analysis pipelines.
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
## Installation (for local run)
|
| 57 |
+
|
| 58 |
+
```bash
|
| 59 |
+
pip install streamlit transformers torch
|
| 60 |
+
streamlit run app.py
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## Author
|
| 64 |
+
|
| 65 |
+
Developed by Bibhu Mishra
|
| 66 |
+
|
| 67 |
+
---
|
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
+
|
| 4 |
+
# --- Page config ---
|
| 5 |
+
st.set_page_config(
|
| 6 |
+
page_title="Tweet Sentiment Analyzer",
|
| 7 |
+
page_icon="📝",
|
| 8 |
+
layout="centered"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# --- App title and description ---
|
| 12 |
+
st.title("📝 Tweet Sentiment Analyzer")
|
| 13 |
+
st.markdown("""
|
| 14 |
+
This app uses a fine-tuned BERT model (**_mishrabp/bert-base-uncased-tweet-sentiment-analysis_**) to classify the sentiment of tweets.
|
| 15 |
+
You can enter one or multiple tweets (one per line), and the model will predict
|
| 16 |
+
whether each tweet expresses **joy, sadness, anger, love, fear, or surprise**.
|
| 17 |
+
""")
|
| 18 |
+
|
| 19 |
+
# --- Load model and tokenizer ---
|
| 20 |
+
@st.cache_resource(show_spinner=True)
|
| 21 |
+
def load_model(model_name):
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 23 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 24 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 25 |
+
return classifier
|
| 26 |
+
|
| 27 |
+
model_name = "mishrabp/bert-base-uncased-tweet-sentiment-analysis"
|
| 28 |
+
classifier = load_model(model_name)
|
| 29 |
+
|
| 30 |
+
# --- Text input for tweets ---
|
| 31 |
+
tweets_input = st.text_area(
|
| 32 |
+
"Enter your tweets (one per line):",
|
| 33 |
+
height=200
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# --- Button to run classification ---
|
| 37 |
+
if st.button("Analyze Sentiment"):
|
| 38 |
+
if not tweets_input.strip():
|
| 39 |
+
st.warning("Please enter at least one tweet to analyze.")
|
| 40 |
+
else:
|
| 41 |
+
tweets = [line.strip() for line in tweets_input.split("\n") if line.strip()]
|
| 42 |
+
with st.spinner("Classifying tweets..."):
|
| 43 |
+
results = classifier(tweets)
|
| 44 |
+
|
| 45 |
+
# --- Display results ---
|
| 46 |
+
st.subheader("Results:")
|
| 47 |
+
for tweet, result in zip(tweets, results):
|
| 48 |
+
label = result["label"]
|
| 49 |
+
score = result["score"]
|
| 50 |
+
st.write(f"**Tweet:** {tweet}")
|
| 51 |
+
st.write(f"**Sentiment:** {label} ({score:.2f})")
|
| 52 |
+
st.markdown("---")
|
pyproject.toml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[build-system]
|
| 2 |
+
requires = ["setuptools>=42", "wheel"]
|
| 3 |
+
build-backend = "setuptools.build_meta"
|
| 4 |
+
|
| 5 |
+
[project]
|
| 6 |
+
name = "tweet-sentiment-analyzer"
|
| 7 |
+
version = "0.1.0"
|
| 8 |
+
description = "Streamlit app for tweet sentiment analysis using a fine-tuned BERT model."
|
| 9 |
+
authors = [
|
| 10 |
+
{name="Bibhu Mishra", email="bibhu.mishra@example.com"}
|
| 11 |
+
]
|
| 12 |
+
readme = "README.md"
|
| 13 |
+
requires-python = ">=3.8"
|
| 14 |
+
|
| 15 |
+
dependencies = [
|
| 16 |
+
"streamlit>=1.24.1",
|
| 17 |
+
"transformers>=4.30.0",
|
| 18 |
+
"torch>=2.1.0",
|
| 19 |
+
]
|