Update app.py
Browse files
app.py
CHANGED
@@ -1,144 +1,145 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
import re
|
3 |
-
import tempfile
|
4 |
-
import gradio as gr
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
import seaborn as sns
|
7 |
-
from wordcloud import WordCloud
|
8 |
-
from flair.models import TextClassifier
|
9 |
-
from flair.data import Sentence
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
text = re.sub(r"
|
20 |
-
text = re.sub(r"
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
plt.
|
37 |
-
plt.
|
38 |
-
plt.
|
39 |
-
plt.
|
40 |
-
plt.
|
41 |
-
plt.
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
plt.
|
46 |
-
plt.
|
47 |
-
plt.
|
48 |
-
plt.
|
49 |
-
plt.
|
50 |
-
plt.
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
df["
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
plt.
|
90 |
-
|
91 |
-
|
92 |
-
plt.
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
plt.
|
99 |
-
|
100 |
-
|
101 |
-
plt.
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
gr.Markdown("
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import re
|
3 |
+
import tempfile
|
4 |
+
import gradio as gr
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import seaborn as sns
|
7 |
+
from wordcloud import WordCloud
|
8 |
+
from flair.models import TextClassifier
|
9 |
+
from flair.data import Sentence
|
10 |
+
from flair.nn import Classifier
|
11 |
+
|
12 |
+
# Load FLAIR sentiment model
|
13 |
+
classifier = Classifier.load("sentiment")
|
14 |
+
|
15 |
+
# Minimal text cleaning
|
16 |
+
def clean_text_for_flair(text):
|
17 |
+
if pd.isnull(text):
|
18 |
+
return ""
|
19 |
+
text = re.sub(r"http\S+|www\S+", "", text) # remove URLs
|
20 |
+
text = re.sub(r"<.*?>", "", text) # remove HTML
|
21 |
+
text = re.sub(r"\s+", " ", text).strip() # normalize whitespace
|
22 |
+
return text
|
23 |
+
|
24 |
+
# Generate word clouds
|
25 |
+
def generate_wordclouds(df):
|
26 |
+
if "sentiment" not in df.columns or "clean_text" not in df.columns:
|
27 |
+
return None, None
|
28 |
+
|
29 |
+
positive_text = " ".join(df[df["sentiment"] == "POSITIVE"]["clean_text"].astype(str))
|
30 |
+
negative_text = " ".join(df[df["sentiment"] == "NEGATIVE"]["clean_text"].astype(str))
|
31 |
+
|
32 |
+
pos_wordcloud = WordCloud(width=800, height=400, background_color='white', colormap='Greens').generate(positive_text)
|
33 |
+
neg_wordcloud = WordCloud(width=800, height=400, background_color='white', colormap='Reds').generate(negative_text)
|
34 |
+
|
35 |
+
pos_path = "positive_wordcloud.png"
|
36 |
+
plt.figure(figsize=(10, 5))
|
37 |
+
plt.imshow(pos_wordcloud, interpolation='bilinear')
|
38 |
+
plt.axis("off")
|
39 |
+
plt.title("Positive Word Cloud")
|
40 |
+
plt.tight_layout()
|
41 |
+
plt.savefig(pos_path)
|
42 |
+
plt.close()
|
43 |
+
|
44 |
+
neg_path = "negative_wordcloud.png"
|
45 |
+
plt.figure(figsize=(10, 5))
|
46 |
+
plt.imshow(neg_wordcloud, interpolation='bilinear')
|
47 |
+
plt.axis("off")
|
48 |
+
plt.title("Negative Word Cloud")
|
49 |
+
plt.tight_layout()
|
50 |
+
plt.savefig(neg_path)
|
51 |
+
plt.close()
|
52 |
+
|
53 |
+
return pos_path, neg_path
|
54 |
+
|
55 |
+
# Main analysis function
|
56 |
+
def analyze_sentiment_flair(file, text_column):
|
57 |
+
try:
|
58 |
+
df = pd.read_csv(file.name)
|
59 |
+
except Exception as e:
|
60 |
+
return f"Error loading file: {e}", None, None, None, None, None
|
61 |
+
|
62 |
+
if text_column not in df.columns:
|
63 |
+
return "Selected text column not found.", None, None, None, None, None
|
64 |
+
|
65 |
+
df["clean_text"] = df[text_column].apply(clean_text_for_flair)
|
66 |
+
|
67 |
+
sentiments = []
|
68 |
+
scores = []
|
69 |
+
|
70 |
+
for text in df["clean_text"]:
|
71 |
+
sentence = Sentence(text)
|
72 |
+
classifier.predict(sentence)
|
73 |
+
label = sentence.labels[0].value
|
74 |
+
score = sentence.labels[0].score
|
75 |
+
sentiments.append(label)
|
76 |
+
scores.append(score)
|
77 |
+
|
78 |
+
df["sentiment"] = sentiments
|
79 |
+
df["confidence"] = scores
|
80 |
+
|
81 |
+
# Save results
|
82 |
+
with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=".csv") as tmp:
|
83 |
+
df.to_csv(tmp.name, index=False)
|
84 |
+
csv_path = tmp.name
|
85 |
+
|
86 |
+
# Sentiment count plot
|
87 |
+
plt.figure(figsize=(6, 4))
|
88 |
+
sns.countplot(data=df, x="sentiment", palette="pastel")
|
89 |
+
plt.title("Sentiment Distribution")
|
90 |
+
plt.tight_layout()
|
91 |
+
sentiment_plot_path = "sentiment_flair_plot.png"
|
92 |
+
plt.savefig(sentiment_plot_path)
|
93 |
+
plt.close()
|
94 |
+
|
95 |
+
# Confidence score distribution
|
96 |
+
plt.figure(figsize=(6, 4))
|
97 |
+
sns.histplot(df["confidence"], bins=30, kde=True, color="lightblue")
|
98 |
+
plt.title("Confidence Score Distribution")
|
99 |
+
plt.tight_layout()
|
100 |
+
confidence_plot_path = "confidence_flair_plot.png"
|
101 |
+
plt.savefig(confidence_plot_path)
|
102 |
+
plt.close()
|
103 |
+
|
104 |
+
# Word clouds
|
105 |
+
pos_wc_path, neg_wc_path = generate_wordclouds(df)
|
106 |
+
|
107 |
+
return f"Sentiment analysis completed on {len(df)} rows.", csv_path, sentiment_plot_path, confidence_plot_path, pos_wc_path, neg_wc_path
|
108 |
+
|
109 |
+
# Gradio interface
|
110 |
+
with gr.Blocks() as app:
|
111 |
+
gr.Markdown("## FLAIR-Based Sentiment Analyzer with Word Clouds")
|
112 |
+
gr.Markdown("Upload a CSV file with text data. This tool uses [FLAIR](https://github.com/flairNLP/flair) for sentiment classification (POSITIVE / NEGATIVE), shows confidence scores, and generates word clouds for each sentiment.")
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
file_input = gr.File(label="Upload CSV", file_types=[".csv"])
|
116 |
+
col_dropdown = gr.Dropdown(label="Select Text Column", choices=[], interactive=True)
|
117 |
+
|
118 |
+
def get_text_columns(file):
|
119 |
+
try:
|
120 |
+
df = pd.read_csv(file.name)
|
121 |
+
text_cols = df.select_dtypes(include='object').columns.tolist()
|
122 |
+
if not text_cols:
|
123 |
+
return gr.update(choices=[], label="⚠️ No text columns found")
|
124 |
+
return gr.update(choices=text_cols, value=text_cols[0])
|
125 |
+
except:
|
126 |
+
return gr.update(choices=[], value=None)
|
127 |
+
|
128 |
+
file_input.change(get_text_columns, inputs=file_input, outputs=col_dropdown)
|
129 |
+
|
130 |
+
analyze_btn = gr.Button("Run Sentiment Analysis")
|
131 |
+
|
132 |
+
output_text = gr.Textbox(label="Status")
|
133 |
+
file_output = gr.File(label="Download Results CSV")
|
134 |
+
sentiment_plot = gr.Image(label="Sentiment Distribution")
|
135 |
+
confidence_plot = gr.Image(label="Confidence Score Distribution")
|
136 |
+
wordcloud_pos = gr.Image(label="Positive Word Cloud")
|
137 |
+
wordcloud_neg = gr.Image(label="Negative Word Cloud")
|
138 |
+
|
139 |
+
analyze_btn.click(
|
140 |
+
analyze_sentiment_flair,
|
141 |
+
inputs=[file_input, col_dropdown],
|
142 |
+
outputs=[output_text, file_output, sentiment_plot, confidence_plot, wordcloud_pos, wordcloud_neg]
|
143 |
+
)
|
144 |
+
|
145 |
+
app.launch(share=True, debug=True)
|