Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ PAYLOAD = {
|
|
| 17 |
}
|
| 18 |
|
| 19 |
def fetch_data():
|
| 20 |
-
"""Fetch stock card report data from API
|
| 21 |
response = requests.post(API_URL, data=PAYLOAD)
|
| 22 |
if response.status_code == 200:
|
| 23 |
try:
|
|
@@ -86,7 +86,6 @@ if not GOOGLE_API_KEY:
|
|
| 86 |
def generateResponse(prompt, df):
|
| 87 |
"""Generate response using PandasAI with SmartDataframe"""
|
| 88 |
llm = GoogleGemini(api_key=GOOGLE_API_KEY)
|
| 89 |
-
# Use SmartDataframe for a single DataFrame
|
| 90 |
pandas_agent = SmartDataframe(df, config={
|
| 91 |
"llm": llm,
|
| 92 |
"response_parser": StreamLitResponse
|
|
@@ -113,7 +112,6 @@ def render_chat_message(message):
|
|
| 113 |
st.write("Unsupported plot format")
|
| 114 |
except Exception as e:
|
| 115 |
st.error(f"Error rendering plot: {e}")
|
| 116 |
-
|
| 117 |
if "content" in message:
|
| 118 |
st.markdown(message["content"])
|
| 119 |
|
|
@@ -162,39 +160,63 @@ def handle_userinput(question, df):
|
|
| 162 |
def main():
|
| 163 |
st.set_page_config(page_title="AI Chat with Your Data", page_icon="๐")
|
| 164 |
|
| 165 |
-
# Initialize session state variables
|
| 166 |
if "chat_history" not in st.session_state:
|
| 167 |
st.session_state.chat_history = []
|
| 168 |
if "dfs" not in st.session_state:
|
| 169 |
st.session_state.dfs = fetch_data() # Load DataFrame at startup
|
| 170 |
|
| 171 |
-
# Create two tabs:
|
| 172 |
tab_chat, tab_reports = st.tabs(["Chat", "Reports"])
|
| 173 |
|
|
|
|
| 174 |
with tab_chat:
|
| 175 |
st.title("AI Chat with Your Data ๐")
|
| 176 |
-
#
|
| 177 |
chat_container = st.container()
|
| 178 |
with chat_container:
|
| 179 |
for message in st.session_state.chat_history:
|
| 180 |
with st.chat_message(message["role"]):
|
| 181 |
render_chat_message(message)
|
| 182 |
-
|
| 183 |
user_question = st.chat_input("Ask a question about your data:")
|
| 184 |
if user_question:
|
| 185 |
handle_userinput(user_question, st.session_state.dfs)
|
| 186 |
-
#
|
| 187 |
chat_container.empty()
|
| 188 |
with chat_container:
|
| 189 |
for message in st.session_state.chat_history:
|
| 190 |
with st.chat_message(message["role"]):
|
| 191 |
render_chat_message(message)
|
| 192 |
|
|
|
|
| 193 |
with tab_reports:
|
| 194 |
st.title("Reports")
|
| 195 |
-
st.write("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
# Sidebar
|
| 198 |
with st.sidebar:
|
| 199 |
st.subheader("Options")
|
| 200 |
if st.button("Reload Data"):
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
def fetch_data():
|
| 20 |
+
"""Fetch stock card report data from API and return cleaned DataFrame"""
|
| 21 |
response = requests.post(API_URL, data=PAYLOAD)
|
| 22 |
if response.status_code == 200:
|
| 23 |
try:
|
|
|
|
| 86 |
def generateResponse(prompt, df):
|
| 87 |
"""Generate response using PandasAI with SmartDataframe"""
|
| 88 |
llm = GoogleGemini(api_key=GOOGLE_API_KEY)
|
|
|
|
| 89 |
pandas_agent = SmartDataframe(df, config={
|
| 90 |
"llm": llm,
|
| 91 |
"response_parser": StreamLitResponse
|
|
|
|
| 112 |
st.write("Unsupported plot format")
|
| 113 |
except Exception as e:
|
| 114 |
st.error(f"Error rendering plot: {e}")
|
|
|
|
| 115 |
if "content" in message:
|
| 116 |
st.markdown(message["content"])
|
| 117 |
|
|
|
|
| 160 |
def main():
|
| 161 |
st.set_page_config(page_title="AI Chat with Your Data", page_icon="๐")
|
| 162 |
|
| 163 |
+
# Initialize session state variables if not present
|
| 164 |
if "chat_history" not in st.session_state:
|
| 165 |
st.session_state.chat_history = []
|
| 166 |
if "dfs" not in st.session_state:
|
| 167 |
st.session_state.dfs = fetch_data() # Load DataFrame at startup
|
| 168 |
|
| 169 |
+
# Create two tabs: Chat and Reports
|
| 170 |
tab_chat, tab_reports = st.tabs(["Chat", "Reports"])
|
| 171 |
|
| 172 |
+
# --- Chat Tab ---
|
| 173 |
with tab_chat:
|
| 174 |
st.title("AI Chat with Your Data ๐")
|
| 175 |
+
# Container for chat messages so they update smoothly
|
| 176 |
chat_container = st.container()
|
| 177 |
with chat_container:
|
| 178 |
for message in st.session_state.chat_history:
|
| 179 |
with st.chat_message(message["role"]):
|
| 180 |
render_chat_message(message)
|
| 181 |
+
# Chat input
|
| 182 |
user_question = st.chat_input("Ask a question about your data:")
|
| 183 |
if user_question:
|
| 184 |
handle_userinput(user_question, st.session_state.dfs)
|
| 185 |
+
# Update chat container immediately after processing the input
|
| 186 |
chat_container.empty()
|
| 187 |
with chat_container:
|
| 188 |
for message in st.session_state.chat_history:
|
| 189 |
with st.chat_message(message["role"]):
|
| 190 |
render_chat_message(message)
|
| 191 |
|
| 192 |
+
# --- Reports Tab ---
|
| 193 |
with tab_reports:
|
| 194 |
st.title("Reports")
|
| 195 |
+
st.write("Filter by product to generate a report")
|
| 196 |
+
# Reload the data (or use the existing one) for reports
|
| 197 |
+
df_report = fetch_data()
|
| 198 |
+
if df_report is not None and not df_report.empty:
|
| 199 |
+
product_names = df_report["product"].unique().tolist() if "product" in df_report.columns else []
|
| 200 |
+
selected_products = st.multiselect("Select Product(s)", product_names, default=product_names)
|
| 201 |
+
if st.button("Apply Filters and Generate Report"):
|
| 202 |
+
filtered_df = df_report.copy()
|
| 203 |
+
if selected_products:
|
| 204 |
+
filtered_df = filtered_df[filtered_df["product"].isin(selected_products)]
|
| 205 |
+
st.write("Filtered DataFrame Preview:")
|
| 206 |
+
with st.expander("Preview"):
|
| 207 |
+
st.dataframe(filtered_df.head())
|
| 208 |
+
with st.spinner("Generating Report, Please Wait...."):
|
| 209 |
+
prompt = (
|
| 210 |
+
"You are an expert business analyst. Analyze the following data and generate a comprehensive and insightful business report "
|
| 211 |
+
"including key performance indicators and recommendations.\n\nData:\n" + str(filtered_df.to_json(orient='records'))
|
| 212 |
+
)
|
| 213 |
+
response = generateResponse(prompt, filtered_df)
|
| 214 |
+
# Display the report text; adjust according to your response format.
|
| 215 |
+
st.markdown(response.get("value", "No report generated."))
|
| 216 |
+
else:
|
| 217 |
+
st.error("No data available for reports.")
|
| 218 |
|
| 219 |
+
# --- Sidebar Options ---
|
| 220 |
with st.sidebar:
|
| 221 |
st.subheader("Options")
|
| 222 |
if st.button("Reload Data"):
|