QuantBetaAI / app.py
rairo's picture
Update app.py
5575725 verified
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
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-1.5-flash",
generation_config=generation_config,
)
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("# QuantBeta Insights")
st.markdown('<style>' + open('./style.css').read() + '</style>', unsafe_allow_html=True)
st.write("##### Engage in insightful conversations with your data")
with st.sidebar:
st.title("QuantBeta Insights")
st.sidebar.image("logoqb.jpeg", use_column_width=True)
tabs = on_hover_tabs(tabName=['Chat', 'Reports'],
iconName=['chat', 'dashboard'], default_choice=0)
uploaded_file = "64Qbeta.csv"
#uploaded_file = "healthcare_dataset.csv"
if tabs =='Chat':
df = pd.read_csv(uploaded_file)
st.subheader("QuantBeta AI assistant")
st.write("Get visualizations and analysis from our Gemini powered agent")
# Read the CSV file
#df = pd.read_csv(uploaded_file)
# Display the data
with st.expander("Preview"):
st.write(df.head())
# Plot the data
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)
# Streamlit App
st.subheader("Reports")
st.write("Filter by Incubator to generate report")
# Display original
# Filtering Interface
st.write("Filtering Options")
branch_names = df['Incubator Name'].unique().tolist()
#product_names = df['Description'].unique().tolist()
selected_branches = st.multiselect('Select incubator(s) Name(s)', branch_names, default=branch_names)
#selected_products = st.multiselect('Select product(s) Name', product_names, default=product_names)
# Button to apply filters
if st.button('Apply Filters and Generate report'):
df = pd.read_csv(uploaded_file)
filtered_df = df.copy()
# Apply Branch Name Filter
if selected_branches:
filtered_df = filtered_df[filtered_df['Incubator Name'].isin(selected_branches)]
# Apply Description Filter
#if selected_products:
# filtered_df = filtered_df[filtered_df['Product_Name'].isin(selected_products)]
# Display filtered DataFrame
st.write("Filtered DataFrame")
with st.expander("Preview"):
st.write(filtered_df.head())
with st.spinner("Generating Report, Please Wait...."):
prompt = """
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.
data:
""" + str(filtered_df.to_json(orient='records'))
response = model.generate_content(prompt)
response2 = generateResponse(filtered_df, "pie chart of number of hours by incubator")
report = response.text
st.markdown(report)
# Display the generated images
st.success("Report Generated!")
else:
st.write("Filtered DataFrame")
st.write("Click 'Apply Filters' to see the filtered data.")