Spaces:
Sleeping
Sleeping
Commit
·
50ab210
0
Parent(s):
deploy
Browse files- Dockerfile +18 -0
- README.md +8 -0
- app.py +72 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python image from Docker Hub
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy the application files
|
| 8 |
+
COPY app.py /app/
|
| 9 |
+
COPY requirements.txt /app/
|
| 10 |
+
|
| 11 |
+
# Install dependencies
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Expose the Streamlit port
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Run the Streamlit app
|
| 18 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Sentiment Analysis App
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
# Load model and tokenizer
|
| 9 |
+
model_name = "tabularisai/multilingual-sentiment-analysis"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 12 |
+
|
| 13 |
+
# Function for sentiment prediction with progress bar
|
| 14 |
+
def predict_sentiment(texts):
|
| 15 |
+
sentiments = []
|
| 16 |
+
sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
|
| 17 |
+
progress_bar = st.progress(0)
|
| 18 |
+
total_texts = len(texts)
|
| 19 |
+
|
| 20 |
+
for i, text in enumerate(texts):
|
| 21 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 25 |
+
sentiment = sentiment_map[torch.argmax(probabilities, dim=-1).item()]
|
| 26 |
+
sentiments.append(sentiment)
|
| 27 |
+
|
| 28 |
+
# Update progress bar
|
| 29 |
+
progress_bar.progress((i + 1) / total_texts)
|
| 30 |
+
time.sleep(0.1) # Optional: To better visualize progress
|
| 31 |
+
|
| 32 |
+
return sentiments
|
| 33 |
+
|
| 34 |
+
# Streamlit UI
|
| 35 |
+
st.title("Sentiment Analysis App")
|
| 36 |
+
st.write("Upload a CSV or Excel file containing text data for sentiment analysis.")
|
| 37 |
+
|
| 38 |
+
# File upload
|
| 39 |
+
uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
|
| 40 |
+
|
| 41 |
+
if uploaded_file is not None:
|
| 42 |
+
# Read file
|
| 43 |
+
if uploaded_file.name.endswith(".csv"):
|
| 44 |
+
df = pd.read_csv(uploaded_file)
|
| 45 |
+
else:
|
| 46 |
+
df = pd.read_excel(uploaded_file)
|
| 47 |
+
|
| 48 |
+
st.write("Dataset Preview:")
|
| 49 |
+
st.dataframe(df.head())
|
| 50 |
+
|
| 51 |
+
# Select text column
|
| 52 |
+
text_column = st.selectbox("Select the text column for analysis", df.columns)
|
| 53 |
+
|
| 54 |
+
if st.button("Analyze Sentiment"):
|
| 55 |
+
# Get text data
|
| 56 |
+
texts = df[text_column].astype(str).tolist()
|
| 57 |
+
|
| 58 |
+
# Predict sentiments with progress bar
|
| 59 |
+
sentiments = predict_sentiment(texts)
|
| 60 |
+
df["Sentiment"] = sentiments
|
| 61 |
+
|
| 62 |
+
# Display results
|
| 63 |
+
st.write("Sentiment Analysis Results:")
|
| 64 |
+
st.dataframe(df[[text_column, "Sentiment"]])
|
| 65 |
+
|
| 66 |
+
# Pie chart of sentiment distribution
|
| 67 |
+
st.write("Sentiment Distribution:")
|
| 68 |
+
sentiment_counts = df["Sentiment"].value_counts()
|
| 69 |
+
fig, ax = plt.subplots()
|
| 70 |
+
ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct="%1.1f%%", startangle=90)
|
| 71 |
+
ax.axis("equal")
|
| 72 |
+
st.pyplot(fig)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
matplotlib
|