sms
Browse files- Dockerfile +29 -0
- app.py +52 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Create a writable cache directory for Hugging Face
|
| 12 |
+
ENV HF_HOME=/app/hf_cache
|
| 13 |
+
RUN mkdir -p /app/hf_cache
|
| 14 |
+
|
| 15 |
+
# Set permissions for the cache directory
|
| 16 |
+
RUN chmod -R 777 /app/hf_cache
|
| 17 |
+
|
| 18 |
+
# Install dependencies
|
| 19 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 20 |
+
|
| 21 |
+
# Create a non-root user and switch to it
|
| 22 |
+
RUN useradd -m appuser
|
| 23 |
+
USER appuser
|
| 24 |
+
|
| 25 |
+
# Expose the Streamlit port
|
| 26 |
+
EXPOSE 7860
|
| 27 |
+
|
| 28 |
+
# Run the Streamlit app
|
| 29 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
model_name = "tabularisai/multilingual-sentiment-analysis"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
def predict_sentiment(texts):
|
| 13 |
+
inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 17 |
+
sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
|
| 18 |
+
return [sentiment_map[p] for p in torch.argmax(probabilities, dim=-1).tolist()]
|
| 19 |
+
|
| 20 |
+
# Streamlit UI
|
| 21 |
+
st.title("Sentiment Analysis App")
|
| 22 |
+
st.write("Upload an Excel file containing text data, and we'll analyze its sentiment.")
|
| 23 |
+
|
| 24 |
+
uploaded_file = st.file_uploader("Upload Excel File", type=["xlsx", "xls"])
|
| 25 |
+
|
| 26 |
+
if uploaded_file is not None:
|
| 27 |
+
df = pd.read_excel(uploaded_file)
|
| 28 |
+
st.write("Preview of Uploaded Data:")
|
| 29 |
+
st.dataframe(df.head())
|
| 30 |
+
|
| 31 |
+
text_column = st.selectbox("Select the column containing text", df.columns)
|
| 32 |
+
|
| 33 |
+
if st.button("Analyze Sentiment"):
|
| 34 |
+
df["Sentiment"] = predict_sentiment(df[text_column].astype(str).tolist())
|
| 35 |
+
|
| 36 |
+
# Display results
|
| 37 |
+
st.write("Sentiment Analysis Results:")
|
| 38 |
+
st.dataframe(df[[text_column, "Sentiment"]])
|
| 39 |
+
|
| 40 |
+
# Pie chart
|
| 41 |
+
sentiment_counts = df["Sentiment"].value_counts()
|
| 42 |
+
fig, ax = plt.subplots()
|
| 43 |
+
ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', colors=["red", "yellow", "pink", "lightgreen", "green"])
|
| 44 |
+
ax.set_title("Sentiment Distribution")
|
| 45 |
+
st.pyplot(fig)
|
| 46 |
+
|
| 47 |
+
# Table display for sentiment analysis results
|
| 48 |
+
st.write("Detailed Sentiment Table:")
|
| 49 |
+
st.table(df[[text_column, "Sentiment"]])
|
| 50 |
+
|
| 51 |
+
# Download option
|
| 52 |
+
st.download_button("Download Results", df.to_csv(index=False).encode('utf-8'), "sentiment_results.csv", "text/csv")
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
matplotlib
|