Update app.py
Browse files
app.py
CHANGED
@@ -44,12 +44,12 @@ def get_sql_query(description):
|
|
44 |
{"role": "user", "content": f'{prompt}'},
|
45 |
]
|
46 |
)
|
|
|
47 |
|
48 |
except Exception as e:
|
49 |
print(f'The following error ocurred: {e}\n')
|
50 |
-
|
51 |
|
52 |
-
sql_query = completion.choices[0].message.content.strip().split('```sql')[1].split('```')[0]
|
53 |
return sql_query
|
54 |
|
55 |
schema = fetch_table_schema(project_id, dataset_id, table_id)
|
@@ -59,16 +59,18 @@ def execute_sql_query(query):
|
|
59 |
|
60 |
try:
|
61 |
result = client.query(query).to_dataframe()
|
62 |
-
message = f'The query
|
63 |
|
64 |
except Exception as e:
|
65 |
-
result =
|
66 |
-
message = f'The query
|
67 |
|
68 |
return result, message
|
69 |
|
70 |
def echo(text):
|
71 |
query = get_sql_query(text)
|
|
|
|
|
72 |
result, message = execute_sql_query(query)
|
73 |
return result, message
|
74 |
|
@@ -79,6 +81,13 @@ def gradio_interface(text):
|
|
79 |
else:
|
80 |
return result, message
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
demo = gr.Blocks(
|
83 |
title="Text-to-SQL",
|
84 |
theme='remilia/ghostly',
|
@@ -101,7 +110,7 @@ with demo:
|
|
101 |
with gr.Row():
|
102 |
with gr.Column(scale=1):
|
103 |
text_input = gr.Textbox(label="Enter your query")
|
104 |
-
button = gr.Button("Submit")
|
105 |
gr.Examples([
|
106 |
'Find the correlation between RTT and Jitter for each Market',
|
107 |
'Find the variance in Jitter for each 5G_Reliability_Category',
|
@@ -117,10 +126,10 @@ with demo:
|
|
117 |
|
118 |
def update_output(text):
|
119 |
result, message = gradio_interface(text)
|
120 |
-
if isinstance(result, pd.DataFrame):
|
121 |
-
return gr.update(visible=True)
|
122 |
else:
|
123 |
-
return gr.update(visible=False)
|
124 |
|
125 |
button.click(update_output, inputs=text_input, outputs=[output_df, output_text])
|
126 |
|
|
|
44 |
{"role": "user", "content": f'{prompt}'},
|
45 |
]
|
46 |
)
|
47 |
+
sql_query = completion.choices[0].message.content.strip().split('```sql')[1].split('```')[0]
|
48 |
|
49 |
except Exception as e:
|
50 |
print(f'The following error ocurred: {e}\n')
|
51 |
+
sql_query = None
|
52 |
|
|
|
53 |
return sql_query
|
54 |
|
55 |
schema = fetch_table_schema(project_id, dataset_id, table_id)
|
|
|
59 |
|
60 |
try:
|
61 |
result = client.query(query).to_dataframe()
|
62 |
+
message = f'The query:{query} was successfully executed.'
|
63 |
|
64 |
except Exception as e:
|
65 |
+
result = None
|
66 |
+
message = f'The query:{query} could not be executed due to the following exception:\n{e}'
|
67 |
|
68 |
return result, message
|
69 |
|
70 |
def echo(text):
|
71 |
query = get_sql_query(text)
|
72 |
+
if query is None:
|
73 |
+
return 'No query generated', 'No query generated'
|
74 |
result, message = execute_sql_query(query)
|
75 |
return result, message
|
76 |
|
|
|
81 |
else:
|
82 |
return result, message
|
83 |
|
84 |
+
def gradio_interface(text):
|
85 |
+
result, message = echo(text)
|
86 |
+
if isinstance(result, pd.DataFrame):
|
87 |
+
return gr.Dataframe(value=result), message
|
88 |
+
else:
|
89 |
+
return result, message
|
90 |
+
|
91 |
demo = gr.Blocks(
|
92 |
title="Text-to-SQL",
|
93 |
theme='remilia/ghostly',
|
|
|
110 |
with gr.Row():
|
111 |
with gr.Column(scale=1):
|
112 |
text_input = gr.Textbox(label="Enter your query")
|
113 |
+
button = gr.Button("Submit")
|
114 |
gr.Examples([
|
115 |
'Find the correlation between RTT and Jitter for each Market',
|
116 |
'Find the variance in Jitter for each 5G_Reliability_Category',
|
|
|
126 |
|
127 |
def update_output(text):
|
128 |
result, message = gradio_interface(text)
|
129 |
+
if result and isinstance(result, pd.DataFrame):
|
130 |
+
return result, message, gr.update(visible=True)
|
131 |
else:
|
132 |
+
return result, message, gr.update(visible=False)
|
133 |
|
134 |
button.click(update_output, inputs=text_input, outputs=[output_df, output_text])
|
135 |
|