Spaces:
Runtime error
Runtime error
Mustehson
commited on
Commit
Β·
323893f
1
Parent(s):
d10cca1
Moved Run Query Bottom Right
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ md_token = os.getenv('MD_TOKEN')
|
|
| 15 |
|
| 16 |
print('Connecting to DB...')
|
| 17 |
# Connect to DB
|
| 18 |
-
conn = duckdb.connect(f"md:my_db?motherduck_token={md_token}")
|
| 19 |
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
device = torch.device("cuda")
|
|
@@ -56,7 +56,7 @@ def update_tables(selected_db):
|
|
| 56 |
return gr.update(choices=tables)
|
| 57 |
|
| 58 |
# Get Schema
|
| 59 |
-
def
|
| 60 |
conn.execute(f"SELECT * FROM '{table}' LIMIT 1;")
|
| 61 |
result = conn.sql(f"SELECT sql FROM duckdb_tables() where table_name ='{table}';").df()
|
| 62 |
ddl_create = result.iloc[0,0]
|
|
@@ -96,7 +96,7 @@ def text2sql(table, query_input):
|
|
| 96 |
result_output:pd.DataFrame([{"error": f"β Unable to get the SQL query based on the text. {e}"}])
|
| 97 |
}
|
| 98 |
|
| 99 |
-
schema =
|
| 100 |
print(f'Schema Generated...')
|
| 101 |
prompt = get_prompt(schema, query_input)
|
| 102 |
print(f'Prompt Generated...')
|
|
@@ -169,7 +169,11 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="indigo"
|
|
| 169 |
|
| 170 |
with gr.Column(scale=2):
|
| 171 |
query_input = gr.Textbox(lines=5, label="Text Query", placeholder="Enter your text query here...")
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
with gr.Tabs():
|
| 175 |
with gr.Tab("Result"):
|
|
@@ -179,7 +183,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="indigo"
|
|
| 179 |
with gr.Tab("Prompt"):
|
| 180 |
input_prompt = gr.Textbox(lines=TAB_LINES, label="Input Prompt", value="", interactive=False)
|
| 181 |
with gr.Tab("Schema"):
|
| 182 |
-
table_schema = gr.Textbox(lines=TAB_LINES, label="Schema", value="", interactive=False)
|
| 183 |
|
| 184 |
database_dropdown.change(update_tables, inputs=database_dropdown, outputs=tables_dropdown)
|
| 185 |
generate_query_button.click(text2sql, inputs=[tables_dropdown, query_input], outputs=[table_schema, input_prompt, generated_query, result_output])
|
|
|
|
| 15 |
|
| 16 |
print('Connecting to DB...')
|
| 17 |
# Connect to DB
|
| 18 |
+
conn = duckdb.connect(f"md:my_db?motherduck_token={md_token}", read_only=True)
|
| 19 |
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
device = torch.device("cuda")
|
|
|
|
| 56 |
return gr.update(choices=tables)
|
| 57 |
|
| 58 |
# Get Schema
|
| 59 |
+
def get_table_schema(table):
|
| 60 |
conn.execute(f"SELECT * FROM '{table}' LIMIT 1;")
|
| 61 |
result = conn.sql(f"SELECT sql FROM duckdb_tables() where table_name ='{table}';").df()
|
| 62 |
ddl_create = result.iloc[0,0]
|
|
|
|
| 96 |
result_output:pd.DataFrame([{"error": f"β Unable to get the SQL query based on the text. {e}"}])
|
| 97 |
}
|
| 98 |
|
| 99 |
+
schema = get_table_schema(table)
|
| 100 |
print(f'Schema Generated...')
|
| 101 |
prompt = get_prompt(schema, query_input)
|
| 102 |
print(f'Prompt Generated...')
|
|
|
|
| 169 |
|
| 170 |
with gr.Column(scale=2):
|
| 171 |
query_input = gr.Textbox(lines=5, label="Text Query", placeholder="Enter your text query here...")
|
| 172 |
+
with gr.Row():
|
| 173 |
+
with gr.Column(scale=7):
|
| 174 |
+
pass
|
| 175 |
+
with gr.Column(scale=1):
|
| 176 |
+
generate_query_button = gr.Button("Run Query", variant="primary")
|
| 177 |
|
| 178 |
with gr.Tabs():
|
| 179 |
with gr.Tab("Result"):
|
|
|
|
| 183 |
with gr.Tab("Prompt"):
|
| 184 |
input_prompt = gr.Textbox(lines=TAB_LINES, label="Input Prompt", value="", interactive=False)
|
| 185 |
with gr.Tab("Schema"):
|
| 186 |
+
table_schema = gr.Textbox(lines=TAB_LINES, label="Table Schema", value="", interactive=False)
|
| 187 |
|
| 188 |
database_dropdown.change(update_tables, inputs=database_dropdown, outputs=tables_dropdown)
|
| 189 |
generate_query_button.click(text2sql, inputs=[tables_dropdown, query_input], outputs=[table_schema, input_prompt, generated_query, result_output])
|