Spaces:
Running
Running
Rami
commited on
Commit
·
50fd384
1
Parent(s):
5f8b3ec
Data table tunned
Browse files
app.py
CHANGED
@@ -2,11 +2,17 @@ import streamlit as st
|
|
2 |
import os
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
|
|
5 |
import google.generativeai as genai
|
6 |
from io import StringIO
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Configure Genai Key
|
9 |
-
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
|
|
10 |
|
11 |
# Function to load Google Gemini Model and provide queries as response
|
12 |
def get_gemini_response(question, prompt):
|
@@ -36,12 +42,116 @@ id,product_name,category,price,stock_quantity,supplier,last_restock_date
|
|
36 |
df['last_restock_date'] = pd.to_datetime(df['last_restock_date'], errors='coerce')
|
37 |
return df
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
# Function to execute pandas query
|
40 |
def execute_pandas_query(df, query):
|
41 |
try:
|
42 |
-
#
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
return result
|
46 |
except Exception as e:
|
47 |
st.error(f"An error occurred: {e}")
|
@@ -56,32 +166,35 @@ prompt = [
|
|
56 |
|
57 |
Examples:
|
58 |
- How many products do we have in total?
|
59 |
-
The pandas operation will be: len()
|
60 |
- What are all the products in the Electronics category?
|
61 |
-
The pandas operation will be: query("category == 'Electronics'")
|
|
|
|
|
62 |
|
63 |
-
The pandas operation should be a valid Python expression that can be applied to a DataFrame 'df'.
|
|
|
64 |
"""
|
65 |
]
|
66 |
|
67 |
# Streamlit App
|
68 |
-
st.set_page_config(page_title="Department Store Analytics", layout="wide")
|
69 |
|
70 |
# Load data
|
71 |
df = load_data()
|
72 |
|
73 |
# Sidebar for user input
|
74 |
-
st.sidebar.title("Department Store
|
75 |
question = st.sidebar.text_area("Enter your question:", key="input")
|
76 |
submit = st.sidebar.button("Ask Me")
|
77 |
|
78 |
# Main content area
|
79 |
-
st.title("Department Store Dashboard")
|
80 |
|
81 |
if submit:
|
82 |
-
with st.spinner("Generating
|
83 |
pandas_query = get_gemini_response(question, prompt)
|
84 |
-
st.code(pandas_query, language="python")
|
85 |
|
86 |
result_df = execute_pandas_query(df, pandas_query)
|
87 |
|
@@ -92,7 +205,7 @@ if submit:
|
|
92 |
st.subheader("Data Table")
|
93 |
st.dataframe(result_df)
|
94 |
|
95 |
-
# Create visualizations based on the data
|
96 |
st.subheader("Data Visualizations")
|
97 |
|
98 |
col1, col2 = st.columns(2)
|
@@ -126,4 +239,9 @@ else:
|
|
126 |
|
127 |
# Footer
|
128 |
st.sidebar.markdown("---")
|
129 |
-
st.sidebar.
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
import pandas as pd
|
4 |
import plotly.express as px
|
5 |
+
import ast
|
6 |
import google.generativeai as genai
|
7 |
from io import StringIO
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
|
13 |
# Configure Genai Key
|
14 |
+
# genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))
|
15 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
16 |
|
17 |
# Function to load Google Gemini Model and provide queries as response
|
18 |
def get_gemini_response(question, prompt):
|
|
|
42 |
df['last_restock_date'] = pd.to_datetime(df['last_restock_date'], errors='coerce')
|
43 |
return df
|
44 |
|
45 |
+
# # Function to execute pandas query
|
46 |
+
# def execute_pandas_query(df, query):
|
47 |
+
# try:
|
48 |
+
# # This is a very simple and unsafe way to execute queries.
|
49 |
+
# # In a real application, you'd need to parse the SQL and translate it to pandas operations.
|
50 |
+
# result = eval(f"df.{query}")
|
51 |
+
# return result
|
52 |
+
# except Exception as e:
|
53 |
+
# st.error(f"An error occurred: {e}")
|
54 |
+
# return pd.DataFrame()
|
55 |
+
|
56 |
+
# # Define Your Prompt
|
57 |
+
# prompt = [
|
58 |
+
# """
|
59 |
+
# You are an expert in converting English questions to pandas DataFrame operations!
|
60 |
+
# The DataFrame 'df' has the following columns:
|
61 |
+
# id, product_name, category, price, stock_quantity, supplier, last_restock_date.
|
62 |
+
|
63 |
+
# Examples:
|
64 |
+
# - How many products do we have in total?
|
65 |
+
# The pandas operation will be: len()
|
66 |
+
# - What are all the products in the Electronics category?
|
67 |
+
# The pandas operation will be: query("category == 'Electronics'")
|
68 |
+
|
69 |
+
# The pandas operation should be a valid Python expression that can be applied to a DataFrame 'df'.
|
70 |
+
# """
|
71 |
+
# ]
|
72 |
+
|
73 |
+
# Function to execute pandas query
|
74 |
+
# def execute_pandas_query(df, query):
|
75 |
+
# try:
|
76 |
+
# # Remove any 'df.' prefixes from the query
|
77 |
+
# query = query.replace('df.', '')
|
78 |
+
|
79 |
+
# # Execute the query
|
80 |
+
# if query.startswith('query'):
|
81 |
+
# # For filtering operations
|
82 |
+
# result = df.query(query.split('(', 1)[1].rsplit(')', 1)[0].strip('"\''))
|
83 |
+
# elif query.startswith('groupby'):
|
84 |
+
# # For groupby operations
|
85 |
+
# group_col, agg_func = query.split('.', 2)[1:]
|
86 |
+
# result = eval(f"df.groupby('{group_col}').{agg_func}")
|
87 |
+
# else:
|
88 |
+
# # For other operations
|
89 |
+
# result = eval(f"df.{query}")
|
90 |
+
|
91 |
+
# return result
|
92 |
+
# except Exception as e:
|
93 |
+
# st.error(f"An error occurred: {e}")
|
94 |
+
# return pd.DataFrame()
|
95 |
+
|
96 |
+
# # Define Your Prompt
|
97 |
+
# prompt = [
|
98 |
+
# """
|
99 |
+
# You are an expert in converting English questions to pandas DataFrame operations!
|
100 |
+
# The DataFrame 'df' has the following columns:
|
101 |
+
# id, product_name, category, price, stock_quantity, supplier, last_restock_date.
|
102 |
+
|
103 |
+
# Examples:
|
104 |
+
# - How many products do we have in total?
|
105 |
+
# The pandas operation will be: shape[0]
|
106 |
+
# - What are all the products in the Electronics category?
|
107 |
+
# The pandas operation will be: query("category == 'Electronics'")
|
108 |
+
# - What is the average price of products in each category?
|
109 |
+
# The pandas operation will be: groupby('category').mean()['price']
|
110 |
+
|
111 |
+
# The pandas operation should be a valid Python expression that can be applied to a DataFrame without the 'df.' prefix.
|
112 |
+
# """
|
113 |
+
# ]
|
114 |
+
|
115 |
+
# Function to safely evaluate a string as a Python expression
|
116 |
+
def safe_eval(expr, df):
|
117 |
+
try:
|
118 |
+
# Parse the expression
|
119 |
+
parsed = ast.parse(expr, mode='eval')
|
120 |
+
|
121 |
+
# Define allowed names
|
122 |
+
allowed_names = {
|
123 |
+
'df': df,
|
124 |
+
'query': df.query,
|
125 |
+
'groupby': df.groupby,
|
126 |
+
'mean': pd.DataFrame.mean,
|
127 |
+
'sum': pd.DataFrame.sum,
|
128 |
+
'count': pd.DataFrame.count,
|
129 |
+
'max': pd.DataFrame.max,
|
130 |
+
'min': pd.DataFrame.min
|
131 |
+
}
|
132 |
+
|
133 |
+
# Evaluate the expression
|
134 |
+
return eval(compile(parsed, '<string>', 'eval'), allowed_names)
|
135 |
+
except Exception as e:
|
136 |
+
st.error(f"Error in query execution: {e}")
|
137 |
+
return pd.DataFrame()
|
138 |
+
|
139 |
# Function to execute pandas query
|
140 |
def execute_pandas_query(df, query):
|
141 |
try:
|
142 |
+
# Remove any 'df.' prefixes from the query
|
143 |
+
query = query.replace('df.', '')
|
144 |
+
|
145 |
+
# Execute the query
|
146 |
+
result = safe_eval(query, df)
|
147 |
+
|
148 |
+
# Convert result to DataFrame if it's not already
|
149 |
+
if not isinstance(result, pd.DataFrame):
|
150 |
+
if isinstance(result, pd.Series):
|
151 |
+
result = result.to_frame()
|
152 |
+
else:
|
153 |
+
result = pd.DataFrame({'Result': [result]})
|
154 |
+
|
155 |
return result
|
156 |
except Exception as e:
|
157 |
st.error(f"An error occurred: {e}")
|
|
|
166 |
|
167 |
Examples:
|
168 |
- How many products do we have in total?
|
169 |
+
The pandas operation will be: len(df)
|
170 |
- What are all the products in the Electronics category?
|
171 |
+
The pandas operation will be: df.query("category == 'Electronics'")
|
172 |
+
- What is the average price of products in each category?
|
173 |
+
The pandas operation will be: df.groupby('category')['price'].mean()
|
174 |
|
175 |
+
The pandas operation should be a valid Python expression that can be applied to a DataFrame named 'df'.
|
176 |
+
Always include 'df.' at the beginning of your operations unless you're using a function like len().
|
177 |
"""
|
178 |
]
|
179 |
|
180 |
# Streamlit App
|
181 |
+
st.set_page_config(page_title="AutomatiX - Department Store Analytics", layout="wide")
|
182 |
|
183 |
# Load data
|
184 |
df = load_data()
|
185 |
|
186 |
# Sidebar for user input
|
187 |
+
st.sidebar.title("AutomatiX - Department Store Chat Interface")
|
188 |
question = st.sidebar.text_area("Enter your question:", key="input")
|
189 |
submit = st.sidebar.button("Ask Me")
|
190 |
|
191 |
# Main content area
|
192 |
+
st.title("AutomatiX - Department Store Dashboard")
|
193 |
|
194 |
if submit:
|
195 |
+
with st.spinner("Generating and Fetching the data..."):
|
196 |
pandas_query = get_gemini_response(question, prompt)
|
197 |
+
# st.code(pandas_query, language="python")
|
198 |
|
199 |
result_df = execute_pandas_query(df, pandas_query)
|
200 |
|
|
|
205 |
st.subheader("Data Table")
|
206 |
st.dataframe(result_df)
|
207 |
|
208 |
+
# # Create visualizations based on the data
|
209 |
st.subheader("Data Visualizations")
|
210 |
|
211 |
col1, col2 = st.columns(2)
|
|
|
239 |
|
240 |
# Footer
|
241 |
st.sidebar.markdown("---")
|
242 |
+
st.sidebar.info("You can ask questions like:\n"
|
243 |
+
"1.What are all the products in the Electronics category?\n"
|
244 |
+
"2.What is the average price of products in each category?\n"
|
245 |
+
"3.Which products have a stock quantity less than 30?\n"
|
246 |
+
"4.What are the top 5 most expensive products?")
|
247 |
+
st.sidebar.warning("CopyRights@AutomatiX - Powered by Streamlit and Google Gemini")
|