File size: 1,590 Bytes
9b3510a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import io

# Load Hugging Face pipelines
sentiment_model = pipeline("sentiment-analysis")
summarizer = pipeline("summarization")

# Function to generate word cloud
def generate_wordcloud(text):
    wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
    img = io.BytesIO()
    plt.figure(figsize=(8, 4))
    plt.imshow(wordcloud, interpolation="bilinear")
    plt.axis("off")
    plt.savefig(img, format="png")
    plt.close()
    return img.getvalue()

# Core function
def analyze_text(user_input):
    # Sentiment
    sentiment = sentiment_model(user_input)[0]

    # Summary
    try:
        summary = summarizer(user_input, max_length=60, min_length=10, do_sample=False)[0]['summary_text']
    except Exception:
        summary = "Summary not available for very short text."

    # Wordcloud
    wc_img = generate_wordcloud(user_input)

    return f"**Label:** {sentiment['label']} | **Score:** {sentiment['score']:.2f}", summary, wc_img


# Gradio Interface
demo = gr.Interface(
    fn=analyze_text,
    inputs=gr.Textbox(lines=5, placeholder="Enter stakeholder comment here..."),
    outputs=[
        gr.Textbox(label="Sentiment"),
        gr.Textbox(label="Summary"),
        gr.Image(label="Word Cloud")
    ],
    title="E-Consultation Sentiment Analysis",
    description="Enter a comment/suggestion. The system predicts sentiment, generates a summary, and visualizes keywords."
)

if __name__ == "__main__":
    demo.launch()