Spaces:
Sleeping
Sleeping
File size: 8,999 Bytes
0404f33 96ede08 0404f33 70415ca 455ee5c b59c2f5 8094bf5 0404f33 455ee5c d989293 455ee5c 7542eff f60452f 7542eff d848201 7542eff 741bfcf 7542eff 455ee5c 8a6ebbd 455ee5c 0404f33 d848201 70415ca 295c000 44b878d 0404f33 295c000 3624c3a 240321f 0404f33 d848201 44b878d 2c75106 44b878d 5079b27 44b878d 2c75106 89ae594 2c75106 89ae594 b615f95 2c75106 89ae594 e5e194c 2c75106 e5e194c c1b5a20 cf2d4c0 d848201 76fb0de d848201 76fb0de d848201 76fb0de d848201 76fb0de 65f8c05 f60452f 942005e f60452f 76fb0de e5e194c 76fb0de c1b5a20 76fb0de c1b5a20 76fb0de d848201 65f8c05 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
from pandasai.llm import GoogleGemini
import streamlit as st
import os
import pandas as pd
from pandasai import SmartDataframe
from pandasai.responses.response_parser import ResponseParser
from st_on_hover_tabs import on_hover_tabs
from ydata_profiling import ProfileReport
import google.generativeai as genai
import json
import plotly.express as px
class StreamLitResponse(ResponseParser):
def __init__(self,context) -> None:
super().__init__(context)
def format_dataframe(self,result):
st.dataframe(result['value'])
return
def format_plot(self,result):
st.image(result['value'])
return
def format_other(self, result):
st.write(result['value'])
return
gemini_api_key = os.environ['Gemini']
genai.configure(api_key=gemini_api_key)
generation_config = {
"temperature": 0.2,
"top_p": 0.95,
"max_output_tokens": 5000,
}
model = genai.GenerativeModel(
model_name="gemini-2.0-flash-thinking-exp",
generation_config=generation_config,
)
def calculate_kpis(df):
total_interventions = len(df) if hasattr(df, '__len__') else 0 # Check if df is a dataframe
try:
interventions_by_category = df['Intervention_Category'].value_counts().to_dict() if 'Intervention_Category' in df.columns else {}
except AttributeError:
interventions_by_category = {}
try:
interventions_by_type = df['Intervention'].value_counts().to_dict() if 'Intervention' in df.columns else {}
except AttributeError:
interventions_by_type = {}
try:
male_participants = len(df[df['Gender'] == 'Male']) if 'Gender' in df.columns else 0
except TypeError:
male_participants = 0
try:
female_participants = len(df[df['Gender'] == 'Female']) if 'Gender' in df.columns else 0
except TypeError:
female_participants = 0
try:
youth_participants = len(df[df['Youth'] == 'Yes']) if 'Youth' in df.columns else 0
except TypeError:
youth_participants = 0
try:
non_youth_participants = len(df[df['Youth'] == 'No']) if 'Youth' in df.columns else 0
except TypeError:
non_youth_participants = 0
kpis = {
"total_interventions": total_interventions,
"interventions_by_category": interventions_by_category,
"interventions_by_type": interventions_by_type,
"male_participants": male_participants,
"female_participants": female_participants,
"youth_participants": youth_participants,
"non_youth_participants": non_youth_participants,
}
return kpis
def get_pandas_profile(df):
profile = ProfileReport(df, title="Profiling Report")
json_profile = profile.to_json()
dict_p = json.loads(json_profile)
keys_to_keep = ['analysis', 'table', 'correlations', 'alerts', 'sample']
# Assuming your dictionary is named 'my_dict'
filtered_dict = {key: dict_p[key] for key in keys_to_keep}
return filtered_dict
def generateResponse(dataFrame,prompt):
llm = GoogleGemini(api_key=gemini_api_key)
pandas_agent = SmartDataframe(dataFrame,config={"llm":llm, "response_parser":StreamLitResponse})
answer = pandas_agent.chat(prompt)
return answer
st.write("# Intervention Analytics")
st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
st.write("Get analysis and engage in insightful conversations with your data through our powerful AI")
with st.sidebar:
st.subheader("Intervention Analytics")
st.sidebar.image("logoqb.jpeg", use_column_width=True)
tabs = on_hover_tabs(tabName=['Chat', 'Reports'], iconName=['chat', 'dashboard'], default_choice=0)
uploaded_file = "Intervention.csv"
# Load the full data *once*
df = pd.read_csv(uploaded_file)
if tabs == 'Chat':
st.write("Overall Data Visualizations") # Title above the visualizations
col1, col2 = st.columns([1, 1]) # Half and half split
with col1: # Visualizations on the left
st.set_option('deprecation.showPyplotGlobalUse', False) # Suppress warning
fig_youth = px.histogram(df, x="Youth", title="Youth Participation")
fig_youth.update_layout(height=400, width=None) # Responsive width
st.plotly_chart(fig_youth)
fig_company = px.histogram(df, y="Company Name", title="Company Distribution")
fig_company.update_layout(height=400, width=None) # Responsive width
st.plotly_chart(fig_company)
fig_category = px.histogram(df, y="Intervention_Category", title="Intervention Category Distribution")
fig_category.update_layout(height=400, width=None) # Responsive width
st.plotly_chart(fig_category)
fig_intervention = px.histogram(df, y="Intervention", title="Intervention Type Distribution")
fig_intervention.update_layout(height=400, width=None) # Responsive width
st.plotly_chart(fig_intervention)
fig_pie_category = px.pie(df, names='Intervention_Category', title='Intervention Category Pie Chart')
fig_pie_category.update_layout(height=400, width=None) # Responsive width
st.plotly_chart(fig_pie_category)
with col2: # Chat on the right
st.subheader("Chat with AI")
st.write("Get visualizations and analysis from our Gemini powered agent")
with st.expander("Preview"):
st.write(df.head())
user_input = st.text_input("Type your message here", placeholder="Ask me about your data")
if user_input:
answer = generateResponse(dataFrame=df, prompt=user_input)
st.write(answer)
elif tabs == 'Reports':
df = pd.read_csv(uploaded_file)
st.subheader("Reports")
st.write("Filter by Company Name, Gender, Youth, Intervention Category or Intervention to generate report")
# Filtering Interface
st.write("Filtering Options")
company_names = df['Company Name'].unique().tolist()
gender_options = df['Gender'].unique().tolist()
youth_options = df['Youth'].unique().tolist()
intervention_categories = df['Intervention_Category'].unique().tolist()
intervention_types = df['Intervention'].unique().tolist()
selected_companies = st.multiselect('Select Company(ies)', company_names, default=company_names)
selected_genders = st.multiselect('Select Gender(s)', gender_options, default=gender_options)
selected_youth = st.multiselect('Select Youth Status(es)', youth_options, default=youth_options)
selected_categories = st.multiselect('Select Intervention Category(ies)', intervention_categories, default=intervention_categories)
selected_interventions = st.multiselect('Select Intervention(s)', intervention_types, default=intervention_types)
if st.button('Apply Filters and Generate report'):
filtered_df = df.copy()
if selected_companies:
filtered_df = filtered_df[filtered_df['Company Name'].isin(selected_companies)]
if selected_genders:
filtered_df = filtered_df[filtered_df['Gender'].isin(selected_genders)]
if selected_youth:
filtered_df = filtered_df[filtered_df['Youth'].isin(selected_youth)]
if selected_categories:
filtered_df = filtered_df[filtered_df['Intervention_Category'].isin(selected_categories)]
if selected_interventions:
filtered_df = filtered_df[filtered_df['Intervention'].isin(selected_interventions)]
if not filtered_df.empty:
if len(filtered_df) > 1:
st.write("Filtered DataFrame")
with st.expander("Preview"):
st.write(filtered_df.head())
with st.spinner("Generating Report, Please Wait...."):
try:
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: dataset:{str(filtered_df.to_json(orient='records'))}, kpis: {str(calculate_kpis(filtered_df))}
"""
response = model.generate_content(prompt)
report = response.text
st.markdown(report)
st.success("Report Generation Complete")
except Exception as e:
st.write(f"Error generating report: {e}")
else:
st.write("Not enough data after filtering for full visualizations and report generation.")
if not filtered_df.empty:
st.write("Filtered DataFrame:")
st.write(filtered_df)
else:
st.write("No data after filtering.")
else:
st.write("Click 'Apply Filters' to see the filtered data.")
|