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()