Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ from pydantic import BaseModel
|
|
| 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,45 +47,14 @@ def parse_execution_results_to_dataframe(execution_results):
|
|
| 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,
|
| 87 |
|
| 88 |
if not user_query:
|
| 89 |
-
return "Error: Please enter a query description", "", "", "", None,
|
| 90 |
|
| 91 |
client = Groq(api_key=api_key)
|
| 92 |
|
|
@@ -102,18 +70,15 @@ After generating the SQL query, you must:
|
|
| 102 |
3. Execute the generated SQL query against the sample table
|
| 103 |
4. Display the SQL table structure and data clearly
|
| 104 |
5. Show the query execution results in a pipe-delimited table format
|
| 105 |
-
|
| 106 |
IMPORTANT: The execution_results field must contain a properly formatted table with:
|
| 107 |
- Header row with column names separated by pipes (|)
|
| 108 |
- A separator row with dashes
|
| 109 |
- Data rows with values separated by pipes (|)
|
| 110 |
-
|
| 111 |
Example format:
|
| 112 |
column1 | column2 | column3
|
| 113 |
--------|---------|--------
|
| 114 |
value1 | value2 | value3
|
| 115 |
value4 | value5 | value6
|
| 116 |
-
|
| 117 |
Always present your response in this order:
|
| 118 |
- Generated SQL query with syntax explanation
|
| 119 |
- Table schema (CREATE TABLE statement)
|
|
@@ -152,35 +117,29 @@ Always present your response in this order:
|
|
| 152 |
metadata = f"""Query Type: {sql_query_generation.query_type}
|
| 153 |
Tables Used: {', '.join(sql_query_generation.tables_used)}
|
| 154 |
Complexity: {sql_query_generation.estimated_complexity}
|
| 155 |
-
|
| 156 |
Execution Notes:
|
| 157 |
{chr(10).join(f"- {note}" for note in sql_query_generation.execution_notes)}
|
| 158 |
-
|
| 159 |
Optimization Notes:
|
| 160 |
{chr(10).join(f"- {note}" for note in sql_query_generation.optimization_notes)}"""
|
| 161 |
|
| 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,
|
| 181 |
|
| 182 |
# Create Gradio interface
|
| 183 |
-
with gr.Blocks(title="SQL Query Generator", theme=gr.themes.
|
| 184 |
gr.Markdown(
|
| 185 |
"""
|
| 186 |
# 🗄️ Natural Language to SQL Query Generator
|
|
@@ -252,24 +211,15 @@ with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Soft()) as demo:
|
|
| 252 |
)
|
| 253 |
|
| 254 |
with gr.Row():
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 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,7 +230,6 @@ with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Soft()) as demo:
|
|
| 280 |
schema_output,
|
| 281 |
sample_data_output,
|
| 282 |
execution_output,
|
| 283 |
-
download_btn,
|
| 284 |
validation_output
|
| 285 |
]
|
| 286 |
)
|
|
@@ -292,16 +241,14 @@ with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Soft()) as demo:
|
|
| 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=
|
|
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
class ValidationStatus(BaseModel):
|
| 8 |
is_valid: bool
|
|
|
|
| 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 |
|
|
|
|
| 70 |
3. Execute the generated SQL query against the sample table
|
| 71 |
4. Display the SQL table structure and data clearly
|
| 72 |
5. Show the query execution results in a pipe-delimited table format
|
|
|
|
| 73 |
IMPORTANT: The execution_results field must contain a properly formatted table with:
|
| 74 |
- Header row with column names separated by pipes (|)
|
| 75 |
- A separator row with dashes
|
| 76 |
- Data rows with values separated by pipes (|)
|
|
|
|
| 77 |
Example format:
|
| 78 |
column1 | column2 | column3
|
| 79 |
--------|---------|--------
|
| 80 |
value1 | value2 | value3
|
| 81 |
value4 | value5 | value6
|
|
|
|
| 82 |
Always present your response in this order:
|
| 83 |
- Generated SQL query with syntax explanation
|
| 84 |
- Table schema (CREATE TABLE statement)
|
|
|
|
| 117 |
metadata = f"""Query Type: {sql_query_generation.query_type}
|
| 118 |
Tables Used: {', '.join(sql_query_generation.tables_used)}
|
| 119 |
Complexity: {sql_query_generation.estimated_complexity}
|
|
|
|
| 120 |
Execution Notes:
|
| 121 |
{chr(10).join(f"- {note}" for note in sql_query_generation.execution_notes)}
|
|
|
|
| 122 |
Optimization Notes:
|
| 123 |
{chr(10).join(f"- {note}" for note in sql_query_generation.optimization_notes)}"""
|
| 124 |
|
| 125 |
# Convert execution results to DataFrame
|
| 126 |
results_df = parse_execution_results_to_dataframe(sql_query_generation.execution_results)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
| 128 |
return (
|
| 129 |
sql_query_generation.query,
|
| 130 |
metadata,
|
| 131 |
sql_query_generation.table_schema,
|
| 132 |
sql_query_generation.sample_data,
|
| 133 |
results_df,
|
|
|
|
| 134 |
validation_text
|
| 135 |
)
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
error_msg = f"Error: {str(e)}"
|
| 139 |
+
return error_msg, "", "", "", None, ""
|
| 140 |
|
| 141 |
# Create Gradio interface
|
| 142 |
+
with gr.Blocks(title="SQL Query Generator", theme=gr.themes.Ocean()) as demo:
|
| 143 |
gr.Markdown(
|
| 144 |
"""
|
| 145 |
# 🗄️ Natural Language to SQL Query Generator
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
with gr.Row():
|
| 214 |
+
execution_output = gr.Dataframe(
|
| 215 |
+
label="📊 Execution Results",
|
| 216 |
+
headers=None,
|
| 217 |
+
datatype="str",
|
| 218 |
+
row_count=10,
|
| 219 |
+
col_count=None,
|
| 220 |
+
wrap=True,
|
| 221 |
+
interactive=False
|
| 222 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
generate_btn.click(
|
| 225 |
fn=generate_sql_query,
|
|
|
|
| 230 |
schema_output,
|
| 231 |
sample_data_output,
|
| 232 |
execution_output,
|
|
|
|
| 233 |
validation_output
|
| 234 |
]
|
| 235 |
)
|
|
|
|
| 241 |
1. Enter your GROQ API key (get one from [console.groq.com](https://console.groq.com))
|
| 242 |
2. Type your natural language query description
|
| 243 |
3. Click "Generate SQL Query" to see the results
|
|
|
|
| 244 |
|
| 245 |
The app will provide:
|
| 246 |
- A validated SQL query
|
| 247 |
- Table schema and sample data
|
| 248 |
- Execution results in Excel-style table format
|
|
|
|
| 249 |
- Optimization suggestions
|
| 250 |
"""
|
| 251 |
)
|
| 252 |
|
| 253 |
if __name__ == "__main__":
|
| 254 |
+
demo.launch(share=True)
|