rairo commited on
Commit
76fb0de
·
verified ·
1 Parent(s): 3dc597e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -44
app.py CHANGED
@@ -141,28 +141,26 @@ if tabs =='Chat':
141
  answer = generateResponse(dataFrame=df,prompt=user_input)
142
  st.write(answer)
143
 
144
- elif tabs == 'Reports':
145
  df = pd.read_csv(uploaded_file)
146
 
147
  st.subheader("Reports")
148
- st.write("Filter by Company Name, Product, Gender, Youth, Intervention Category or Intervention to generate report")
149
 
150
  # Filtering Interface
151
  st.write("Filtering Options")
152
- branch_names = df['Company Name'].unique().tolist() # Company Name instead of Branch Name
153
  gender_options = df['Gender'].unique().tolist()
154
  youth_options = df['Youth'].unique().tolist()
155
  intervention_categories = df['Intervention_Category'].unique().tolist()
156
  intervention_types = df['Intervention'].unique().tolist()
157
 
158
-
159
- selected_companies = st.multiselect('Select Company(ies)', branch_names, default=branch_names)
160
  selected_genders = st.multiselect('Select Gender(s)', gender_options, default=gender_options)
161
  selected_youth = st.multiselect('Select Youth Status(es)', youth_options, default=youth_options)
162
  selected_categories = st.multiselect('Select Intervention Category(ies)', intervention_categories, default=intervention_categories)
163
  selected_interventions = st.multiselect('Select Intervention(s)', intervention_types, default=intervention_types)
164
 
165
-
166
  if st.button('Apply Filters and Generate report'):
167
  filtered_df = df.copy()
168
 
@@ -177,46 +175,59 @@ elif tabs == 'Reports':
177
  if selected_interventions:
178
  filtered_df = filtered_df[filtered_df['Intervention'].isin(selected_interventions)]
179
 
180
-
181
- st.write("Filtered DataFrame")
182
- with st.expander("Preview"):
183
- st.write(filtered_df.head())
184
-
185
- # Dashboard Visualizations (Plotly)
186
- st.subheader("Dashboard")
187
-
188
- col1, col2 = st.columns(2) # Create two columns for better layout
189
-
190
- with col1:
191
- fig_gender = px.histogram(filtered_df, x="Gender", title="Gender Distribution")
192
- st.plotly_chart(fig_gender)
193
-
194
- fig_youth = px.histogram(filtered_df, x="Youth", title="Youth Participation")
195
- st.plotly_chart(fig_youth)
196
-
197
- with col2:
198
- fig_category = px.histogram(filtered_df, x="Intervention_Category", title="Intervention Category Distribution")
199
- st.plotly_chart(fig_category)
200
-
201
- fig_intervention = px.histogram(filtered_df, x="Intervention", title="Intervention Type Distribution")
202
- st.plotly_chart(fig_intervention)
203
-
204
- with st.spinner("Generating Report, Please Wait...."):
205
- prompt = f"""
206
- You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report, including appropriate key performance indicators and recommendations.
207
-
208
- Data:
209
- {str(calculate_kpis(filtered_df))}
210
- {str(get_pandas_profile(filtered_df))}
211
- """
212
-
213
- response = model.generate_content(prompt)
214
- report = response.text
215
- st.markdown(report)
216
- st.success("Report Generated!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
 
218
  else:
219
- st.write("Filtered DataFrame")
220
  st.write("Click 'Apply Filters' to see the filtered data.")
221
 
222
 
 
141
  answer = generateResponse(dataFrame=df,prompt=user_input)
142
  st.write(answer)
143
 
144
+ if tabs == 'Reports':
145
  df = pd.read_csv(uploaded_file)
146
 
147
  st.subheader("Reports")
148
+ st.write("Filter by Company Name, Gender, Youth, Intervention Category or Intervention to generate report")
149
 
150
  # Filtering Interface
151
  st.write("Filtering Options")
152
+ company_names = df['Company Name'].unique().tolist()
153
  gender_options = df['Gender'].unique().tolist()
154
  youth_options = df['Youth'].unique().tolist()
155
  intervention_categories = df['Intervention_Category'].unique().tolist()
156
  intervention_types = df['Intervention'].unique().tolist()
157
 
158
+ selected_companies = st.multiselect('Select Company(ies)', company_names, default=company_names)
 
159
  selected_genders = st.multiselect('Select Gender(s)', gender_options, default=gender_options)
160
  selected_youth = st.multiselect('Select Youth Status(es)', youth_options, default=youth_options)
161
  selected_categories = st.multiselect('Select Intervention Category(ies)', intervention_categories, default=intervention_categories)
162
  selected_interventions = st.multiselect('Select Intervention(s)', intervention_types, default=intervention_types)
163
 
 
164
  if st.button('Apply Filters and Generate report'):
165
  filtered_df = df.copy()
166
 
 
175
  if selected_interventions:
176
  filtered_df = filtered_df[filtered_df['Intervention'].isin(selected_interventions)]
177
 
178
+ if not filtered_df.empty:
179
+ if len(filtered_df) > 1:
180
+ st.write("Filtered DataFrame")
181
+ with st.expander("Preview"):
182
+ st.write(filtered_df.head())
183
+
184
+ # Dashboard Visualizations (Plotly)
185
+ st.subheader("Dashboard")
186
+ col1, col2 = st.columns([1, 1]) # Adjust column ratios if needed
187
+
188
+ with col1:
189
+ try:
190
+ fig_gender = px.histogram(filtered_df, x="Gender", title="Gender Distribution")
191
+ fig_gender.update_layout(height=400, width=500)
192
+ st.plotly_chart(fig_gender)
193
+
194
+ fig_youth = px.histogram(filtered_df, x="Youth", title="Youth Participation")
195
+ fig_youth.update_layout(height=400, width=500)
196
+ st.plotly_chart(fig_youth)
197
+ except Exception as e:
198
+ st.write(f"Error generating visualizations: {e}")
199
+
200
+ with col2:
201
+ try:
202
+ fig_category = px.histogram(filtered_df, x="Intervention_Category", title="Intervention Category Distribution")
203
+ fig_category.update_layout(height=400, width=500)
204
+ st.plotly_chart(fig_category)
205
+
206
+ fig_intervention = px.histogram(filtered_df, x="Intervention", title="Intervention Type Distribution")
207
+ fig_intervention.update_layout(height=400, width=500)
208
+ st.plotly_chart(fig_intervention)
209
+ except Exception as e:
210
+ st.write(f"Error generating visualizations: {e}")
211
+
212
+ with st.spinner("Generating Report, Please Wait...."):
213
+ try:
214
+ prompt = f"""You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report, including appropriate key performance indicators and recommendations.Data:{str(calculate_kpis(filtered_df))}{str(get_pandas_profile(filtered_df))}"""
215
+ response = model.generate_content(prompt)
216
+ report = response.text
217
+ st.markdown(report)
218
+ st.success("Report Generated!")
219
+ except Exception as e:
220
+ st.write(f"Error generating report: {e}")
221
+
222
+ else: # Single row or empty
223
+ st.write("Not enough data after filtering for full visualizations and report generation.")
224
+ if not filtered_df.empty:
225
+ st.write("Filtered DataFrame:")
226
+ st.write(filtered_df) # Display the single row
227
+ else:
228
+ st.write("No data after filtering.")
229
 
230
  else:
 
231
  st.write("Click 'Apply Filters' to see the filtered data.")
232
 
233