Krish_Script commited on
Commit
faa0e0c
·
1 Parent(s): 5e68afe

feat: AI SQL assistant — Streamlit UI with LLaMA-3.3-70B

Browse files
Files changed (2) hide show
  1. app.py +351 -0
  2. requirements.txt +10 -3
app.py ADDED
@@ -0,0 +1,351 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ app_hf.py -- Hugging Face Spaces version of the Streamlit UI
3
+ Calls Groq directly (no Flask layer needed on HF Spaces).
4
+
5
+ Deploy:
6
+ 1. Create Space at huggingface.co -> New Space -> Streamlit -> Public
7
+ 2. Add GROQ_API_KEY to Space Secrets (Settings -> Repository Secrets)
8
+ 3. Copy this file as app.py to your Space repo
9
+ 4. Copy requirements_hf.txt as requirements.txt
10
+ """
11
+
12
+ import os
13
+ import sys
14
+ import json
15
+ import time
16
+ import re
17
+ import numpy as np
18
+ import pandas as pd
19
+ import streamlit as st
20
+ from pathlib import Path
21
+ from datetime import datetime
22
+ from groq import Groq
23
+ from dotenv import load_dotenv
24
+
25
+ load_dotenv()
26
+
27
+ # -------------------------------------------------------
28
+ # Config
29
+ # -------------------------------------------------------
30
+
31
+ MODEL = "llama-3.3-70b-versatile"
32
+ TEMPERATURE = 0
33
+ MAX_TOKENS = 300
34
+ TIMEOUT_SEC = 30
35
+
36
+ STRATEGY_LABELS = {
37
+ "zero_shot" : "Zero-Shot",
38
+ "few_shot" : "Few-Shot",
39
+ "chain_of_thought": "Chain-of-Thought",
40
+ }
41
+
42
+ SQL_KEYWORDS = [
43
+ "SELECT","FROM","WHERE","AND","OR","NOT","IN","EXISTS",
44
+ "JOIN","LEFT","RIGHT","INNER","OUTER","ON","AS","DISTINCT",
45
+ "ORDER","BY","GROUP","HAVING","LIMIT","COUNT","SUM","AVG",
46
+ "MAX","MIN","ASC","DESC","UNION","INTERSECT","EXCEPT",
47
+ ]
48
+
49
+ SYSTEM_PROMPTS = {
50
+ "zero_shot": (
51
+ "You are an expert SQL assistant. Given a database schema and a natural "
52
+ "language question, return ONLY the SQL query. Do not explain. Do not use "
53
+ "markdown. Do not add any text before or after the SQL. "
54
+ "Use DISTINCT when the question implies unique or different values. "
55
+ "Use foreign key relationships between tables when writing JOINs."
56
+ ),
57
+ "few_shot": (
58
+ "You are an expert SQL assistant. Study the examples below, then generate "
59
+ "SQL for the new question. Return ONLY the SQL query. Do not explain. "
60
+ "Do not use markdown. Do not add any text before or after the SQL. "
61
+ "Use DISTINCT when the question implies unique or different values. "
62
+ "Use foreign key relationships between tables when writing JOINs."
63
+ ),
64
+ "chain_of_thought": (
65
+ "You are a SQL expert. Think through the query step by step, then write "
66
+ "the final SQL. Rules: Use DISTINCT when the question implies uniqueness. "
67
+ "Use foreign key relationships between tables when writing JOINs. "
68
+ "Always include all relevant columns. "
69
+ "Your final answer MUST start with SELECT and contain only SQL -- "
70
+ "no explanation after the query."
71
+ ),
72
+ }
73
+
74
+ # -------------------------------------------------------
75
+ # Page config
76
+ # -------------------------------------------------------
77
+
78
+ st.set_page_config(
79
+ page_title = "AI SQL Assistant",
80
+ page_icon = "🛢",
81
+ layout = "wide",
82
+ initial_sidebar_state = "expanded",
83
+ )
84
+
85
+ # -------------------------------------------------------
86
+ # Session state
87
+ # -------------------------------------------------------
88
+
89
+ if "history" not in st.session_state:
90
+ st.session_state.history = []
91
+
92
+ # -------------------------------------------------------
93
+ # Groq client (cached)
94
+ # -------------------------------------------------------
95
+
96
+ @st.cache_resource
97
+ def get_groq_client():
98
+ api_key = os.getenv("GROQ_API_KEY")
99
+ if not api_key:
100
+ return None
101
+ return Groq(api_key=api_key)
102
+
103
+
104
+ # -------------------------------------------------------
105
+ # SQL helpers
106
+ # -------------------------------------------------------
107
+
108
+ def clean_sql(raw: str) -> str:
109
+ if not raw or not raw.strip():
110
+ return ""
111
+ text = raw.strip()
112
+ # Strip markdown fences
113
+ match = re.search(r'```(?:sql|SQL)?\s*\n?(.*?)\n?```', text, re.DOTALL)
114
+ if match:
115
+ text = match.group(1).strip()
116
+ # Strip prefix explanation
117
+ upper = text.upper()
118
+ idx = upper.find('SELECT')
119
+ if idx > 0:
120
+ prefix = text[:idx].strip()
121
+ if prefix:
122
+ text = text[idx:].strip()
123
+ # Uppercase keywords
124
+ for kw in SQL_KEYWORDS:
125
+ text = re.sub(rf'\b{kw}\b', kw, text, flags=re.IGNORECASE)
126
+ # Collapse whitespace
127
+ text = re.sub(r'\s+', ' ', text).strip()
128
+ # Remove trailing semicolon
129
+ text = text.rstrip(';').rstrip()
130
+ return text
131
+
132
+
133
+ def is_valid_sql(sql: str) -> bool:
134
+ try:
135
+ import sqlglot
136
+ sqlglot.parse_one(sql)
137
+ return True
138
+ except Exception:
139
+ return 'SELECT' in sql.upper() and 'FROM' in sql.upper()
140
+
141
+
142
+ def build_prompt(nl_query: str, schema: dict, strategy: str) -> str:
143
+ tables = ", ".join(schema.get("tables", []))
144
+ columns = ", ".join(schema.get("columns", [])[:15])
145
+ schema_text = ""
146
+ if tables:
147
+ schema_text += f"Tables: {tables}\n"
148
+ if columns:
149
+ schema_text += f"Columns: {columns}"
150
+
151
+ if strategy == "chain_of_thought":
152
+ return (
153
+ f"Database schema:\n{schema_text}\n\n"
154
+ f"Question: {nl_query}\n\n"
155
+ f"Think step by step:\n"
156
+ f"1. Which tables do I need?\n"
157
+ f"2. Which columns are relevant?\n"
158
+ f"3. What SQL pattern fits?\n"
159
+ f"4. Does the question imply DISTINCT?\n\n"
160
+ f"Now write ONLY the SQL query. Start with SELECT:"
161
+ )
162
+ else:
163
+ return (
164
+ f"Database schema:\n{schema_text}\n\n"
165
+ f"Question: {nl_query}\n\n"
166
+ f"SQL:"
167
+ )
168
+
169
+
170
+ def generate_sql_direct(nl_query: str, schema: dict, strategy: str) -> dict:
171
+ client = get_groq_client()
172
+ if not client:
173
+ return {"error": "GROQ_API_KEY not set in Space Secrets"}
174
+
175
+ system = SYSTEM_PROMPTS.get(strategy, SYSTEM_PROMPTS["zero_shot"])
176
+ prompt = build_prompt(nl_query, schema, strategy)
177
+
178
+ t0 = time.time()
179
+ try:
180
+ resp = client.chat.completions.create(
181
+ model = MODEL,
182
+ temperature = TEMPERATURE,
183
+ max_tokens = MAX_TOKENS,
184
+ messages = [
185
+ {"role": "system", "content": system},
186
+ {"role": "user", "content": prompt},
187
+ ],
188
+ )
189
+ raw = resp.choices[0].message.content.strip()
190
+ except Exception as e:
191
+ return {"error": str(e)}
192
+
193
+ latency = round(time.time() - t0, 3)
194
+ sql = clean_sql(raw)
195
+ valid = is_valid_sql(sql) if sql else False
196
+
197
+ return {
198
+ "sql" : sql,
199
+ "raw" : raw,
200
+ "valid" : valid,
201
+ "strategy" : strategy,
202
+ "model" : MODEL,
203
+ "latency_s": latency,
204
+ "rag_used" : False,
205
+ "retrieved_pairs": [],
206
+ }
207
+
208
+
209
+ # -------------------------------------------------------
210
+ # Sidebar
211
+ # -------------------------------------------------------
212
+
213
+ with st.sidebar:
214
+ st.title("🛢 AI SQL Assistant")
215
+ st.caption("RAG-powered NL to SQL generation")
216
+ st.divider()
217
+
218
+ client = get_groq_client()
219
+ if client:
220
+ st.success("Groq API connected")
221
+ st.caption(f"Model: {MODEL}")
222
+ else:
223
+ st.error("GROQ_API_KEY not set")
224
+ st.caption("Add it to Space Secrets")
225
+
226
+ st.divider()
227
+
228
+ strategy_key = st.selectbox(
229
+ "Prompt Strategy",
230
+ options = list(STRATEGY_LABELS.keys()),
231
+ format_func = lambda k: STRATEGY_LABELS[k],
232
+ )
233
+
234
+ st.divider()
235
+ st.caption(f"Session queries: {len(st.session_state.history)}")
236
+ st.caption("Built with Groq + LLaMA-3.3-70B")
237
+
238
+
239
+ # -------------------------------------------------------
240
+ # Tabs
241
+ # -------------------------------------------------------
242
+
243
+ tab_generate, tab_history = st.tabs(["Generate", "History"])
244
+
245
+
246
+ # ===============================================================
247
+ # TAB 1 -- GENERATE
248
+ # ===============================================================
249
+
250
+ with tab_generate:
251
+ st.header("Generate SQL from Natural Language")
252
+
253
+ with st.expander("Optional: provide database schema hint", expanded=False):
254
+ col_t, col_c = st.columns(2)
255
+ with col_t:
256
+ tables_input = st.text_input("Tables (comma-separated)",
257
+ placeholder="orders, customers, products")
258
+ with col_c:
259
+ columns_input = st.text_input("Columns (comma-separated)",
260
+ placeholder="id, name, amount, region")
261
+
262
+ schema = {}
263
+ if tables_input or columns_input:
264
+ schema = {
265
+ "tables" : [t.strip() for t in tables_input.split(",") if t.strip()],
266
+ "columns": [c.strip() for c in columns_input.split(",") if c.strip()],
267
+ }
268
+
269
+ nl_query = st.text_area(
270
+ "Natural language question",
271
+ placeholder="e.g. Find the top 5 customers by total spend in 2024",
272
+ height=100,
273
+ )
274
+
275
+ generate_clicked = st.button("Generate SQL", type="primary")
276
+
277
+ if generate_clicked:
278
+ if not nl_query.strip():
279
+ st.warning("Please enter a question.")
280
+ elif not client:
281
+ st.error("GROQ_API_KEY not set in Space Secrets.")
282
+ else:
283
+ with st.spinner("Generating SQL..."):
284
+ result = generate_sql_direct(
285
+ nl_query = nl_query.strip(),
286
+ schema = schema,
287
+ strategy = strategy_key,
288
+ )
289
+
290
+ if result and "error" not in result:
291
+ st.divider()
292
+ st.subheader("Result")
293
+
294
+ m1, m2, m3 = st.columns(3)
295
+ m1.metric("Valid SQL", "Yes" if result.get("valid") else "No")
296
+ m2.metric("Strategy", STRATEGY_LABELS.get(result.get("strategy",""), ""))
297
+ m3.metric("Latency", f"{result.get('latency_s', 0):.2f}s")
298
+
299
+ st.subheader("Generated SQL")
300
+ st.code(result.get("sql", ""), language="sql")
301
+
302
+ st.session_state.history.append({
303
+ "timestamp": datetime.now().strftime("%H:%M:%S"),
304
+ "nl_query" : nl_query.strip(),
305
+ "sql" : result.get("sql", ""),
306
+ "strategy" : result.get("strategy", ""),
307
+ "valid" : result.get("valid", False),
308
+ "latency_s": result.get("latency_s", 0),
309
+ "rag_used" : False,
310
+ })
311
+ elif result and "error" in result:
312
+ st.error(f"Error: {result['error']}")
313
+
314
+
315
+ # ===============================================================
316
+ # TAB 2 -- HISTORY
317
+ # ===============================================================
318
+
319
+ with tab_history:
320
+ st.header("Session History")
321
+
322
+ if not st.session_state.history:
323
+ st.info("No queries yet. Use the Generate tab to start.")
324
+ else:
325
+ col_exp, col_dl = st.columns([1, 1])
326
+ with col_exp:
327
+ if st.button("Clear history"):
328
+ st.session_state.history = []
329
+ st.rerun()
330
+ with col_dl:
331
+ history_json = json.dumps(st.session_state.history, indent=2)
332
+ st.download_button(
333
+ label = "Export history (JSON)",
334
+ data = history_json,
335
+ file_name = f"sql_history_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
336
+ mime = "application/json",
337
+ )
338
+
339
+ st.divider()
340
+ for i, entry in enumerate(reversed(st.session_state.history), 1):
341
+ valid_tag = "Valid" if entry.get("valid") else "Invalid"
342
+ with st.expander(
343
+ f"[{entry['timestamp']}] {entry['nl_query'][:70]} | {valid_tag}",
344
+ expanded = i == 1,
345
+ ):
346
+ st.write(f"**Question:** {entry['nl_query']}")
347
+ st.code(entry["sql"], language="sql")
348
+ c1, c2, c3 = st.columns(3)
349
+ c1.caption(f"Strategy: {STRATEGY_LABELS.get(entry['strategy'], entry['strategy'])}")
350
+ c2.caption(f"Valid: {'Yes' if entry.get('valid') else 'No'}")
351
+ c3.caption(f"Latency: {entry.get('latency_s', 0):.2f}s")
requirements.txt CHANGED
@@ -1,3 +1,10 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
 
 
 
1
+ # requirements.txt for Hugging Face Spaces
2
+ # Lighter version -- no Flask, no local eval tools
3
+
4
+ groq==1.1.2
5
+ streamlit==1.49.1
6
+ python-dotenv==1.2.2
7
+ sqlglot==30.2.1
8
+ numpy==2.4.4
9
+ pandas==3.0.2
10
+ requests==2.33.1