rairo commited on
Commit
55b75b4
·
verified ·
1 Parent(s): 091f61e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -61
app.py CHANGED
@@ -37,54 +37,6 @@ model = genai.GenerativeModel(
37
  generation_config=generation_config,
38
  )
39
 
40
- def calculate_kpis(df):
41
- """
42
- Calculates key performance indicators from a given transaction dataset.
43
-
44
- Args:
45
- df: Pandas DataFrame containing transaction data.
46
-
47
- Returns:
48
- A JSON object containing the calculated KPIs.
49
- """
50
-
51
- # Calculate Total Revenue
52
- total_revenue = df['Price'] * df['Quantity'].sum()
53
-
54
- # Calculate Top Five Products by Revenue
55
- if df['Description'].nunique() > 5:
56
- top_five_products = df.groupby('Description')['Price'].sum().nlargest(5).index.tolist()
57
- else:
58
- top_five_product = "there are less than 5 products in this dataset"
59
-
60
- if df['Branch_Name'].nunique() > 1:
61
- best_branch = df.groupby('Branch_Name')['Price'].sum().nlargest(1).index.tolist()
62
- else:
63
- best_branch = "there is only one branch in this dataset"
64
-
65
- # Calculate Average Order Value (AOV)
66
- aov = df.groupby('Receipt No_')['Price'].sum().mean()
67
-
68
- # Calculate Customer Purchase Frequency (Requires more data for accurate calculation)
69
- # Assuming 'Member Card No_' is a unique identifier for customers
70
- customer_purchase_frequency = df.groupby('Customer_Name')['Receipt No_'].nunique().mean()
71
-
72
- # Calculate Estimated Customer Lifetime Value (CLTV) (Requires more data for accurate calculation)
73
- # Assuming a simple CLTV model based on AOV and purchase frequency
74
- estimated_cltv = aov * customer_purchase_frequency * 12 # Assuming annual value
75
-
76
- # Create JSON output
77
- kpis = {
78
- "total_revenue": total_revenue,
79
- "top_five_products": top_five_products,
80
- "average_order_value": aov,
81
- "customer_purchase_frequency": customer_purchase_frequency,
82
- "estimated_cltv": estimated_cltv,
83
- "best_performing_branch": best_branch
84
- }
85
-
86
- return kpis
87
-
88
 
89
  def get_pandas_profile(df):
90
  profile = ProfileReport(df, title="Profiling Report")
@@ -102,9 +54,9 @@ def generateResponse(dataFrame,prompt):
102
  answer = pandas_agent.chat(prompt)
103
  return answer
104
 
105
- st.write("# Brave Retail Insights")
106
  st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
107
- st.write("##### Engage in insightful conversations with your data through powerful visualizations")
108
  with st.sidebar:
109
  st.title("Brave Retail Insights")
110
  st.sidebar.image("IMG_1181.jpeg", use_column_width=True)
@@ -113,11 +65,11 @@ with st.sidebar:
113
 
114
 
115
 
116
- uploaded_file = "bon_marche.csv"
117
  #uploaded_file = "healthcare_dataset.csv"
118
  if tabs =='Chat':
119
  df = pd.read_csv(uploaded_file)
120
- st.subheader("Brave Retail Chat")
121
  st.write("Get visualizations and analysis from our Gemini powered agent")
122
 
123
  # Read the CSV file
@@ -139,15 +91,15 @@ elif tabs == 'Reports':
139
 
140
  # Streamlit App
141
  st.subheader("Reports")
142
- st.write("Filter by Branch Name or Product to generate report")
143
 
144
  # Display original
145
 
146
  # Filtering Interface
147
  st.write("Filtering Options")
148
- branch_names = df['Branch_Name'].unique().tolist()
149
  #product_names = df['Description'].unique().tolist()
150
- selected_branches = st.multiselect('Select Branch(es) Name', branch_names, default=branch_names)
151
  #selected_products = st.multiselect('Select product(s) Name', product_names, default=product_names)
152
 
153
  # Button to apply filters
@@ -157,7 +109,7 @@ elif tabs == 'Reports':
157
 
158
  # Apply Branch Name Filter
159
  if selected_branches:
160
- filtered_df = filtered_df[filtered_df['Branch_Name'].isin(selected_branches)]
161
 
162
  # Apply Description Filter
163
  #if selected_products:
@@ -169,19 +121,17 @@ elif tabs == 'Reports':
169
  st.write(filtered_df.head())
170
  with st.spinner("Generating Report, Please Wait...."):
171
  prompt = """
172
- You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report, including appropriate key perfomance indicators and reccomendations.
173
 
174
  data:
175
- """ + str(calculate_kpis(filtered_df)) + str(get_pandas_profile(filtered_df))
176
 
177
  response = model.generate_content(prompt)
178
- response2 = generateResponse(filtered_df, "pie chart of revenue by branch")
179
  response3 = generateResponse(filtered_df, "bar chart of of most popular products")
180
  report = response.text
181
  st.markdown(report)
182
  # Display the generated images
183
- st.markdown(response2)
184
- st.markdown(response3)
185
  st.success("Report Generated!")
186
  else:
187
  st.write("Filtered DataFrame")
 
37
  generation_config=generation_config,
38
  )
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  def get_pandas_profile(df):
42
  profile = ProfileReport(df, title="Profiling Report")
 
54
  answer = pandas_agent.chat(prompt)
55
  return answer
56
 
57
+ st.write("# QuantBeta Insights")
58
  st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
59
+ st.write("##### Engage in insightful conversations with your data")
60
  with st.sidebar:
61
  st.title("Brave Retail Insights")
62
  st.sidebar.image("IMG_1181.jpeg", use_column_width=True)
 
65
 
66
 
67
 
68
+ uploaded_file = "64QBeta.csv"
69
  #uploaded_file = "healthcare_dataset.csv"
70
  if tabs =='Chat':
71
  df = pd.read_csv(uploaded_file)
72
+ st.subheader("QuantBeta AI assistant")
73
  st.write("Get visualizations and analysis from our Gemini powered agent")
74
 
75
  # Read the CSV file
 
91
 
92
  # Streamlit App
93
  st.subheader("Reports")
94
+ st.write("Filter by Incubator to generate report")
95
 
96
  # Display original
97
 
98
  # Filtering Interface
99
  st.write("Filtering Options")
100
+ branch_names = df['Incubator Name'].unique().tolist()
101
  #product_names = df['Description'].unique().tolist()
102
+ selected_branches = st.multiselect('Select incubator(s) Name(s)', branch_names, default=branch_names)
103
  #selected_products = st.multiselect('Select product(s) Name', product_names, default=product_names)
104
 
105
  # Button to apply filters
 
109
 
110
  # Apply Branch Name Filter
111
  if selected_branches:
112
+ filtered_df = filtered_df[filtered_df['Incubator Name'].isin(selected_branches)]
113
 
114
  # Apply Description Filter
115
  #if selected_products:
 
121
  st.write(filtered_df.head())
122
  with st.spinner("Generating Report, Please Wait...."):
123
  prompt = """
124
+ You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report, including appropriate key perfomance indicators and recommendations.
125
 
126
  data:
127
+ """ + str(filtered_df.to_json(orient='records')) + str(get_pandas_profile(filtered_df))
128
 
129
  response = model.generate_content(prompt)
130
+ response2 = generateResponse(filtered_df, "pie chart of number of hours by branch")
131
  response3 = generateResponse(filtered_df, "bar chart of of most popular products")
132
  report = response.text
133
  st.markdown(report)
134
  # Display the generated images
 
 
135
  st.success("Report Generated!")
136
  else:
137
  st.write("Filtered DataFrame")