Spaces:
Sleeping
Sleeping
Rami
commited on
Commit
·
3998c95
1
Parent(s):
976d3ed
AutomatiX-Deport added
Browse files- app.py +134 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import psycopg2 as pgsql
|
4 |
+
import pandas as pd
|
5 |
+
import plotly.express as px
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import google.generativeai as genai
|
8 |
+
|
9 |
+
# Load environment variables
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Configure Genai Key
|
13 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
14 |
+
|
15 |
+
# Function to load Google Gemini Model and provide queries as response
|
16 |
+
def get_gemini_response(question, prompt):
|
17 |
+
model = genai.GenerativeModel('gemini-pro')
|
18 |
+
response = model.generate_content([prompt[0], question])
|
19 |
+
return response.text.strip()
|
20 |
+
|
21 |
+
# Function to retrieve query from the database
|
22 |
+
def read_sql_query(sql, db_params):
|
23 |
+
try:
|
24 |
+
conn = pgsql.connect(**db_params)
|
25 |
+
cur = conn.cursor()
|
26 |
+
cur.execute(sql)
|
27 |
+
rows = cur.fetchall()
|
28 |
+
colnames = [desc[0] for desc in cur.description] if cur.description else []
|
29 |
+
conn.commit()
|
30 |
+
cur.close()
|
31 |
+
conn.close()
|
32 |
+
df = pd.DataFrame(rows, columns=colnames)
|
33 |
+
|
34 |
+
# Convert 'price' column to numeric if it exists
|
35 |
+
if 'price' in df.columns:
|
36 |
+
df['price'] = pd.to_numeric(df['price'], errors='coerce')
|
37 |
+
|
38 |
+
return df
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"An error occurred: {e}")
|
41 |
+
return pd.DataFrame()
|
42 |
+
|
43 |
+
# Define your PostgreSQL connection parameters
|
44 |
+
db_params = {
|
45 |
+
'dbname': 'GeminiPro',
|
46 |
+
'user': 'postgres',
|
47 |
+
'password': 'root',
|
48 |
+
'host': 'localhost',
|
49 |
+
'port': 5432
|
50 |
+
}
|
51 |
+
|
52 |
+
# Define Your Prompt
|
53 |
+
prompt = [
|
54 |
+
"""
|
55 |
+
You are an expert in converting English questions to SQL queries!
|
56 |
+
The SQL database has a table named 'department_store' with the following columns:
|
57 |
+
id, product_name, category, price, stock_quantity, supplier, last_restock_date.
|
58 |
+
|
59 |
+
Examples:
|
60 |
+
- How many products do we have in total?
|
61 |
+
The SQL command will be: SELECT COUNT(*) FROM department_store;
|
62 |
+
- What are all the products in the Electronics category?
|
63 |
+
The SQL command will be: SELECT * FROM department_store WHERE category = 'Electronics';
|
64 |
+
|
65 |
+
The SQL code should not include backticks and should not start with the word 'SQL'.
|
66 |
+
"""
|
67 |
+
]
|
68 |
+
|
69 |
+
# Streamlit App
|
70 |
+
st.set_page_config(page_title="AutomatiX - Department Store Analytics", layout="wide")
|
71 |
+
|
72 |
+
# Sidebar for user input
|
73 |
+
st.sidebar.title("AutomatiX - Department Store Chat Interface")
|
74 |
+
question = st.sidebar.text_area("Enter your question:", key="input")
|
75 |
+
submit = st.sidebar.button("Ask Me")
|
76 |
+
|
77 |
+
# Main content area
|
78 |
+
st.title("AutomatiX - Department Store Dashboard")
|
79 |
+
|
80 |
+
if submit:
|
81 |
+
with st.spinner("Generating and fetching data..."):
|
82 |
+
sql_query = get_gemini_response(question, prompt)
|
83 |
+
# st.code(sql_query, language="sql")
|
84 |
+
|
85 |
+
df = read_sql_query(sql_query, db_params)
|
86 |
+
|
87 |
+
if not df.empty:
|
88 |
+
st.success("Query executed successfully!")
|
89 |
+
|
90 |
+
# Display data in a table
|
91 |
+
st.subheader("Data Table")
|
92 |
+
st.dataframe(df)
|
93 |
+
|
94 |
+
# Create visualizations based on the data
|
95 |
+
st.subheader("Data Visualizations")
|
96 |
+
|
97 |
+
col1, col2 = st.columns(2)
|
98 |
+
|
99 |
+
with col1:
|
100 |
+
if 'price' in df.columns and df['price'].notna().any():
|
101 |
+
fig = px.histogram(df, x='price', title='Price Distribution')
|
102 |
+
st.plotly_chart(fig, use_container_width=True)
|
103 |
+
|
104 |
+
if 'category' in df.columns:
|
105 |
+
category_counts = df['category'].value_counts()
|
106 |
+
fig = px.pie(values=category_counts.values, names=category_counts.index, title='Products by Category')
|
107 |
+
st.plotly_chart(fig, use_container_width=True)
|
108 |
+
|
109 |
+
with col2:
|
110 |
+
if 'last_restock_date' in df.columns:
|
111 |
+
df['last_restock_date'] = pd.to_datetime(df['last_restock_date'], errors='coerce')
|
112 |
+
df['restock_month'] = df['last_restock_date'].dt.to_period('M')
|
113 |
+
restock_counts = df['restock_month'].value_counts().sort_index()
|
114 |
+
fig = px.line(x=restock_counts.index.astype(str), y=restock_counts.values, title='Restocking Trend')
|
115 |
+
st.plotly_chart(fig, use_container_width=True)
|
116 |
+
|
117 |
+
if 'product_name' in df.columns and 'price' in df.columns and df['price'].notna().any():
|
118 |
+
top_prices = df.sort_values('price', ascending=False).head(10)
|
119 |
+
fig = px.bar(top_prices, x='product_name', y='price', title='Top 10 Most Expensive Products')
|
120 |
+
st.plotly_chart(fig, use_container_width=True)
|
121 |
+
else:
|
122 |
+
st.warning("No data returned from the query.")
|
123 |
+
|
124 |
+
else:
|
125 |
+
st.info("Enter a question and click 'Ask Me' to get started!")
|
126 |
+
|
127 |
+
# Footer
|
128 |
+
st.sidebar.markdown("---")
|
129 |
+
st.sidebar.info("You can ask questions like:\n"
|
130 |
+
"1.What are all the products in the Electronics category?\n"
|
131 |
+
"2.What is the average price of products in each category?\n"
|
132 |
+
"3.Which products have a stock quantity less than 30?\n"
|
133 |
+
"4.What are the top 5 most expensive products?")
|
134 |
+
st.sidebar.warning("CopyRights@AutomatiX - Powered by Streamlit and Google Gemini")
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
google-generativeai
|
3 |
+
python-dotenv
|
4 |
+
psycopg2-binary
|
5 |
+
mysql-connector-python
|
6 |
+
pandas
|