File size: 1,629 Bytes
9b3510a
ec05904
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()