Pro-Coder commited on
Commit
ec05904
·
verified ·
1 Parent(s): dc5b022

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -52
app.py CHANGED
@@ -1,53 +1,47 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- from wordcloud import WordCloud
4
- import matplotlib.pyplot as plt
5
- import io
6
-
7
- # Load Hugging Face pipelines
8
- sentiment_model = pipeline("sentiment-analysis")
9
- summarizer = pipeline("summarization")
10
-
11
- # Function to generate word cloud
12
- def generate_wordcloud(text):
13
- wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
14
- img = io.BytesIO()
15
- plt.figure(figsize=(8, 4))
16
- plt.imshow(wordcloud, interpolation="bilinear")
17
- plt.axis("off")
18
- plt.savefig(img, format="png")
19
- plt.close()
20
- return img.getvalue()
21
-
22
- # Core function
23
- def analyze_text(user_input):
24
- # Sentiment
25
- sentiment = sentiment_model(user_input)[0]
26
-
27
- # Summary
28
- try:
29
- summary = summarizer(user_input, max_length=60, min_length=10, do_sample=False)[0]['summary_text']
30
- except Exception:
31
- summary = "Summary not available for very short text."
32
-
33
- # Wordcloud
34
- wc_img = generate_wordcloud(user_input)
35
-
36
- return f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}", summary, wc_img
37
-
38
-
39
- # Gradio Interface
40
- demo = gr.Interface(
41
- fn=analyze_text,
42
- inputs=gr.Textbox(lines=5, placeholder="Enter stakeholder comment here..."),
43
- outputs=[
44
- gr.Textbox(label="Sentiment"),
45
- gr.Textbox(label="Summary"),
46
- gr.Image(label="Word Cloud")
47
- ],
48
- title="E-Consultation Sentiment Analysis",
49
- description="Enter a comment/suggestion. The system predicts sentiment, generates a summary, and visualizes keywords."
50
- )
51
-
52
- if __name__ == "__main__":
53
- demo.launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ from pipeline import process_comment
4
+
5
+ def analyze_single(text):
6
+ sentiment, summary, wc_img = process_comment(text)
7
+ result = f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}"
8
+ return result, summary, wc_img
9
+
10
+ def analyze_batch(file):
11
+ df = pd.read_csv(file.name)
12
+ results = []
13
+ summaries = []
14
+ combined_text = ""
15
+
16
+ for comment in df["comment"]:
17
+ sentiment, summary, _ = process_comment(comment)
18
+ results.append(f"{sentiment['label']} ({sentiment['score']:.2f})")
19
+ summaries.append(summary)
20
+ combined_text += " " + comment
21
+
22
+ # Generate one big word cloud for all comments
23
+ _, _, wc_img = process_comment(combined_text)
24
+
25
+ df["Sentiment"] = results
26
+ df["Summary"] = summaries
27
+
28
+ return df, wc_img
29
+
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("# 🏛️ E-Consultation Sentiment Analysis\nAnalyze stakeholder comments with AI.")
32
+
33
+ with gr.Tab("Single Comment"):
34
+ inp = gr.Textbox(lines=5, placeholder="Enter stakeholder comment...")
35
+ out1 = gr.Textbox(label="Sentiment")
36
+ out2 = gr.Textbox(label="Summary")
37
+ out3 = gr.Image(label="Word Cloud")
38
+ btn = gr.Button("Analyze")
39
+ btn.click(analyze_single, inputs=inp, outputs=[out1, out2, out3])
40
+
41
+ with gr.Tab("Batch Upload (CSV)"):
42
+ file_in = gr.File(label="Upload CSV with 'comment' column", type="file")
43
+ df_out = gr.Dataframe(label="Analysis Results")
44
+ wc_out = gr.Image(label="Word Cloud")
45
+ file_in.change(analyze_batch, inputs=file_in, outputs=[df_out, wc_out])
46
+
47
+ demo.launch()