Ginidu2003 commited on
Commit
a480e6b
·
verified ·
1 Parent(s): 80f7470

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -5
app.py CHANGED
@@ -8,6 +8,7 @@ from nltk.corpus import stopwords
8
  from nltk.stem import WordNetLemmatizer
9
  import re
10
  import string
 
11
 
12
  # ====================== NLTK SETUP ======================
13
 
@@ -65,9 +66,41 @@ def classify_csv(file):
65
  category_counts = df['class'].value_counts().reset_index()
66
  category_counts.columns = ["Category", "Count"]
67
 
68
- return f"✅ Success! Classified {len(df)} rows", output_file, category_counts
 
 
 
69
  except Exception as e:
70
  return f"❌ Error: {str(e)}", None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
 
73
 
@@ -113,10 +146,9 @@ with gr.Blocks(title=" News Classifier & Question Answering App") as demo:
113
  classify_btn = gr.Button("🚀 Classify News", variant="primary")
114
  output_text = gr.Textbox(label="Status")
115
  output_file = gr.File(label="Download output.csv")
116
- bar_chart = gr.BarPlot(
117
- label="Category Distribution Across 5 Classes",
118
- x="Category",
119
- y="Count",
120
 
121
  )
122
 
 
8
  from nltk.stem import WordNetLemmatizer
9
  import re
10
  import string
11
+ import matplotlib.pyplot as plt
12
 
13
  # ====================== NLTK SETUP ======================
14
 
 
66
  category_counts = df['class'].value_counts().reset_index()
67
  category_counts.columns = ["Category", "Count"]
68
 
69
+ # Create colored bar chart
70
+ fig = create_colored_bar_chart(category_counts)
71
+
72
+ return f"✅ Success! Classified {len(df)} rows", output_file, fig
73
  except Exception as e:
74
  return f"❌ Error: {str(e)}", None, None
75
+
76
+ # ====================== COLORED BAR CHART ======================
77
+ def create_colored_bar_chart(category_counts):
78
+ if category_counts is None or len(category_counts) == 0:
79
+ fig, ax = plt.subplots()
80
+ ax.text(0.5, 0.5, "No data available", ha='center', va='center')
81
+ return fig
82
+
83
+ categories = category_counts["Category"]
84
+ counts = category_counts["Count"]
85
+
86
+ # Different attractive colors for each category
87
+ colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEEAD']
88
+
89
+ fig, ax = plt.subplots(figsize=(10, 6))
90
+ bars = ax.bar(categories, counts, color=colors)
91
+
92
+ # Add count numbers on top of bars
93
+ for bar in bars:
94
+ height = bar.get_height()
95
+ ax.text(bar.get_x() + bar.get_width()/2, height + 0.5,
96
+ str(int(height)), ha='center', va='bottom', fontsize=12, fontweight='bold')
97
+
98
+ ax.set_title("Category Distribution Across 5 Classes", fontsize=14, fontweight='bold')
99
+ ax.set_xlabel("Category")
100
+ ax.set_ylabel("Count")
101
+ plt.xticks(rotation=15)
102
+ plt.tight_layout()
103
+ return fig
104
 
105
 
106
 
 
146
  classify_btn = gr.Button("🚀 Classify News", variant="primary")
147
  output_text = gr.Textbox(label="Status")
148
  output_file = gr.File(label="Download output.csv")
149
+ bar_chart = gr.Plot(
150
+ label="Category Distribution Across 5 Classes"
151
+
 
152
 
153
  )
154