-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
181 lines (152 loc) · 6.18 KB
/
Copy pathapp.py
File metadata and controls
181 lines (152 loc) · 6.18 KB
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
import streamlit as st
import pandas as pd
import os
import json
from engine.query_processor import LogicEngine
from assets.charts import ChartLibrary
from google import genai
# Page Config
st.set_page_config(page_title="Text to Visual", page_icon="📊", layout="wide")
# Custom CSS for Premium Look
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap');
html, body, [class*="css"] {
font-family: 'Inter', sans-serif;
}
.stApp {
background-color: #0e0e0e;
color: white;
}
.main {
background: radial-gradient(circle at top left, #6c63ff22, transparent 40%),
radial-gradient(circle at bottom right, #1a1a2e, #0e0e0e);
}
.stTextInput > div > div > input {
background-color: #1c1c2e !important;
color: white !important;
border: 1px solid #6c63ff !important;
border-radius: 20px !important;
padding: 10px 20px !important;
}
.stButton > button {
background: linear-gradient(135deg, #6c63ff, #48cfad) !important;
color: white !important;
border-radius: 30px !important;
border: none !important;
padding: 10px 30px !important;
font-weight: 600 !important;
text-transform: uppercase;
letter-spacing: 1px;
transition: all 0.3s ease;
}
.stButton > button:hover {
transform: scale(1.05);
box-shadow: 0 0 20px #6c63ff66;
}
.metric-card {
background: #1c1c2e;
padding: 20px;
border-radius: 15px;
border-left: 5px solid #6c63ff;
margin-bottom: 20px;
}
h1, h2, h3 {
color: white !important;
}
</style>
""", unsafe_allow_html=True)
# Helper function for Gemini
def call_gemini(prompt):
api_key = st.session_state.get("api_key") or os.getenv("GOOGLE_API_KEY")
if not api_key:
return json.dumps({
"error": "Missing API Key. Please provide it in the sidebar."
})
try:
client = genai.Client(api_key=api_key)
response = client.models.generate_content(
model="gemini-3.1-flash-lite",
contents=prompt
)
return response.text
except Exception as e:
return json.dumps({"error": str(e)})
# Sidebar
with st.sidebar:
st.markdown("## 📊 Text to Visual")
st.markdown("---")
st.markdown("### 🔑 API Key")
api_key_input = st.text_input("Google Gemini API Key", type="password", placeholder="Paste your API key here")
if api_key_input:
st.session_state.api_key = api_key_input
st.success("API Key saved!")
st.markdown("---")
st.markdown("### 📁 Upload Data")
uploaded_file = st.file_uploader("Upload any CSV file", type="csv")
if uploaded_file:
df = pd.read_csv(uploaded_file)
st.success(f"✅ Loaded **{len(df):,}** rows")
st.session_state.df = df
st.session_state.engine = LogicEngine(df)
st.markdown("---")
st.markdown("### 🗂️ Columns Detected")
for col in df.columns:
st.markdown(f"- `{col}`")
else:
st.info("Upload a CSV to begin exploration.")
# Main Layout
st.title("📊 Text to Visual")
st.subheader("Ask questions in plain English. Get instant charts.")
if 'df' in st.session_state:
col1, col2 = st.columns([1, 3])
with col1:
st.markdown("### 🔍 Ask a Question")
query = st.text_input("What do you want to explore?", placeholder="e.g. Top 10 rows by sales", key="query_input")
submit = st.button("Generate Visualization")
st.markdown("---")
st.markdown("### 📊 Dataset Overview")
st.write(f"**Rows:** {len(st.session_state.df):,}")
st.write(f"**Columns:** {len(st.session_state.df.columns)}")
if st.checkbox("Show Raw Data (first 10 rows)"):
st.dataframe(st.session_state.df.head(10))
with col2:
if submit and query:
with st.spinner("Analyzing with Gemini..."):
plan = st.session_state.engine.process_query(query, call_gemini)
if isinstance(plan, dict) and "error" in plan:
st.error(plan["error"])
else:
try:
result_df = st.session_state.engine.execute_logic(plan)
chart_type = plan.get('chart_type', 'bar')
x = plan.get('x')
y = plan.get('y')
title = plan.get('title', 'Generated Visual')
st.markdown(f"### {title}")
if chart_type == 'bar':
fig = ChartLibrary.bar_chart(result_df, x, y, title)
elif chart_type == 'line':
fig = ChartLibrary.line_chart(result_df, x, y, title)
elif chart_type == 'scatter':
fig = ChartLibrary.scatter_plot(result_df, x, y, title)
elif chart_type == 'pie':
fig = ChartLibrary.pie_chart(result_df, x, y, title)
elif chart_type == 'histogram':
fig = ChartLibrary.histogram(result_df, x, title)
else:
fig = ChartLibrary.bar_chart(result_df, x, y, title)
st.plotly_chart(fig, use_container_width=True)
with st.expander("🔧 Technical Details"):
st.code(plan['pandas_code'], language='python')
except Exception as e:
st.error(f"Logic Error: {str(e)}")
else:
st.info("👋 Upload a dataset and enter a query to generate a visualization.")
else:
st.warning("👈 Upload a CSV file in the sidebar to start exploring.")
st.markdown("""
<div style='text-align: center; color: #444; padding: 20px; margin-top: 40px;'>
Built by Jayan
</div>
""", unsafe_allow_html=True)