Spaces:
Running
on
Zero
Running
on
Zero
Mustehson
commited on
Commit
·
6e47eb5
1
Parent(s):
323893f
Added Database Schemas
Browse files
app.py
CHANGED
@@ -40,19 +40,22 @@ print('Model Loaded...')
|
|
40 |
print(f'Model Device: {model.device}')
|
41 |
|
42 |
# Get Databases
|
43 |
-
def
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
|
47 |
# Get Tables
|
48 |
-
def get_tables(
|
49 |
-
conn.execute(f"
|
50 |
-
tables = conn.execute("SHOW TABLES").fetchall()
|
51 |
return [table[0] for table in tables]
|
52 |
|
53 |
# Update Tables
|
54 |
-
def update_tables(
|
55 |
-
tables = get_tables(
|
56 |
return gr.update(choices=tables)
|
57 |
|
58 |
# Get Schema
|
@@ -130,7 +133,7 @@ def text2sql(table, query_input):
|
|
130 |
}
|
131 |
|
132 |
# Load Databases Names
|
133 |
-
|
134 |
|
135 |
# Custom CSS styling
|
136 |
custom_css = """
|
@@ -164,7 +167,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="indigo"
|
|
164 |
with gr.Row():
|
165 |
|
166 |
with gr.Column(scale=1, variant='panel'):
|
167 |
-
|
168 |
tables_dropdown = gr.Dropdown(choices=[], label="Available Tables", value=None)
|
169 |
|
170 |
with gr.Column(scale=2):
|
@@ -185,7 +188,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="indigo"
|
|
185 |
with gr.Tab("Schema"):
|
186 |
table_schema = gr.Textbox(lines=TAB_LINES, label="Table Schema", value="", interactive=False)
|
187 |
|
188 |
-
|
189 |
generate_query_button.click(text2sql, inputs=[tables_dropdown, query_input], outputs=[table_schema, input_prompt, generated_query, result_output])
|
190 |
|
191 |
if __name__ == "__main__":
|
|
|
40 |
print(f'Model Device: {model.device}')
|
41 |
|
42 |
# Get Databases
|
43 |
+
def get_schemas():
|
44 |
+
schemas = conn.execute("""
|
45 |
+
SELECT DISTINCT schema_name
|
46 |
+
FROM information_schema.schemata
|
47 |
+
WHERE schema_name NOT IN ('information_schema', 'pg_catalog')
|
48 |
+
""").fetchall()
|
49 |
+
return [item[0] for item in schemas]
|
50 |
|
51 |
# Get Tables
|
52 |
+
def get_tables(schema_name):
|
53 |
+
tables = conn.execute(f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{schema_name}'").fetchall()
|
|
|
54 |
return [table[0] for table in tables]
|
55 |
|
56 |
# Update Tables
|
57 |
+
def update_tables(schema_name):
|
58 |
+
tables = get_tables(schema_name)
|
59 |
return gr.update(choices=tables)
|
60 |
|
61 |
# Get Schema
|
|
|
133 |
}
|
134 |
|
135 |
# Load Databases Names
|
136 |
+
|
137 |
|
138 |
# Custom CSS styling
|
139 |
custom_css = """
|
|
|
167 |
with gr.Row():
|
168 |
|
169 |
with gr.Column(scale=1, variant='panel'):
|
170 |
+
schema_dropdown = gr.Dropdown(choices=get_schemas(), label="Select Schema", interactive=True)
|
171 |
tables_dropdown = gr.Dropdown(choices=[], label="Available Tables", value=None)
|
172 |
|
173 |
with gr.Column(scale=2):
|
|
|
188 |
with gr.Tab("Schema"):
|
189 |
table_schema = gr.Textbox(lines=TAB_LINES, label="Table Schema", value="", interactive=False)
|
190 |
|
191 |
+
schema_dropdown.change(update_tables, inputs=schema_dropdown, outputs=tables_dropdown)
|
192 |
generate_query_button.click(text2sql, inputs=[tables_dropdown, query_input], outputs=[table_schema, input_prompt, generated_query, result_output])
|
193 |
|
194 |
if __name__ == "__main__":
|