zaid002 commited on
Commit
8af3937
Β·
verified Β·
1 Parent(s): eb8d2d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -35
app.py CHANGED
@@ -56,36 +56,64 @@ class SentimentAnalyzerApp:
56
 
57
  def load_sample_data(self):
58
  """Create sample data for demo purposes"""
59
- sample_data = {
60
- 'date': ['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04', '2024-01-05'],
61
- 'review': [
62
- 'This app is absolutely amazing and very helpful!',
63
- 'The application works okay but could be better.',
64
- 'I am very disappointed with the performance.',
65
- 'Excellent features and great user interface.',
66
- 'Not what I expected, needs improvement.'
67
- ],
68
- 'rating': [5, 3, 1, 5, 2],
69
- 'platform': ['Web', 'Mobile', 'Web', 'Mobile', 'Web'],
70
- 'language': ['en', 'en', 'en', 'en', 'en'],
71
- 'location': ['USA', 'UK', 'Canada', 'Australia', 'India'],
72
- 'verified_purchase': ['Yes', 'No', 'Yes', 'Yes', 'No'],
73
- 'helpful_votes': [10, 2, 5, 8, 1]
74
- }
75
- self.df = pd.DataFrame(sample_data)
76
- self.df['date'] = pd.to_datetime(self.df['date'])
77
-
78
- # Create sentiment labels
79
- def get_sentiment(rating):
80
- if rating >= 4:
81
- return 'Positive'
82
- elif rating == 3:
83
- return 'Neutral'
84
- else:
85
- return 'Negative'
86
-
87
- self.df['sentiment'] = self.df['rating'].apply(get_sentiment)
88
- return True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  def load_model(self):
91
  """Try to load model, but use simulated predictions if not available"""
@@ -104,6 +132,14 @@ class SentimentAnalyzerApp:
104
  st.warning(f"Model loading failed: {e}. Using simulated mode.")
105
  return False
106
 
 
 
 
 
 
 
 
 
107
  def predict_sentiment(self, text):
108
  """Predict sentiment for new text"""
109
  if self.model is not None and self.vectorizer is not None:
@@ -174,9 +210,7 @@ class SentimentAnalyzerApp:
174
  st.markdown("### Customer Review Sentiment Analysis Dashboard")
175
 
176
  # Initialize and load data
177
- if 'data_loaded' not in st.session_state:
178
- self.load_sample_data()
179
- st.session_state.data_loaded = True
180
 
181
  if 'model_loaded' not in st.session_state:
182
  st.session_state.model_loaded = self.load_model()
@@ -202,6 +236,9 @@ class SentimentAnalyzerApp:
202
  """Overview page"""
203
  st.header("πŸ“Š Project Overview")
204
 
 
 
 
205
  # Key metrics
206
  col1, col2, col3, col4 = st.columns(4)
207
 
@@ -241,7 +278,11 @@ class SentimentAnalyzerApp:
241
  title='Sentiment Distribution')
242
  st.plotly_chart(fig, use_container_width=True)
243
 
244
- st.info("πŸ’‘ This is a demo with sample data. Upload your dataset to the 'data' folder for real analysis.")
 
 
 
 
245
 
246
  def show_model_demo(self):
247
  """Interactive model demo"""
@@ -307,13 +348,19 @@ class SentimentAnalyzerApp:
307
  for i, example in enumerate(examples):
308
  with cols[i % 3]:
309
  if st.button(f"'{example[:30]}...'", use_container_width=True):
310
- st.session_state.demo_text = example
311
  st.rerun()
312
 
313
  def show_analysis(self):
314
  """Analysis page"""
315
  st.header("πŸ“ˆ Data Analysis")
316
 
 
 
 
 
 
 
 
317
  # Platform analysis
318
  st.subheader("Platform Comparison")
319
  platform_counts = self.df['platform'].value_counts()
@@ -362,6 +409,13 @@ class SentimentAnalyzerApp:
362
  """Insights page"""
363
  st.header("πŸ’‘ Business Insights & Recommendations")
364
 
 
 
 
 
 
 
 
365
  # Key metrics
366
  positive_pct = (self.df['sentiment'] == 'Positive').mean() * 100
367
  avg_rating = self.df['rating'].mean()
 
56
 
57
  def load_sample_data(self):
58
  """Create sample data for demo purposes"""
59
+ try:
60
+ sample_data = {
61
+ 'date': ['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04', '2024-01-05'],
62
+ 'review': [
63
+ 'This app is absolutely amazing and very helpful!',
64
+ 'The application works okay but could be better.',
65
+ 'I am very disappointed with the performance.',
66
+ 'Excellent features and great user interface.',
67
+ 'Not what I expected, needs improvement.'
68
+ ],
69
+ 'rating': [5, 3, 1, 5, 2],
70
+ 'platform': ['Web', 'Mobile', 'Web', 'Mobile', 'Web'],
71
+ 'language': ['en', 'en', 'en', 'en', 'en'],
72
+ 'location': ['USA', 'UK', 'Canada', 'Australia', 'India'],
73
+ 'verified_purchase': ['Yes', 'No', 'Yes', 'Yes', 'No'],
74
+ 'helpful_votes': [10, 2, 5, 8, 1]
75
+ }
76
+ self.df = pd.DataFrame(sample_data)
77
+ self.df['date'] = pd.to_datetime(self.df['date'])
78
+
79
+ # Create sentiment labels
80
+ def get_sentiment(rating):
81
+ if rating >= 4:
82
+ return 'Positive'
83
+ elif rating == 3:
84
+ return 'Neutral'
85
+ else:
86
+ return 'Negative'
87
+
88
+ self.df['sentiment'] = self.df['rating'].apply(get_sentiment)
89
+ return True
90
+ except Exception as e:
91
+ st.error(f"Error creating sample data: {e}")
92
+ return False
93
+
94
+ def load_real_data(self):
95
+ """Try to load real data from file"""
96
+ try:
97
+ data_path = 'data/chatgpt_style_reviews_dataset.csv'
98
+ if os.path.exists(data_path):
99
+ self.df = pd.read_csv(data_path)
100
+ self.df['date'] = pd.to_datetime(self.df['date'], errors='coerce')
101
+
102
+ # Create sentiment labels
103
+ def get_sentiment(rating):
104
+ if rating >= 4:
105
+ return 'Positive'
106
+ elif rating == 3:
107
+ return 'Neutral'
108
+ else:
109
+ return 'Negative'
110
+
111
+ self.df['sentiment'] = self.df['rating'].apply(get_sentiment)
112
+ return True
113
+ return False
114
+ except Exception as e:
115
+ st.error(f"Error loading real data: {e}")
116
+ return False
117
 
118
  def load_model(self):
119
  """Try to load model, but use simulated predictions if not available"""
 
132
  st.warning(f"Model loading failed: {e}. Using simulated mode.")
133
  return False
134
 
135
+ def ensure_data_loaded(self):
136
+ """Ensure data is loaded, use sample if real data not available"""
137
+ if self.df is None:
138
+ # First try to load real data
139
+ if not self.load_real_data():
140
+ # If real data fails, load sample data
141
+ self.load_sample_data()
142
+
143
  def predict_sentiment(self, text):
144
  """Predict sentiment for new text"""
145
  if self.model is not None and self.vectorizer is not None:
 
210
  st.markdown("### Customer Review Sentiment Analysis Dashboard")
211
 
212
  # Initialize and load data
213
+ self.ensure_data_loaded()
 
 
214
 
215
  if 'model_loaded' not in st.session_state:
216
  st.session_state.model_loaded = self.load_model()
 
236
  """Overview page"""
237
  st.header("πŸ“Š Project Overview")
238
 
239
+ # Ensure data is loaded
240
+ self.ensure_data_loaded()
241
+
242
  # Key metrics
243
  col1, col2, col3, col4 = st.columns(4)
244
 
 
278
  title='Sentiment Distribution')
279
  st.plotly_chart(fig, use_container_width=True)
280
 
281
+ # Show data source info
282
+ if hasattr(self, 'using_real_data') and self.using_real_data:
283
+ st.success("βœ… Using real dataset from file")
284
+ else:
285
+ st.info("πŸ’‘ Using sample data for demo. Upload your dataset to the 'data' folder for real analysis.")
286
 
287
  def show_model_demo(self):
288
  """Interactive model demo"""
 
348
  for i, example in enumerate(examples):
349
  with cols[i % 3]:
350
  if st.button(f"'{example[:30]}...'", use_container_width=True):
 
351
  st.rerun()
352
 
353
  def show_analysis(self):
354
  """Analysis page"""
355
  st.header("πŸ“ˆ Data Analysis")
356
 
357
+ # Ensure data is loaded
358
+ self.ensure_data_loaded()
359
+
360
+ if self.df is None:
361
+ st.error("No data available for analysis.")
362
+ return
363
+
364
  # Platform analysis
365
  st.subheader("Platform Comparison")
366
  platform_counts = self.df['platform'].value_counts()
 
409
  """Insights page"""
410
  st.header("πŸ’‘ Business Insights & Recommendations")
411
 
412
+ # Ensure data is loaded
413
+ self.ensure_data_loaded()
414
+
415
+ if self.df is None:
416
+ st.error("No data available for insights.")
417
+ return
418
+
419
  # Key metrics
420
  positive_pct = (self.df['sentiment'] == 'Positive').mean() * 100
421
  avg_rating = self.df['rating'].mean()