Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from pydantic import BaseModel
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
class ValidationStatus(BaseModel):
|
| 8 |
is_valid: bool
|
|
@@ -47,14 +48,45 @@ def parse_execution_results_to_dataframe(execution_results):
|
|
| 47 |
print(f"Error parsing results: {e}")
|
| 48 |
return None
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def generate_sql_query(api_key, user_query):
|
| 51 |
"""Generate SQL query from natural language using GROQ API"""
|
| 52 |
try:
|
| 53 |
if not api_key:
|
| 54 |
-
return "Error: Please enter your GROQ API key", "", "", "", None, ""
|
| 55 |
|
| 56 |
if not user_query:
|
| 57 |
-
return "Error: Please enter a query description", "", "", "", None, ""
|
| 58 |
|
| 59 |
client = Groq(api_key=api_key)
|
| 60 |
|
|
@@ -130,21 +162,25 @@ Optimization Notes:
|
|
| 130 |
# Convert execution results to DataFrame
|
| 131 |
results_df = parse_execution_results_to_dataframe(sql_query_generation.execution_results)
|
| 132 |
|
|
|
|
|
|
|
|
|
|
| 133 |
return (
|
| 134 |
sql_query_generation.query,
|
| 135 |
metadata,
|
| 136 |
sql_query_generation.table_schema,
|
| 137 |
sql_query_generation.sample_data,
|
| 138 |
results_df,
|
|
|
|
| 139 |
validation_text
|
| 140 |
)
|
| 141 |
|
| 142 |
except Exception as e:
|
| 143 |
error_msg = f"Error: {str(e)}"
|
| 144 |
-
return error_msg, "", "", "", None, ""
|
| 145 |
|
| 146 |
# Create Gradio interface
|
| 147 |
-
with gr.Blocks(title="SQL Query Generator", theme=gr.themes.
|
| 148 |
gr.Markdown(
|
| 149 |
"""
|
| 150 |
# 🗄️ Natural Language to SQL Query Generator
|
|
@@ -216,15 +252,24 @@ with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Ocean()) as demo:
|
|
| 216 |
)
|
| 217 |
|
| 218 |
with gr.Row():
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
generate_btn.click(
|
| 230 |
fn=generate_sql_query,
|
|
@@ -235,6 +280,7 @@ with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Ocean()) as demo:
|
|
| 235 |
schema_output,
|
| 236 |
sample_data_output,
|
| 237 |
execution_output,
|
|
|
|
| 238 |
validation_output
|
| 239 |
]
|
| 240 |
)
|
|
@@ -246,14 +292,16 @@ with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Ocean()) as demo:
|
|
| 246 |
1. Enter your GROQ API key (get one from [console.groq.com](https://console.groq.com))
|
| 247 |
2. Type your natural language query description
|
| 248 |
3. Click "Generate SQL Query" to see the results
|
|
|
|
| 249 |
|
| 250 |
The app will provide:
|
| 251 |
- A validated SQL query
|
| 252 |
- Table schema and sample data
|
| 253 |
- Execution results in Excel-style table format
|
|
|
|
| 254 |
- Optimization suggestions
|
| 255 |
"""
|
| 256 |
)
|
| 257 |
|
| 258 |
if __name__ == "__main__":
|
| 259 |
-
demo.launch(share=
|
|
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
class ValidationStatus(BaseModel):
|
| 9 |
is_valid: bool
|
|
|
|
| 48 |
print(f"Error parsing results: {e}")
|
| 49 |
return None
|
| 50 |
|
| 51 |
+
def create_excel_file(df):
|
| 52 |
+
"""Create an Excel file from DataFrame and return the file path"""
|
| 53 |
+
if df is None or df.empty:
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
# Create a BytesIO buffer
|
| 58 |
+
output = io.BytesIO()
|
| 59 |
+
|
| 60 |
+
# Write DataFrame to Excel
|
| 61 |
+
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
| 62 |
+
df.to_excel(writer, index=False, sheet_name='Query Results')
|
| 63 |
+
|
| 64 |
+
# Get the worksheet
|
| 65 |
+
worksheet = writer.sheets['Query Results']
|
| 66 |
+
|
| 67 |
+
# Auto-adjust column widths
|
| 68 |
+
for idx, col in enumerate(df.columns):
|
| 69 |
+
max_length = max(
|
| 70 |
+
df[col].astype(str).apply(len).max(),
|
| 71 |
+
len(str(col))
|
| 72 |
+
)
|
| 73 |
+
worksheet.column_dimensions[chr(65 + idx)].width = min(max_length + 2, 50)
|
| 74 |
+
|
| 75 |
+
output.seek(0)
|
| 76 |
+
return output.getvalue()
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Error creating Excel file: {e}")
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
def generate_sql_query(api_key, user_query):
|
| 83 |
"""Generate SQL query from natural language using GROQ API"""
|
| 84 |
try:
|
| 85 |
if not api_key:
|
| 86 |
+
return "Error: Please enter your GROQ API key", "", "", "", None, None, ""
|
| 87 |
|
| 88 |
if not user_query:
|
| 89 |
+
return "Error: Please enter a query description", "", "", "", None, None, ""
|
| 90 |
|
| 91 |
client = Groq(api_key=api_key)
|
| 92 |
|
|
|
|
| 162 |
# Convert execution results to DataFrame
|
| 163 |
results_df = parse_execution_results_to_dataframe(sql_query_generation.execution_results)
|
| 164 |
|
| 165 |
+
# Create Excel file
|
| 166 |
+
excel_file = create_excel_file(results_df) if results_df is not None else None
|
| 167 |
+
|
| 168 |
return (
|
| 169 |
sql_query_generation.query,
|
| 170 |
metadata,
|
| 171 |
sql_query_generation.table_schema,
|
| 172 |
sql_query_generation.sample_data,
|
| 173 |
results_df,
|
| 174 |
+
excel_file,
|
| 175 |
validation_text
|
| 176 |
)
|
| 177 |
|
| 178 |
except Exception as e:
|
| 179 |
error_msg = f"Error: {str(e)}"
|
| 180 |
+
return error_msg, "", "", "", None, None, ""
|
| 181 |
|
| 182 |
# Create Gradio interface
|
| 183 |
+
with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Soft()) as demo:
|
| 184 |
gr.Markdown(
|
| 185 |
"""
|
| 186 |
# 🗄️ Natural Language to SQL Query Generator
|
|
|
|
| 252 |
)
|
| 253 |
|
| 254 |
with gr.Row():
|
| 255 |
+
with gr.Column():
|
| 256 |
+
execution_output = gr.Dataframe(
|
| 257 |
+
label="📊 Execution Results",
|
| 258 |
+
headers=None,
|
| 259 |
+
datatype="str",
|
| 260 |
+
row_count=10,
|
| 261 |
+
col_count=None,
|
| 262 |
+
wrap=True,
|
| 263 |
+
interactive=False
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
with gr.Row():
|
| 267 |
+
with gr.Column():
|
| 268 |
+
download_btn = gr.File(
|
| 269 |
+
label="📥 Download Excel File",
|
| 270 |
+
type="binary",
|
| 271 |
+
interactive=False
|
| 272 |
+
)
|
| 273 |
|
| 274 |
generate_btn.click(
|
| 275 |
fn=generate_sql_query,
|
|
|
|
| 280 |
schema_output,
|
| 281 |
sample_data_output,
|
| 282 |
execution_output,
|
| 283 |
+
download_btn,
|
| 284 |
validation_output
|
| 285 |
]
|
| 286 |
)
|
|
|
|
| 292 |
1. Enter your GROQ API key (get one from [console.groq.com](https://console.groq.com))
|
| 293 |
2. Type your natural language query description
|
| 294 |
3. Click "Generate SQL Query" to see the results
|
| 295 |
+
4. Download the execution results as an Excel file
|
| 296 |
|
| 297 |
The app will provide:
|
| 298 |
- A validated SQL query
|
| 299 |
- Table schema and sample data
|
| 300 |
- Execution results in Excel-style table format
|
| 301 |
+
- Downloadable Excel file with results
|
| 302 |
- Optimization suggestions
|
| 303 |
"""
|
| 304 |
)
|
| 305 |
|
| 306 |
if __name__ == "__main__":
|
| 307 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|