rairo commited on
Commit
f60452f
·
verified ·
1 Parent(s): c1b5a20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -39,16 +39,31 @@ model = genai.GenerativeModel(
39
  )
40
 
41
  def calculate_kpis(df):
42
- """Calculates KPIs relevant to the given dataset."""
43
-
44
- total_interventions = len(df)
45
- interventions_by_category = df['Intervention_Category'].value_counts().to_dict()
46
- interventions_by_type = df['Intervention'].value_counts().to_dict()
47
- male_participants = len(df[df['Gender'] == 'Male'])
48
- female_participants = len(df[df['Gender'] == 'Female'])
49
- youth_participants = len(df[df['Youth'] == 'Yes'])
50
- non_youth_participants = len(df[df['Youth'] == 'No'])
51
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  kpis = {
54
  "total_interventions": total_interventions,
@@ -229,7 +244,13 @@ elif tabs == 'Reports':
229
 
230
  with st.spinner("Generating Report, Please Wait...."):
231
  try:
232
- 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))}"""
 
 
 
 
 
 
233
  response = model.generate_content(prompt)
234
  report = response.text
235
  st.markdown(report)
 
39
  )
40
 
41
  def calculate_kpis(df):
42
+ total_interventions = len(df) if hasattr(df, '__len__') else 0 # Check if df is a dataframe
43
+ try:
44
+ interventions_by_category = df['Intervention_Category'].value_counts().to_dict() if 'Intervention_Category' in df.columns else {}
45
+ except AttributeError:
46
+ interventions_by_category = {}
47
+ try:
48
+ interventions_by_type = df['Intervention'].value_counts().to_dict() if 'Intervention' in df.columns else {}
49
+ except AttributeError:
50
+ interventions_by_type = {}
51
+ try:
52
+ male_participants = len(df[df['Gender'] == 'Male']) if 'Gender' in df.columns else 0
53
+ except TypeError:
54
+ male_participants = 0
55
+ try:
56
+ female_participants = len(df[df['Gender'] == 'Female']) if 'Gender' in df.columns else 0
57
+ except TypeError:
58
+ female_participants = 0
59
+ try:
60
+ youth_participants = len(df[df['Youth'] == 'Yes']) if 'Youth' in df.columns else 0
61
+ except TypeError:
62
+ youth_participants = 0
63
+ try:
64
+ non_youth_participants = len(df[df['Youth'] == 'No']) if 'Youth' in df.columns else 0
65
+ except TypeError:
66
+ non_youth_participants = 0
67
 
68
  kpis = {
69
  "total_interventions": total_interventions,
 
244
 
245
  with st.spinner("Generating Report, Please Wait...."):
246
  try:
247
+ prompt = f"""
248
+ You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report,
249
+ including appropriate key performance indicators and recommendations.
250
+ Data: dataset:{str(filtered_df.to_json(orient='records'))},
251
+ kpi:{str(calculate_kpis(filtered_df))}, dataset profile:{str(get_pandas_profile(filtered_df))}
252
+ """
253
+
254
  response = model.generate_content(prompt)
255
  report = response.text
256
  st.markdown(report)