Spaces:
Runtime error
Runtime error
File size: 3,427 Bytes
4aaebc3 5935e64 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import streamlit as st
import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from transformers import pipeline
# Load Hugging Face models
sentiment_analyzer = pipeline("sentiment-analysis")
summarizer = pipeline("summarization")
st.set_page_config(page_title="Sentiment Analysis App", layout="wide")
st.title("๐ Sentiment Analysis of e-Consultation Comments")
st.write("Analyze single or multiple comments: sentiment, summary, and word cloud.")
# --- Sidebar mode selection ---
mode = st.sidebar.radio("Choose mode:", ["Single Comment", "Upload File"])
# --- Mode 1: Single Comment Analysis ---
if mode == "Single Comment":
user_input = st.text_area("Enter a comment:", height=150)
if st.button("Analyze Comment"):
if user_input.strip():
# Sentiment
sentiment = sentiment_analyzer(user_input)[0]
st.subheader("๐น Sentiment Analysis")
st.write(f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}")
# Summarization (if long enough)
if len(user_input.split()) > 30:
summary = summarizer(user_input, max_length=50, min_length=20, do_sample=False)[0]['summary_text']
st.subheader("๐น Summary")
st.write(summary)
else:
st.info("Not enough text for summarization (need > 30 words).")
# Word Cloud
st.subheader("๐น Word Cloud")
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(user_input)
fig, ax = plt.subplots(figsize=(10, 5))
ax.imshow(wordcloud, interpolation="bilinear")
ax.axis("off")
st.pyplot(fig)
else:
st.warning("โ ๏ธ Please enter a comment before analyzing.")
# --- Mode 2: Batch Analysis from File ---
else:
st.info("Upload a CSV or Excel file containing a column named **comment**.")
uploaded_file = st.file_uploader("Upload file", type=["csv", "xlsx"])
if uploaded_file:
# Read file
if uploaded_file.name.endswith(".csv"):
df = pd.read_csv(uploaded_file)
else:
df = pd.read_excel(uploaded_file)
if "comment" not in df.columns:
st.error("โ File must contain a column named 'comment'.")
else:
st.write("### Uploaded Data", df.head())
if st.button("Analyze All Comments"):
sentiments = []
all_text = " "
for text in df["comment"].dropna():
result = sentiment_analyzer(str(text))[0]
sentiments.append(result["label"])
all_text += " " + str(text)
df["sentiment"] = sentiments
st.subheader("๐น Sentiment Results")
st.write(df)
# Overall Word Cloud
st.subheader("๐น Word Cloud (All Comments)")
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(all_text)
fig, ax = plt.subplots(figsize=(10, 5))
ax.imshow(wordcloud, interpolation="bilinear")
ax.axis("off")
st.pyplot(fig)
# Sentiment Distribution
st.subheader("๐น Sentiment Distribution")
st.bar_chart(df["sentiment"].value_counts())
|