Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,57 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
import
|
| 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 |
-
combined_text += " " + comment
|
| 59 |
-
|
| 60 |
-
# One word cloud for all comments
|
| 61 |
-
wc_img = generate_wordcloud(combined_text)
|
| 62 |
-
|
| 63 |
-
df["Sentiment"] = results
|
| 64 |
-
df["Summary"] = summaries
|
| 65 |
-
return df, wc_img
|
| 66 |
-
|
| 67 |
-
# Gradio UI
|
| 68 |
-
with gr.Blocks() as demo:
|
| 69 |
-
gr.Markdown("## π E-Consultation Sentiment Analysis")
|
| 70 |
-
|
| 71 |
-
with gr.Tab("Single Comment"):
|
| 72 |
-
inp = gr.Textbox(label="Enter Comment")
|
| 73 |
-
out1 = gr.Textbox(label="Sentiment")
|
| 74 |
-
out2 = gr.Textbox(label="Summary")
|
| 75 |
-
out3 = gr.Image(label="Word Cloud")
|
| 76 |
-
inp.submit(process_comment, inp, [out1, out2, out3])
|
| 77 |
-
|
| 78 |
-
with gr.Tab("Batch Upload"):
|
| 79 |
-
file_in = gr.File(label="Upload CSV with 'comment' column", type="filepath")
|
| 80 |
-
out_df = gr.Dataframe(label="Results")
|
| 81 |
-
out_wc = gr.Image(label="Word Cloud (All Comments)")
|
| 82 |
-
file_in.change(analyze_batch, file_in, [out_df, out_wc])
|
| 83 |
-
|
| 84 |
-
if __name__ == "__main__":
|
| 85 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from modules.data_loader import load_texts
|
| 5 |
+
from modules.preprocessing import preprocess_texts
|
| 6 |
+
from modules.sentiment_analysis import analyze_sentiment
|
| 7 |
+
from modules.summarizer import summarize_texts
|
| 8 |
+
from modules.visualization import generate_wordcloud, plot_sentiment_distribution
|
| 9 |
+
from modules.utils import save_results_to_csv, display_dataframe
|
| 10 |
+
from config.settings import DATA_PATH
|
| 11 |
+
|
| 12 |
+
# Title
|
| 13 |
+
st.title("π Sentiment Analysis of E-Consultation Comments")
|
| 14 |
+
st.write("AI-powered tool for sentiment analysis, summarization, and visualization.")
|
| 15 |
+
|
| 16 |
+
# File uploader
|
| 17 |
+
uploaded_file = st.file_uploader("Upload a .txt file (one comment per line)", type=["txt"])
|
| 18 |
+
|
| 19 |
+
if uploaded_file:
|
| 20 |
+
# Save uploaded file
|
| 21 |
+
with open(DATA_PATH, "wb") as f:
|
| 22 |
+
f.write(uploaded_file.read())
|
| 23 |
+
st.success("β
File uploaded successfully!")
|
| 24 |
+
|
| 25 |
+
# Load data
|
| 26 |
+
texts = load_texts(DATA_PATH)
|
| 27 |
+
st.write(f"Loaded **{len(texts)}** comments.")
|
| 28 |
+
|
| 29 |
+
# Preprocess
|
| 30 |
+
clean_texts = preprocess_texts(texts)
|
| 31 |
+
|
| 32 |
+
# Sentiment Analysis
|
| 33 |
+
st.subheader("π Sentiment Analysis")
|
| 34 |
+
sentiment_results = analyze_sentiment(clean_texts)
|
| 35 |
+
sentiment_chart = plot_sentiment_distribution(sentiment_results)
|
| 36 |
+
st.image(sentiment_chart, caption="Sentiment Distribution")
|
| 37 |
+
|
| 38 |
+
# Summarization
|
| 39 |
+
st.subheader("π Summarization")
|
| 40 |
+
summaries = summarize_texts(clean_texts)
|
| 41 |
+
st.write("Here are some summaries:")
|
| 42 |
+
for s in summaries[:5]:
|
| 43 |
+
st.write(f"- {s['summary']}")
|
| 44 |
+
|
| 45 |
+
# Word Cloud
|
| 46 |
+
st.subheader("β Word Cloud")
|
| 47 |
+
wc_image = generate_wordcloud(clean_texts)
|
| 48 |
+
st.image(wc_image, caption="Word Cloud of Comments")
|
| 49 |
+
|
| 50 |
+
# Results Table
|
| 51 |
+
st.subheader("π Results Table")
|
| 52 |
+
df = display_dataframe(sentiment_results, summaries)
|
| 53 |
+
st.dataframe(df)
|
| 54 |
+
|
| 55 |
+
# Save results
|
| 56 |
+
results_path = save_results_to_csv(sentiment_results, summaries)
|
| 57 |
+
st.download_button("π₯ Download Results CSV", data=open(results_path, "rb"), file_name="results.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|