File size: 10,792 Bytes
d8ff681
 
 
 
 
 
82fb5aa
 
d8ff681
1029219
d8ff681
 
 
 
 
 
 
 
 
 
 
 
1029219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ff681
1029219
 
 
 
 
 
 
 
82fb5aa
1029219
 
 
 
 
 
 
d8ff681
1029219
 
 
 
 
82fb5aa
1029219
 
 
 
 
 
 
 
 
 
 
 
 
 
9910f72
1029219
9910f72
1029219
 
 
 
 
 
9910f72
1029219
 
 
 
 
9910f72
1029219
 
 
 
 
82fb5aa
1029219
82fb5aa
1029219
 
 
 
 
 
 
 
 
 
 
 
d8ff681
1029219
 
d8ff681
 
1029219
 
d8ff681
1029219
 
 
 
 
 
d8ff681
1029219
d8ff681
 
 
1029219
d8ff681
1029219
d8ff681
 
1029219
d8ff681
 
 
1029219
 
 
d8ff681
1029219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ff681
1029219
 
 
 
 
d8ff681
1029219
 
 
d8ff681
1029219
 
 
 
 
d8ff681
1029219
 
 
d8ff681
1029219
d8ff681
1029219
 
 
d8ff681
1029219
 
 
 
d8ff681
 
 
1029219
 
 
 
 
 
 
d8ff681
1029219
d8ff681
 
1029219
 
d8ff681
1029219
 
d8ff681
1029219
d8ff681
1029219
d8ff681
1029219
 
 
 
d8ff681
 
 
1029219
d8ff681
82fb5aa
1029219
d8ff681
1029219
d8ff681
 
 
82fb5aa
1029219
d8ff681
 
 
 
1029219
 
 
d8ff681
 
 
1029219
 
d8ff681
 
 
1029219
 
d8ff681
 
 
1029219
 
 
 
 
d8ff681
1029219
d8ff681
1029219
 
 
d8ff681
82fb5aa
 
 
1029219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82fb5aa
d8ff681
1029219
d8ff681
1029219
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
import gradio as gr
from groq import Groq
from pydantic import BaseModel
import json
import sqlite3
import pandas as pd
from datetime import datetime, timedelta
import random

# Pydantic models for structured output
class ValidationStatus(BaseModel):
    is_valid: bool
    syntax_errors: list[str]

class SQLQueryGeneration(BaseModel):
    query: str
    query_type: str
    tables_used: list[str]
    estimated_complexity: str
    execution_notes: list[str]
    validation_status: ValidationStatus

# Sample data generators
def generate_sample_customers(count=10):
    """Generate sample customer data"""
    first_names = ["Alice", "Bob", "Carol", "David", "Emma", "Frank", "Grace", "Henry", "Ivy", "Jack"]
    last_names = ["Johnson", "Smith", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis", "Rodriguez", "Martinez"]
    
    customers = []
    for i in range(1, count + 1):
        fname = random.choice(first_names)
        lname = random.choice(last_names)
        customers.append({
            'customer_id': i,
            'name': f"{fname} {lname}",
            'email': f"{fname.lower()}{i}@example.com"
        })
    return customers

def generate_sample_orders(customer_count=10, order_count=20):
    """Generate sample order data"""
    orders = []
    base_date = datetime.now()
    
    for i in range(1, order_count + 1):
        days_ago = random.randint(0, 60)
        order_date = (base_date - timedelta(days=days_ago)).strftime('%Y-%m-%d')
        
        orders.append({
            'order_id': 100 + i,
            'customer_id': random.randint(1, customer_count),
            'total_amount': random.choice([250, 350, 450, 600, 800, 1200, 1500, 300]),
            'order_date': order_date
        })
    return orders

def generate_sample_products(count=15):
    """Generate sample product data"""
    products = []
    categories = ["Electronics", "Clothing", "Home", "Sports", "Books"]
    product_names = ["Widget", "Gadget", "Tool", "Item", "Device"]
    
    for i in range(1, count + 1):
        products.append({
            'product_id': i,
            'product_name': f"{random.choice(product_names)} {i}",
            'category': random.choice(categories),
            'price': round(random.uniform(10, 500), 2),
            'stock_quantity': random.randint(0, 100)
        })
    return products

def create_database_from_tables(tables_used):
    """Create SQLite database with sample data based on tables mentioned in query"""
    conn = sqlite3.connect(':memory:')
    cursor = conn.cursor()
    
    sample_data = {}
    
    # Generate data based on tables mentioned
    if 'customers' in tables_used:
        customers = generate_sample_customers(10)
        df_customers = pd.DataFrame(customers)
        df_customers.to_sql('customers', conn, index=False, if_exists='replace')
        sample_data['customers'] = df_customers
    
    if 'orders' in tables_used:
        orders = generate_sample_orders(10, 20)
        df_orders = pd.DataFrame(orders)
        df_orders.to_sql('orders', conn, index=False, if_exists='replace')
        sample_data['orders'] = df_orders
    
    if 'products' in tables_used:
        products = generate_sample_products(15)
        df_products = pd.DataFrame(products)
        df_products.to_sql('products', conn, index=False, if_exists='replace')
        sample_data['products'] = df_products
    
    return conn, sample_data

def execute_sql_on_sample_data(sql_query, conn):
    """Execute the generated SQL query on sample database"""
    try:
        df_result = pd.read_sql_query(sql_query, conn)
        return df_result, None
    except Exception as e:
        return None, str(e)

def process_nl_query(api_key, natural_query):
    """Main function to process natural language query"""
    if not api_key:
        return "❌ Please enter your Groq API key", "", "", ""
    
    if not natural_query:
        return "❌ Please enter a natural language query", "", "", ""
    
    try:
        # Initialize Groq client
        client = Groq(api_key=api_key)
        
        # Step 1: Generate SQL from natural language
        output_text = "## πŸ“‹ STEP-BY-STEP PROCESS\n\n"
        output_text += "### Step 1: Understanding User Intent\n"
        output_text += f"**User Query:** {natural_query}\n\n"
        
        # Call Groq API for SQL generation
        response = client.chat.completions.create(
            model="mixtral-8x7b-32768",
            messages=[
                {
                    "role": "system",
                    "content": "You are a SQL expert. Generate structured SQL queries from natural language descriptions with proper syntax validation and metadata. Use standard SQL syntax compatible with SQLite.",
                },
                {"role": "user", "content": natural_query},
            ],
            response_format={
                "type": "json_object"
            }
        )
        
        # Parse the response
        response_content = response.choices[0].message.content
        sql_data = json.loads(response_content)
        
        # Try to map to our Pydantic model
        try:
            sql_query_gen = SQLQueryGeneration(**sql_data)
        except:
            # If response doesn't match exact schema, create it manually
            sql_query_gen = SQLQueryGeneration(
                query=sql_data.get('query', ''),
                query_type=sql_data.get('query_type', 'SELECT'),
                tables_used=sql_data.get('tables_used', []),
                estimated_complexity=sql_data.get('estimated_complexity', 'medium'),
                execution_notes=sql_data.get('execution_notes', []),
                validation_status=ValidationStatus(
                    is_valid=sql_data.get('validation_status', {}).get('is_valid', True),
                    syntax_errors=sql_data.get('validation_status', {}).get('syntax_errors', [])
                )
            )
        
        # Step 2: Display Structured SQL Output
        output_text += "### Step 2: Generated Structured SQL\n\n"
        output_text += "```json\n"
        output_text += json.dumps(sql_query_gen.model_dump(), indent=2)
        output_text += "\n```\n\n"
        
        # Step 3: Generate Sample Database Tables
        output_text += "### Step 3: Auto-Generated Sample Database Tables\n\n"
        conn, sample_data = create_database_from_tables(sql_query_gen.tables_used)
        
        # Display sample tables
        for table_name, df in sample_data.items():
            output_text += f"**πŸ“Š Sample `{table_name}` Table:**\n\n"
            output_text += df.to_markdown(index=False)
            output_text += "\n\n"
        
        # Step 4: Execute SQL Query
        output_text += "### Step 4: Execute Generated SQL on Sample Tables\n\n"
        output_text += f"**SQL Query:**\n```sql\n{sql_query_gen.query}\n```\n\n"
        
        result_df, error = execute_sql_on_sample_data(sql_query_gen.query, conn)
        
        if error:
            output_text += f"❌ **Execution Error:** {error}\n"
            result_table = None
        else:
            output_text += "βœ… **Query executed successfully!**\n\n"
            output_text += "**πŸ“ˆ SQL Execution Result:**\n\n"
            output_text += result_df.to_markdown(index=False)
            result_table = result_df
        
        conn.close()
        
        # Format outputs for Gradio
        json_output = json.dumps(sql_query_gen.model_dump(), indent=2)
        
        if result_df is not None:
            result_display = result_df
        else:
            result_display = pd.DataFrame({"Error": [error]})
        
        return output_text, json_output, result_display, sql_query_gen.query
        
    except Exception as e:
        error_msg = f"❌ **Error:** {str(e)}\n\nPlease check your API key and query."
        return error_msg, "", pd.DataFrame(), ""

# Create Gradio Interface
with gr.Blocks(title="Natural Language to SQL Query Executor", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ” Natural Language to SQL Query Executor
    
    Convert natural language queries into SQL, generate sample data, and execute queries automatically!
    
    **Example queries to try:**
    - "Find all customers who made orders over $500 in the last 30 days, show their name, email, and total order amount"
    - "Show all products with stock quantity less than 10"
    - "List top 5 customers by total order amount"
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            api_key_input = gr.Textbox(
                label="πŸ”‘ Groq API Key",
                type="password",
                placeholder="Enter your Groq API key here...",
                info="Get your API key from https://console.groq.com"
            )
            
            query_input = gr.Textbox(
                label="πŸ’¬ Natural Language Query",
                placeholder="e.g., Find all customers who made orders over $500 in the last 30 days...",
                lines=3
            )
            
            submit_btn = gr.Button("πŸš€ Generate & Execute SQL", variant="primary", size="lg")
            
            gr.Markdown("### πŸ“ Generated SQL Query")
            sql_output = gr.Code(label="SQL Query", language="sql")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ“Š Process & Results")
            process_output = gr.Markdown()
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### 🎯 Structured JSON Output")
            json_output = gr.Code(label="JSON Response", language="json")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ“ˆ Query Execution Result")
            result_output = gr.Dataframe(
                label="Result Table",
                interactive=False
            )
    
    # Connect the button to the processing function
    submit_btn.click(
        fn=process_nl_query,
        inputs=[api_key_input, query_input],
        outputs=[process_output, json_output, result_output, sql_output]
    )
    
    gr.Markdown("""
    ---
    ### πŸ“– How it works:
    1. **Enter your Groq API key** - Required for SQL generation
    2. **Write your query in plain English** - Describe what data you want to find
    3. **Click Generate & Execute** - The system will:
       - Convert your query to SQL
       - Generate sample database tables
       - Execute the query
       - Show you the results
    
    ### 🎯 Features:
    - βœ… Natural language to SQL conversion
    - βœ… Automatic sample data generation
    - βœ… Query validation and metadata
    - βœ… SQL execution on sample data
    - βœ… Structured JSON output format
    """)

# Launch the app
if __name__ == "__main__":
    demo.launch()