Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -87,12 +87,12 @@ def generate_generic_table_data(table_name, row_count=15):
|
|
| 87 |
'position': [random.choice(['Engineer', 'Manager', 'Analyst', 'Developer', 'Designer'])
|
| 88 |
for _ in range(row_count)]
|
| 89 |
},
|
| 90 |
-
'departments': {
|
| 91 |
'id': list(range(1, 6)),
|
| 92 |
'name': ['Engineering', 'Sales', 'Marketing', 'HR', 'Finance'],
|
| 93 |
'manager_id': [random.randint(1, 15) for _ in range(5)],
|
| 94 |
'budget': [random.randint(100000, 1000000) for _ in range(5)]
|
| 95 |
-
}
|
| 96 |
'books': {
|
| 97 |
'book_id': gen_id(),
|
| 98 |
'title': [f"Book Title {i}" for i in range(1, row_count + 1)],
|
|
@@ -230,11 +230,15 @@ def generate_generic_table_data(table_name, row_count=15):
|
|
| 230 |
}
|
| 231 |
|
| 232 |
# Return predefined schema if exists, otherwise create generic one
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
# Generic fallback for unknown tables
|
| 237 |
-
# Try to infer structure from table name
|
| 238 |
generic_data = {
|
| 239 |
f'{table_name}_id': gen_id(),
|
| 240 |
'name': gen_names(),
|
|
@@ -257,11 +261,11 @@ def create_database_from_tables(tables_used):
|
|
| 257 |
table_name = table.lower().strip()
|
| 258 |
|
| 259 |
# Generate appropriate sample data
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
table_dict =
|
| 265 |
|
| 266 |
df = pd.DataFrame(table_dict)
|
| 267 |
df.to_sql(table_name, conn, index=False, if_exists='replace')
|
|
@@ -371,7 +375,8 @@ def process_nl_query(api_key, natural_query):
|
|
| 371 |
# Display sample tables (show first 10 rows for readability)
|
| 372 |
for table_name, df in sample_data.items():
|
| 373 |
output_text += f"**📊 Sample `{table_name}` Table** ({len(df)} rows):\n\n"
|
| 374 |
-
|
|
|
|
| 375 |
if len(df) > 10:
|
| 376 |
output_text += f"\n\n*...and {len(df) - 10} more rows*"
|
| 377 |
output_text += "\n\n"
|
|
@@ -406,7 +411,7 @@ def process_nl_query(api_key, natural_query):
|
|
| 406 |
return error_msg, "", pd.DataFrame({"Error": [str(e)]}), ""
|
| 407 |
|
| 408 |
# Create Gradio Interface
|
| 409 |
-
with gr.Blocks(title="Natural Language to SQL Query Executor", theme=gr.themes.
|
| 410 |
gr.Markdown("""
|
| 411 |
# 🔍 Natural Language to SQL Query Executor
|
| 412 |
|
|
|
|
| 87 |
'position': [random.choice(['Engineer', 'Manager', 'Analyst', 'Developer', 'Designer'])
|
| 88 |
for _ in range(row_count)]
|
| 89 |
},
|
| 90 |
+
'departments': lambda: {
|
| 91 |
'id': list(range(1, 6)),
|
| 92 |
'name': ['Engineering', 'Sales', 'Marketing', 'HR', 'Finance'],
|
| 93 |
'manager_id': [random.randint(1, 15) for _ in range(5)],
|
| 94 |
'budget': [random.randint(100000, 1000000) for _ in range(5)]
|
| 95 |
+
},
|
| 96 |
'books': {
|
| 97 |
'book_id': gen_id(),
|
| 98 |
'title': [f"Book Title {i}" for i in range(1, row_count + 1)],
|
|
|
|
| 230 |
}
|
| 231 |
|
| 232 |
# Return predefined schema if exists, otherwise create generic one
|
| 233 |
+
table_lower = table_name.lower()
|
| 234 |
+
if table_lower in table_schemas:
|
| 235 |
+
schema = table_schemas[table_lower]
|
| 236 |
+
# If it's a callable (lambda), execute it
|
| 237 |
+
if callable(schema):
|
| 238 |
+
return schema()
|
| 239 |
+
return schema
|
| 240 |
|
| 241 |
# Generic fallback for unknown tables
|
|
|
|
| 242 |
generic_data = {
|
| 243 |
f'{table_name}_id': gen_id(),
|
| 244 |
'name': gen_names(),
|
|
|
|
| 261 |
table_name = table.lower().strip()
|
| 262 |
|
| 263 |
# Generate appropriate sample data
|
| 264 |
+
# Special handling for departments (only 5 rows)
|
| 265 |
+
if table_name == 'departments':
|
| 266 |
+
table_dict = generate_generic_table_data(table_name, row_count=5)
|
| 267 |
+
else:
|
| 268 |
+
table_dict = generate_generic_table_data(table_name, row_count=15)
|
| 269 |
|
| 270 |
df = pd.DataFrame(table_dict)
|
| 271 |
df.to_sql(table_name, conn, index=False, if_exists='replace')
|
|
|
|
| 375 |
# Display sample tables (show first 10 rows for readability)
|
| 376 |
for table_name, df in sample_data.items():
|
| 377 |
output_text += f"**📊 Sample `{table_name}` Table** ({len(df)} rows):\n\n"
|
| 378 |
+
display_df = df.head(10)
|
| 379 |
+
output_text += display_df.to_markdown(index=False)
|
| 380 |
if len(df) > 10:
|
| 381 |
output_text += f"\n\n*...and {len(df) - 10} more rows*"
|
| 382 |
output_text += "\n\n"
|
|
|
|
| 411 |
return error_msg, "", pd.DataFrame({"Error": [str(e)]}), ""
|
| 412 |
|
| 413 |
# Create Gradio Interface
|
| 414 |
+
with gr.Blocks(title="Natural Language to SQL Query Executor", theme=gr.themes.Soft()) as demo:
|
| 415 |
gr.Markdown("""
|
| 416 |
# 🔍 Natural Language to SQL Query Executor
|
| 417 |
|