Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,51 +38,26 @@ model = genai.GenerativeModel(
|
|
| 38 |
)
|
| 39 |
|
| 40 |
def calculate_kpis(df):
|
| 41 |
-
"""
|
| 42 |
-
Calculates key performance indicators from a given transaction dataset.
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
"
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
"
|
|
|
|
| 84 |
}
|
| 85 |
-
|
| 86 |
return kpis
|
| 87 |
|
| 88 |
|
|
@@ -102,22 +77,102 @@ def generateResponse(dataFrame,prompt):
|
|
| 102 |
answer = pandas_agent.chat(prompt)
|
| 103 |
return answer
|
| 104 |
|
| 105 |
-
st.write("#
|
| 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("
|
| 110 |
st.sidebar.image("IMG_1181.jpeg", use_column_width=True)
|
| 111 |
-
tabs = on_hover_tabs(tabName=['
|
| 112 |
-
iconName=['
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
-
uploaded_file = "
|
| 117 |
#uploaded_file = "healthcare_dataset.csv"
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
df = pd.read_csv(uploaded_file)
|
| 120 |
-
st.subheader("
|
| 121 |
st.write("Get visualizations and analysis from our Gemini powered agent")
|
| 122 |
|
| 123 |
# Read the CSV file
|
|
@@ -132,59 +187,4 @@ if tabs =='Chat':
|
|
| 132 |
if user_input:
|
| 133 |
answer = generateResponse(dataFrame=df,prompt=user_input)
|
| 134 |
st.write(answer)
|
| 135 |
-
|
| 136 |
-
elif tabs == 'Reports':
|
| 137 |
-
df = pd.read_csv(uploaded_file)
|
| 138 |
-
|
| 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
|
| 154 |
-
if st.button('Apply Filters and Generate report'):
|
| 155 |
-
df = pd.read_csv(uploaded_file)
|
| 156 |
-
filtered_df = df.copy()
|
| 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:
|
| 164 |
-
# filtered_df = filtered_df[filtered_df['Product_Name'].isin(selected_products)]
|
| 165 |
-
|
| 166 |
-
# Display filtered DataFrame
|
| 167 |
-
st.write("Filtered DataFrame")
|
| 168 |
-
with st.expander("Preview"):
|
| 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")
|
| 188 |
-
st.write("Click 'Apply Filters' to see the filtered data.")
|
| 189 |
-
|
| 190 |
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
def calculate_kpis(df):
|
| 41 |
+
"""Calculates KPIs relevant to the given dataset."""
|
|
|
|
| 42 |
|
| 43 |
+
total_interventions = len(df)
|
| 44 |
+
interventions_by_category = df['Intervention_Category'].value_counts().to_dict()
|
| 45 |
+
interventions_by_type = df['Intervention'].value_counts().to_dict()
|
| 46 |
+
male_participants = len(df[df['Gender'] == 'Male'])
|
| 47 |
+
female_participants = len(df[df['Gender'] == 'Female'])
|
| 48 |
+
youth_participants = len(df[df['Youth'] == 'Yes'])
|
| 49 |
+
non_youth_participants = len(df[df['Youth'] == 'No'])
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
kpis = {
|
| 53 |
+
"total_interventions": total_interventions,
|
| 54 |
+
"interventions_by_category": interventions_by_category,
|
| 55 |
+
"interventions_by_type": interventions_by_type,
|
| 56 |
+
"male_participants": male_participants,
|
| 57 |
+
"female_participants": female_participants,
|
| 58 |
+
"youth_participants": youth_participants,
|
| 59 |
+
"non_youth_participants": non_youth_participants,
|
| 60 |
}
|
|
|
|
| 61 |
return kpis
|
| 62 |
|
| 63 |
|
|
|
|
| 77 |
answer = pandas_agent.chat(prompt)
|
| 78 |
return answer
|
| 79 |
|
| 80 |
+
st.write("# Intervention Analytics")
|
| 81 |
st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
|
| 82 |
st.write("##### Engage in insightful conversations with your data through powerful visualizations")
|
| 83 |
with st.sidebar:
|
| 84 |
+
st.title("Intervention Analytics")
|
| 85 |
st.sidebar.image("IMG_1181.jpeg", use_column_width=True)
|
| 86 |
+
tabs = on_hover_tabs(tabName=['Dashboard & Reports', 'Chat'],
|
| 87 |
+
iconName=['dashboard', 'chat'], default_choice=0)
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
+
uploaded_file = "Intervention.csv"
|
| 92 |
#uploaded_file = "healthcare_dataset.csv"
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
if tabs == 'Reports':
|
| 96 |
+
df = pd.read_csv(uploaded_file)
|
| 97 |
+
|
| 98 |
+
st.subheader("Reports")
|
| 99 |
+
st.write("Filter by Company Name, Product, Gender, Youth, Intervention Category or Intervention to generate report")
|
| 100 |
+
|
| 101 |
+
# Filtering Interface
|
| 102 |
+
st.write("Filtering Options")
|
| 103 |
+
branch_names = df['Company Name'].unique().tolist() # Company Name instead of Branch Name
|
| 104 |
+
gender_options = df['Gender'].unique().tolist()
|
| 105 |
+
youth_options = df['Youth'].unique().tolist()
|
| 106 |
+
intervention_categories = df['Intervention_Category'].unique().tolist()
|
| 107 |
+
intervention_types = df['Intervention'].unique().tolist()
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
selected_companies = st.multiselect('Select Company(ies)', branch_names, default=branch_names)
|
| 111 |
+
selected_genders = st.multiselect('Select Gender(s)', gender_options, default=gender_options)
|
| 112 |
+
selected_youth = st.multiselect('Select Youth Status(es)', youth_options, default=youth_options)
|
| 113 |
+
selected_categories = st.multiselect('Select Intervention Category(ies)', intervention_categories, default=intervention_categories)
|
| 114 |
+
selected_interventions = st.multiselect('Select Intervention(s)', intervention_types, default=intervention_types)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
if st.button('Apply Filters and Generate report'):
|
| 118 |
+
filtered_df = df.copy()
|
| 119 |
+
|
| 120 |
+
if selected_companies:
|
| 121 |
+
filtered_df = filtered_df[filtered_df['Company Name'].isin(selected_companies)]
|
| 122 |
+
if selected_genders:
|
| 123 |
+
filtered_df = filtered_df[filtered_df['Gender'].isin(selected_genders)]
|
| 124 |
+
if selected_youth:
|
| 125 |
+
filtered_df = filtered_df[filtered_df['Youth'].isin(selected_youth)]
|
| 126 |
+
if selected_categories:
|
| 127 |
+
filtered_df = filtered_df[filtered_df['Intervention_Category'].isin(selected_categories)]
|
| 128 |
+
if selected_interventions:
|
| 129 |
+
filtered_df = filtered_df[filtered_df['Intervention'].isin(selected_interventions)]
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
st.write("Filtered DataFrame")
|
| 133 |
+
with st.expander("Preview"):
|
| 134 |
+
st.write(filtered_df.head())
|
| 135 |
+
|
| 136 |
+
# Dashboard Visualizations (Plotly)
|
| 137 |
+
st.subheader("Dashboard")
|
| 138 |
+
|
| 139 |
+
col1, col2 = st.columns(2) # Create two columns for better layout
|
| 140 |
+
|
| 141 |
+
with col1:
|
| 142 |
+
fig_gender = px.histogram(filtered_df, x="Gender", title="Gender Distribution")
|
| 143 |
+
st.plotly_chart(fig_gender)
|
| 144 |
+
|
| 145 |
+
fig_youth = px.histogram(filtered_df, x="Youth", title="Youth Participation")
|
| 146 |
+
st.plotly_chart(fig_youth)
|
| 147 |
+
|
| 148 |
+
with col2:
|
| 149 |
+
fig_category = px.histogram(filtered_df, x="Intervention_Category", title="Intervention Category Distribution")
|
| 150 |
+
st.plotly_chart(fig_category)
|
| 151 |
+
|
| 152 |
+
fig_intervention = px.histogram(filtered_df, x="Intervention", title="Intervention Type Distribution")
|
| 153 |
+
st.plotly_chart(fig_intervention)
|
| 154 |
+
|
| 155 |
+
with st.spinner("Generating Report, Please Wait...."):
|
| 156 |
+
prompt = f"""
|
| 157 |
+
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.
|
| 158 |
+
|
| 159 |
+
Data:
|
| 160 |
+
{str(calculate_kpis(filtered_df))}
|
| 161 |
+
{str(get_pandas_profile(filtered_df))}
|
| 162 |
+
"""
|
| 163 |
+
|
| 164 |
+
response = model.generate_content(prompt)
|
| 165 |
+
report = response.text
|
| 166 |
+
st.markdown(report)
|
| 167 |
+
st.success("Report Generated!")
|
| 168 |
+
|
| 169 |
+
else:
|
| 170 |
+
st.write("Filtered DataFrame")
|
| 171 |
+
st.write("Click 'Apply Filters' to see the filtered data.")
|
| 172 |
+
|
| 173 |
+
elif tabs =='Chat':
|
| 174 |
df = pd.read_csv(uploaded_file)
|
| 175 |
+
st.subheader("Chat with AI")
|
| 176 |
st.write("Get visualizations and analysis from our Gemini powered agent")
|
| 177 |
|
| 178 |
# Read the CSV file
|
|
|
|
| 187 |
if user_input:
|
| 188 |
answer = generateResponse(dataFrame=df,prompt=user_input)
|
| 189 |
st.write(answer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|