File size: 20,338 Bytes
985fc10 8f087b3 985fc10 |
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
import os
import pandas as pd
import sqlite3
import numpy as np
import json
import re
from typing import List, Dict, Tuple
from groq import Groq
import gradio as gr
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings('ignore')
# ------------------------------
# โ
GROQ API KEY FROM ENVIRONMENT
# ------------------------------
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
print("โ ๏ธ WARNING: GROQ_API_KEY environment variable not set!")
print("Please add your Groq API key to your Hugging Face Space secrets.")
print("For demo purposes, the app will continue but API calls will fail.")
GROQ_API_KEY = "dummy-key-for-demo"
# ------------------------------
# SQL Converter Using Groq API
# ------------------------------
class EnhancedNL2SQLConverter:
def __init__(self, model_name: str = "llama-3.3-70b-versatile"):
self.model_name = model_name
self.client = None
try:
if GROQ_API_KEY and GROQ_API_KEY != "dummy-key-for-demo":
self.client = Groq(api_key=GROQ_API_KEY)
print(f"โ
Successfully initialized Groq client with model: {self.model_name}")
else:
print("โ ๏ธ Groq client not initialized - API key missing")
except Exception as e:
print(f"โ Error initializing Groq client: {str(e)}")
self.client = None
self.default_schema = """
Table: employees
Columns:
- id (INTEGER) PRIMARY KEY
- name (TEXT) NOT NULL
- department (TEXT)
- salary (REAL)
- hire_date (TEXT)
- manager_id (INTEGER)
"""
def generate_sql(self, query: str, schema: str = None) -> str:
try:
if not self.client:
return "ERROR: Groq API client not initialized. Please check your API key."
schema_to_use = schema or self.default_schema
system_prompt = """You are an expert SQL query generator. Convert natural language questions to SQL queries based on the provided database schema.
Rules:
1. Only return the SQL query, nothing else
2. Use proper SQL syntax
3. Be precise with column names and table names
4. Use appropriate WHERE clauses, JOINs, and aggregations as needed
5. For date comparisons, use proper date format
6. Don't include explanations, just the SQL query"""
user_prompt = f"""Database Schema:
{schema_to_use}
Natural Language Question: {query}
Generate the SQL query:"""
chat_completion = self.client.chat.completions.create(
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
model=self.model_name,
temperature=0.1,
max_tokens=200
)
sql_query = chat_completion.choices[0].message.content.strip()
return self._clean_sql(sql_query)
except Exception as e:
print(f"Error generating SQL: {str(e)}")
return f"ERROR: Could not generate SQL query - {str(e)}"
def _clean_sql(self, sql: str) -> str:
sql = sql.strip()
sql = re.sub(r'```sql\n?', '', sql)
sql = re.sub(r'```\n?', '', sql)
sql = re.sub(r'^["\']|["\']$', '', sql)
sql = sql.rstrip(';')
sql_keywords = ['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'CREATE', 'DROP', 'ALTER']
if not any(sql.upper().startswith(keyword) for keyword in sql_keywords):
for keyword in sql_keywords:
if keyword in sql.upper():
sql = sql[sql.upper().find(keyword):]
break
return sql
# ------------------------------
# SQL Evaluator & Test Database
# ------------------------------
class SQLEvaluator:
def __init__(self):
self.db_path = "test_database.db"
self.setup_test_database()
def setup_test_database(self):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
department TEXT,
salary REAL,
hire_date TEXT,
manager_id INTEGER
)''')
sample_data = [
(1, 'Alice Johnson', 'Engineering', 75000, '2022-01-15', None),
(2, 'Bob Smith', 'Sales', 65000, '2021-06-20', None),
(3, 'Charlie Brown', 'Engineering', 80000, '2020-03-10', 1),
(4, 'Diana Prince', 'HR', 60000, '2023-02-28', None),
(5, 'Eve Wilson', 'Sales', 70000, '2022-11-05', 2),
(6, 'Frank Miller', 'Engineering', 85000, '2019-08-12', 1),
(7, 'Grace Lee', 'Marketing', 55000, '2023-01-20', None),
(8, 'Henry Davis', 'Engineering', 72000, '2022-07-30', 1)
]
cursor.executemany('''
INSERT OR REPLACE INTO employees (id, name, department, salary, hire_date, manager_id)
VALUES (?, ?, ?, ?, ?, ?)''', sample_data)
conn.commit()
conn.close()
print("โ
Test database initialized successfully")
def execute_sql(self, sql_query: str) -> Tuple[bool, any]:
try:
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute(sql_query)
if sql_query.strip().upper().startswith('SELECT'):
results = cursor.fetchall()
columns = [description[0] for description in cursor.description]
conn.close()
return True, {'columns': columns, 'data': results}
else:
conn.commit()
conn.close()
return True, "Query executed successfully"
except Exception as e:
return False, str(e)
# ------------------------------
# Initialize components
# ------------------------------
try:
converter = EnhancedNL2SQLConverter()
evaluator = SQLEvaluator()
print("โ
Application components initialized successfully")
except Exception as e:
print(f"โ Error initializing components: {str(e)}")
converter = None
evaluator = SQLEvaluator()
# ------------------------------
# Enhanced UI Functions
# ------------------------------
def process_nl_query(nl_query: str) -> Tuple[str, str, str]:
"""Process natural language query and return SQL + results"""
if not nl_query.strip():
return "", "", "โ ๏ธ Please enter a natural language query."
try:
if not converter:
return "", "", "โ Error: SQL converter not initialized. Please check API configuration."
generated_sql = converter.generate_sql(nl_query)
if generated_sql.startswith("ERROR"):
return generated_sql, "", "โ Failed to generate SQL query. Please check your API key."
success, result = evaluator.execute_sql(generated_sql)
if success and isinstance(result, dict):
df = pd.DataFrame(result['data'], columns=result['columns'])
if len(df) == 0:
formatted_output = "No results found."
else:
formatted_output = df.to_string(index=False)
return generated_sql, formatted_output, "โ
Query executed successfully!"
elif success:
return generated_sql, str(result), "โ
Query executed successfully!"
else:
return generated_sql, "", f"โ Error executing query: {result}"
except Exception as e:
return "", "", f"โ Unexpected error: {str(e)}"
def get_sample_queries():
return [
"Show all employees in the Engineering department",
"Find employees with salary greater than 70000",
"List all employees hired after 2022",
"Count employees by department",
"Show the highest paid employee in each department",
"Find employees who don't have a manager",
"Show average salary by department"
]
# ------------------------------
# Beautiful Custom CSS
# ------------------------------
custom_css = """
/* Main container styling */
.gradio-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
min-height: 100vh;
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
/* Header styling */
.header-container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(20px);
border-radius: 20px;
padding: 2rem;
margin-bottom: 2rem;
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
}
/* Card styling */
.card {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(20px);
border-radius: 16px;
padding: 1.5rem;
margin: 1rem 0;
border: 1px solid rgba(255, 255, 255, 0.3);
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
transition: all 0.3s ease;
}
.card:hover {
transform: translateY(-2px);
box-shadow: 0 12px 40px rgba(31, 38, 135, 0.25);
}
/* Input styling */
.gr-textbox {
border-radius: 12px !important;
border: 2px solid rgba(103, 126, 234, 0.3) !important;
background: rgba(255, 255, 255, 0.9) !important;
transition: all 0.3s ease !important;
}
.gr-textbox:focus {
border-color: #667eea !important;
box-shadow: 0 0 0 3px rgba(103, 126, 234, 0.1) !important;
transform: scale(1.02);
}
/* Button styling */
.gr-button {
background: linear-gradient(45deg, #667eea, #764ba2) !important;
border: none !important;
border-radius: 12px !important;
padding: 12px 24px !important;
font-weight: 600 !important;
color: white !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 15px rgba(103, 126, 234, 0.4) !important;
}
.gr-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 8px 25px rgba(103, 126, 234, 0.6) !important;
}
.sample-btn {
background: linear-gradient(45deg, #f093fb, #f5576c) !important;
margin: 0.25rem !important;
font-size: 0.9rem !important;
padding: 8px 16px !important;
}
.sample-btn:hover {
background: linear-gradient(45deg, #f5576c, #f093fb) !important;
}
/* Results area styling */
.results-container {
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
border-radius: 16px;
padding: 1.5rem;
margin-top: 1rem;
}
/* Status indicators */
.status-success {
color: #10b981 !important;
font-weight: 600 !important;
}
.status-error {
color: #ef4444 !important;
font-weight: 600 !important;
}
.status-warning {
color: #f59e0b !important;
font-weight: 600 !important;
}
/* Schema box */
.schema-box {
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
border-radius: 12px;
padding: 1rem;
font-family: 'Monaco', 'Consolas', monospace;
border-left: 4px solid #f59e0b;
}
/* Animation keyframes */
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.fade-in {
animation: fadeInUp 0.6s ease-out;
}
/* Responsive design */
@media (max-width: 768px) {
.gradio-container {
padding: 1rem;
}
.card {
padding: 1rem;
margin: 0.5rem 0;
}
}
/* Loading spinner */
.loading {
display: inline-block;
width: 20px;
height: 20px;
border: 3px solid rgba(255,255,255,.3);
border-radius: 50%;
border-top-color: #fff;
animation: spin 1s ease-in-out infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
"""
# ------------------------------
# Enhanced Gradio Interface
# ------------------------------
with gr.Blocks(css=custom_css, title="AI-Powered NL2SQL Converter", theme=gr.themes.Glass()) as iface:
# Header Section
with gr.Row(elem_classes="header-container fade-in"):
gr.HTML("""
<div style="text-align: center; color: white;">
<h1 style="font-size: 3rem; margin-bottom: 0.5rem; background: linear-gradient(45deg, #fff, #f0f0f0); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
๐ AI-Powered NL2SQL Converter
</h1>
<p style="font-size: 1.2rem; opacity: 0.9; margin-bottom: 1rem;">
Transform natural language into powerful SQL queries using Groq's advanced AI
</p>
<div style="display: flex; justify-content: center; gap: 2rem; margin-top: 1rem;">
<div style="text-align: center;">
<div style="font-size: 2rem;">๐ค</div>
<div style="font-size: 0.9rem; opacity: 0.8;">AI-Powered</div>
</div>
<div style="text-align: center;">
<div style="font-size: 2rem;">โก</div>
<div style="font-size: 0.9rem; opacity: 0.8;">Lightning Fast</div>
</div>
<div style="text-align: center;">
<div style="font-size: 2rem;">๐ฏ</div>
<div style="font-size: 0.9rem; opacity: 0.8;">Precise Results</div>
</div>
</div>
</div>
""")
# Database Schema Section
with gr.Row(elem_classes="card fade-in"):
gr.HTML("""
<div class="schema-box">
<h3 style="color: #d97706; margin-bottom: 1rem;">๐ Database Schema</h3>
<div style="background: rgba(255,255,255,0.7); padding: 1rem; border-radius: 8px;">
<strong>employees</strong> table:<br>
โข <code>id</code> (INTEGER) - Primary Key<br>
โข <code>name</code> (TEXT) - Employee Name<br>
โข <code>department</code> (TEXT) - Department<br>
โข <code>salary</code> (REAL) - Salary Amount<br>
โข <code>hire_date</code> (TEXT) - Hiring Date<br>
โข <code>manager_id</code> (INTEGER) - Manager Reference
</div>
</div>
""")
# Main Input Section
with gr.Row(elem_classes="card fade-in"):
with gr.Column(scale=3):
nl_input = gr.Textbox(
label="๐ฌ Ask your question in plain English",
placeholder="e.g., Show me all engineers earning more than $75,000",
lines=3,
elem_classes="main-input"
)
with gr.Row():
submit_btn = gr.Button(
"๐ฎ Generate & Execute SQL",
variant="primary",
size="lg",
elem_classes="main-button"
)
clear_btn = gr.Button(
"๐๏ธ Clear",
variant="secondary",
size="lg"
)
with gr.Column(scale=2):
gr.HTML("<h3 style='color: #667eea; margin-bottom: 1rem;'>๐ฏ Try These Examples</h3>")
sample_queries = get_sample_queries()
for i, query in enumerate(sample_queries):
sample_btn = gr.Button(
f"๐ก {query}",
variant="secondary",
size="sm",
elem_classes="sample-btn"
)
sample_btn.click(
lambda q=query: q,
outputs=nl_input
)
# Results Section
with gr.Row(elem_classes="results-container fade-in"):
with gr.Column():
gr.HTML("<h3 style='color: #6366f1; margin-bottom: 1rem;'>๐ Generated SQL Query</h3>")
sql_output = gr.Code(
label="",
language="sql",
lines=4,
interactive=False,
elem_classes="sql-output"
)
status_output = gr.HTML(
"<div style='padding: 1rem; text-align: center; font-size: 1.1rem;'>Ready to process your query! ๐</div>"
)
with gr.Row(elem_classes="card fade-in"):
gr.HTML("<h3 style='color: #059669; margin-bottom: 1rem;'>๐ Query Results</h3>")
results_output = gr.Code(
label="",
lines=12,
interactive=False,
elem_classes="results-output"
)
# Footer Section
with gr.Row(elem_classes="card fade-in"):
gr.HTML("""
<div style="text-align: center; padding: 1rem;">
<h3 style="color: #667eea; margin-bottom: 1rem;">๐ About This Application</h3>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; margin-top: 1rem;">
<div style="background: linear-gradient(135deg, #667eea, #764ba2); color: white; padding: 1rem; border-radius: 12px;">
<h4>๐ค AI Model</h4>
<p>Powered by Groq's Llama3-70B for intelligent SQL generation</p>
</div>
<div style="background: linear-gradient(135deg, #f093fb, #f5576c); color: white; padding: 1rem; border-radius: 12px;">
<h4>๐พ Database</h4>
<p>SQLite with sample employee data for testing and learning</p>
</div>
<div style="background: linear-gradient(135deg, #a8edea, #fed6e3); color: #374151; padding: 1rem; border-radius: 12px;">
<h4>โจ Features</h4>
<p>Natural language processing, SQL execution, and formatted results</p>
</div>
</div>
<div style="margin-top: 2rem; padding: 1rem; background: rgba(103, 126, 234, 0.1); border-radius: 12px;">
<h4 style="color: #667eea;">๐ก Pro Tips for Better Results</h4>
<ul style="text-align: left; display: inline-block; color: #4b5563;">
<li>Be specific and clear in your questions</li>
<li>Use column names mentioned in the schema</li>
<li>Try the sample queries to understand the format</li>
<li>Use natural language - no need for technical jargon</li>
</ul>
</div>
</div>
""")
# Event Handlers with Enhanced Feedback
def enhanced_process(query):
if not query.strip():
return "", "<div class='status-warning'>โ ๏ธ Please enter a question first!</div>", ""
# Show loading state
loading_html = "<div class='status-info'>๐ Processing your query... <span class='loading'></span></div>"
try:
sql, results, status = process_nl_query(query)
# Enhanced status formatting
if "successfully" in status.lower():
status_html = f"<div class='status-success'>{status}</div>"
elif "error" in status.lower() or "failed" in status.lower():
status_html = f"<div class='status-error'>{status}</div>"
else:
status_html = f"<div class='status-warning'>{status}</div>"
return sql, status_html, results
except Exception as e:
return "", f"<div class='status-error'>โ Unexpected error: {str(e)}</div>", ""
def clear_all():
return "", "", "<div style='padding: 1rem; text-align: center; font-size: 1.1rem;'>Ready to process your query! ๐</div>", ""
# Connect events
submit_btn.click(
fn=enhanced_process,
inputs=[nl_input],
outputs=[sql_output, status_output, results_output]
)
nl_input.submit(
fn=enhanced_process,
inputs=[nl_input],
outputs=[sql_output, status_output, results_output]
)
clear_btn.click(
fn=clear_all,
outputs=[nl_input, sql_output, status_output, results_output]
)
# Launch the app
if __name__ == "__main__":
print("๐ Launching Enhanced NL2SQL Application...")
iface.launch(
server_name="0.0.0.0",
server_port=7860,
share=True,
show_error=True
) |