Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|