Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ import json
|
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
from datetime import datetime
|
|
|
|
|
|
|
| 7 |
|
| 8 |
class ValidationStatus(BaseModel):
|
| 9 |
is_valid: bool
|
|
@@ -188,6 +190,9 @@ Return a complete JSON object with all fields populated, including explanation a
|
|
| 188 |
|
| 189 |
def export_query(sql_query, schema, sample_data):
|
| 190 |
"""Export query with schema and sample data as a complete SQL file"""
|
|
|
|
|
|
|
|
|
|
| 191 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 192 |
content = f"""-- Generated SQL Query
|
| 193 |
-- Timestamp: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
|
@@ -207,10 +212,28 @@ def export_query(sql_query, schema, sample_data):
|
|
| 207 |
-- QUERY
|
| 208 |
{sql_query}
|
| 209 |
"""
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
# Create Gradio interface with enhanced features
|
| 213 |
-
with gr.Blocks(title="SQL Query Generator Pro", theme=gr.themes.
|
| 214 |
gr.Markdown(
|
| 215 |
"""
|
| 216 |
# 🗄️ Natural Language to SQL Query Generator Pro
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
from datetime import datetime
|
| 7 |
+
import tempfile
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
class ValidationStatus(BaseModel):
|
| 11 |
is_valid: bool
|
|
|
|
| 190 |
|
| 191 |
def export_query(sql_query, schema, sample_data):
|
| 192 |
"""Export query with schema and sample data as a complete SQL file"""
|
| 193 |
+
if not sql_query or sql_query.startswith("Error"):
|
| 194 |
+
return None
|
| 195 |
+
|
| 196 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 197 |
content = f"""-- Generated SQL Query
|
| 198 |
-- Timestamp: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
|
|
|
| 212 |
-- QUERY
|
| 213 |
{sql_query}
|
| 214 |
"""
|
| 215 |
+
|
| 216 |
+
# Create a temporary file with proper naming
|
| 217 |
+
temp_file = tempfile.NamedTemporaryFile(
|
| 218 |
+
mode='w',
|
| 219 |
+
suffix='.sql',
|
| 220 |
+
prefix=f'sql_query_{timestamp}_',
|
| 221 |
+
delete=False
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
try:
|
| 225 |
+
temp_file.write(content)
|
| 226 |
+
temp_file.flush()
|
| 227 |
+
temp_file.close()
|
| 228 |
+
return temp_file.name
|
| 229 |
+
except Exception as e:
|
| 230 |
+
print(f"Error creating file: {e}")
|
| 231 |
+
if os.path.exists(temp_file.name):
|
| 232 |
+
os.unlink(temp_file.name)
|
| 233 |
+
return None
|
| 234 |
|
| 235 |
# Create Gradio interface with enhanced features
|
| 236 |
+
with gr.Blocks(title="SQL Query Generator Pro", theme=gr.themes.Soft()) as demo:
|
| 237 |
gr.Markdown(
|
| 238 |
"""
|
| 239 |
# 🗄️ Natural Language to SQL Query Generator Pro
|