Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,90 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import streamlit as st
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
st.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
st.
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from wordcloud import WordCloud
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
# Load Hugging Face models
|
| 8 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 9 |
+
summarizer = pipeline("summarization")
|
| 10 |
+
|
| 11 |
+
st.set_page_config(page_title="Sentiment Analysis App", layout="wide")
|
| 12 |
+
|
| 13 |
+
st.title("π Sentiment Analysis of e-Consultation Comments")
|
| 14 |
+
st.write("Analyze single or multiple comments: sentiment, summary, and word cloud.")
|
| 15 |
+
|
| 16 |
+
# --- Sidebar mode selection ---
|
| 17 |
+
mode = st.sidebar.radio("Choose mode:", ["Single Comment", "Upload File"])
|
| 18 |
+
|
| 19 |
+
# --- Mode 1: Single Comment Analysis ---
|
| 20 |
+
if mode == "Single Comment":
|
| 21 |
+
user_input = st.text_area("Enter a comment:", height=150)
|
| 22 |
+
|
| 23 |
+
if st.button("Analyze Comment"):
|
| 24 |
+
if user_input.strip():
|
| 25 |
+
# Sentiment
|
| 26 |
+
sentiment = sentiment_analyzer(user_input)[0]
|
| 27 |
+
st.subheader("πΉ Sentiment Analysis")
|
| 28 |
+
st.write(f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}")
|
| 29 |
+
|
| 30 |
+
# Summarization (if long enough)
|
| 31 |
+
if len(user_input.split()) > 30:
|
| 32 |
+
summary = summarizer(user_input, max_length=50, min_length=20, do_sample=False)[0]['summary_text']
|
| 33 |
+
st.subheader("πΉ Summary")
|
| 34 |
+
st.write(summary)
|
| 35 |
+
else:
|
| 36 |
+
st.info("Not enough text for summarization (need > 30 words).")
|
| 37 |
+
|
| 38 |
+
# Word Cloud
|
| 39 |
+
st.subheader("πΉ Word Cloud")
|
| 40 |
+
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(user_input)
|
| 41 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
| 42 |
+
ax.imshow(wordcloud, interpolation="bilinear")
|
| 43 |
+
ax.axis("off")
|
| 44 |
+
st.pyplot(fig)
|
| 45 |
+
else:
|
| 46 |
+
st.warning("β οΈ Please enter a comment before analyzing.")
|
| 47 |
+
|
| 48 |
+
# --- Mode 2: Batch Analysis from File ---
|
| 49 |
+
else:
|
| 50 |
+
st.info("Upload a CSV or Excel file containing a column named **comment**.")
|
| 51 |
+
|
| 52 |
+
uploaded_file = st.file_uploader("Upload file", type=["csv", "xlsx"])
|
| 53 |
+
|
| 54 |
+
if uploaded_file:
|
| 55 |
+
# Read file
|
| 56 |
+
if uploaded_file.name.endswith(".csv"):
|
| 57 |
+
df = pd.read_csv(uploaded_file)
|
| 58 |
+
else:
|
| 59 |
+
df = pd.read_excel(uploaded_file)
|
| 60 |
+
|
| 61 |
+
if "comment" not in df.columns:
|
| 62 |
+
st.error("β File must contain a column named 'comment'.")
|
| 63 |
+
else:
|
| 64 |
+
st.write("### Uploaded Data", df.head())
|
| 65 |
+
|
| 66 |
+
if st.button("Analyze All Comments"):
|
| 67 |
+
sentiments = []
|
| 68 |
+
all_text = " "
|
| 69 |
+
|
| 70 |
+
for text in df["comment"].dropna():
|
| 71 |
+
result = sentiment_analyzer(str(text))[0]
|
| 72 |
+
sentiments.append(result["label"])
|
| 73 |
+
all_text += " " + str(text)
|
| 74 |
+
|
| 75 |
+
df["sentiment"] = sentiments
|
| 76 |
+
|
| 77 |
+
st.subheader("πΉ Sentiment Results")
|
| 78 |
+
st.write(df)
|
| 79 |
+
|
| 80 |
+
# Overall Word Cloud
|
| 81 |
+
st.subheader("πΉ Word Cloud (All Comments)")
|
| 82 |
+
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(all_text)
|
| 83 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
| 84 |
+
ax.imshow(wordcloud, interpolation="bilinear")
|
| 85 |
+
ax.axis("off")
|
| 86 |
+
st.pyplot(fig)
|
| 87 |
+
|
| 88 |
+
# Sentiment Distribution
|
| 89 |
+
st.subheader("πΉ Sentiment Distribution")
|
| 90 |
+
st.bar_chart(df["sentiment"].value_counts())
|