Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import pandas as pd | |
| from pipeline import process_comment | |
| def analyze_single(text): | |
| sentiment, summary, wc_img = process_comment(text) | |
| result = f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}" | |
| return result, summary, wc_img | |
| def analyze_batch(file): | |
| df = pd.read_csv(file.name) | |
| results = [] | |
| summaries = [] | |
| combined_text = "" | |
| for comment in df["comment"]: | |
| sentiment, summary, _ = process_comment(comment) | |
| results.append(f"{sentiment['label']} ({sentiment['score']:.2f})") | |
| summaries.append(summary) | |
| combined_text += " " + comment | |
| # Generate one big word cloud for all comments | |
| _, _, wc_img = process_comment(combined_text) | |
| df["Sentiment"] = results | |
| df["Summary"] = summaries | |
| return df, wc_img | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🏛️ E-Consultation Sentiment Analysis\nAnalyze stakeholder comments with AI.") | |
| with gr.Tab("Single Comment"): | |
| inp = gr.Textbox(lines=5, placeholder="Enter stakeholder comment...") | |
| out1 = gr.Textbox(label="Sentiment") | |
| out2 = gr.Textbox(label="Summary") | |
| out3 = gr.Image(label="Word Cloud") | |
| btn = gr.Button("Analyze") | |
| btn.click(analyze_single, inputs=inp, outputs=[out1, out2, out3]) | |
| with gr.Tab("Batch Upload (CSV)"): | |
| file_in = gr.File(label="Upload CSV with 'comment' column", type="file") | |
| df_out = gr.Dataframe(label="Analysis Results") | |
| wc_out = gr.Image(label="Word Cloud") | |
| file_in.change(analyze_batch, inputs=file_in, outputs=[df_out, wc_out]) | |
| demo.launch() | |