LeonceNsh commited on
Commit
d9a0200
·
verified ·
1 Parent(s): 9028739

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -8
app.py CHANGED
@@ -6,10 +6,9 @@ from functools import lru_cache
6
  import pandas as pd
7
  import plotly.express as px
8
  import os
9
- from openai import OpenAI
10
 
11
  # Set OpenAI API key
12
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
13
 
14
  # =========================
15
  # Configuration and Setup
@@ -84,13 +83,13 @@ async def parse_query(nl_query):
84
  ]
85
 
86
  try:
87
- response = openai.chat.completions.create(
88
- model="gpt-4o-mini",
89
  messages=messages,
90
  temperature=0,
91
  max_tokens=150,
92
  )
93
- sql_query = response.choices[0].message.content.strip()
94
  return sql_query
95
  except Exception as e:
96
  return f"Error generating SQL query: {e}"
@@ -169,9 +168,12 @@ with gr.Blocks() as demo:
169
  ## Instructions
170
 
171
  1. **Describe the data you want**: e.g., `Show awards over 1M in CA`
172
- 2. **Generate SQL**: Click "Generate SQL" to see the SQL query.
173
- 3. **Execute Query**: Run the query to view results and plots.
174
- 4. **Dataset Schema**: See available columns and types in the "Schema" tab.
 
 
 
175
  """)
176
 
177
  with gr.Tabs():
@@ -179,6 +181,15 @@ with gr.Blocks() as demo:
179
  with gr.Row():
180
  with gr.Column(scale=1):
181
  query = gr.Textbox(label="Natural Language Query", placeholder='e.g., "Awards > 1M in CA"')
 
 
 
 
 
 
 
 
 
182
  btn_generate = gr.Button("Generate SQL")
183
  sql_out = gr.Code(label="Generated SQL Query", language="sql")
184
  plot_code_out = gr.Code(label="Generated Plot Code", language="python")
@@ -219,6 +230,50 @@ with gr.Blocks() as demo:
219
  else:
220
  return result_df, None, ""
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  btn_generate.click(fn=on_generate_click, inputs=query, outputs=[sql_out, plot_code_out])
223
  btn_execute.click(fn=on_execute_click, inputs=[sql_out, plot_code_out], outputs=[results_out, plot_out, error_out])
224
 
 
6
  import pandas as pd
7
  import plotly.express as px
8
  import os
 
9
 
10
  # Set OpenAI API key
11
+ openai.api_key = os.getenv("OPENAI_API_KEY")
12
 
13
  # =========================
14
  # Configuration and Setup
 
83
  ]
84
 
85
  try:
86
+ response = await openai.ChatCompletion.acreate(
87
+ model="gpt-4",
88
  messages=messages,
89
  temperature=0,
90
  max_tokens=150,
91
  )
92
+ sql_query = response['choices'][0]['message']['content'].strip()
93
  return sql_query
94
  except Exception as e:
95
  return f"Error generating SQL query: {e}"
 
168
  ## Instructions
169
 
170
  1. **Describe the data you want**: e.g., `Show awards over 1M in CA`
171
+ 2. **Use Example Queries**: Click on any example query button below to execute.
172
+ 3. **Generate SQL**: Or, enter your own query and click "Generate SQL" to see the SQL query.
173
+ 4. **Execute Query**: Run the query to view results and plots.
174
+ 5. **Dataset Schema**: See available columns and types in the "Schema" tab.
175
+
176
+ ## Example Queries
177
  """)
178
 
179
  with gr.Tabs():
 
181
  with gr.Row():
182
  with gr.Column(scale=1):
183
  query = gr.Textbox(label="Natural Language Query", placeholder='e.g., "Awards > 1M in CA"')
184
+
185
+ # Example query buttons
186
+ gr.Markdown("### Click on an example query:")
187
+ with gr.Row():
188
+ btn_example1 = gr.Button("Show awards over 1M in CA")
189
+ btn_example2 = gr.Button("List all contracts in New York")
190
+ btn_example3 = gr.Button("Show top 5 departments by award amount")
191
+ btn_example4 = gr.Button("Execute: SELECT * from contract_data LIMIT 10;")
192
+
193
  btn_generate = gr.Button("Generate SQL")
194
  sql_out = gr.Code(label="Generated SQL Query", language="sql")
195
  plot_code_out = gr.Code(label="Generated Plot Code", language="python")
 
230
  else:
231
  return result_df, None, ""
232
 
233
+ # Functions for example query buttons
234
+ async def on_example_nl_click(query_text):
235
+ sql_query, plot_code = await generate_sql_and_plot_code(query_text)
236
+ result_df, error_msg = execute_query(sql_query)
237
+ fig = None
238
+ if error_msg:
239
+ return sql_query, plot_code, None, None, error_msg
240
+ if plot_code.strip():
241
+ fig, plot_error = generate_plot(plot_code, result_df)
242
+ if plot_error:
243
+ error_msg = plot_error
244
+ else:
245
+ error_msg = ""
246
+ else:
247
+ fig = None
248
+ error_msg = ""
249
+ return sql_query, plot_code, result_df, fig, error_msg
250
+
251
+ def on_example_sql_click(sql_query):
252
+ result_df, error_msg = execute_query(sql_query)
253
+ fig = None
254
+ plot_code = ""
255
+ if error_msg:
256
+ return sql_query, plot_code, None, None, error_msg
257
+ else:
258
+ return sql_query, plot_code, result_df, fig, ""
259
+
260
+ async def on_example1_click():
261
+ return await on_example_nl_click("Show awards over 1M in CA")
262
+
263
+ async def on_example2_click():
264
+ return await on_example_nl_click("List all contracts in New York")
265
+
266
+ async def on_example3_click():
267
+ return await on_example_nl_click("Show top 5 departments by award amount")
268
+
269
+ def on_example4_click():
270
+ return on_example_sql_click("SELECT * from contract_data LIMIT 10;")
271
+
272
+ btn_example1.click(fn=on_example1_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
273
+ btn_example2.click(fn=on_example2_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
274
+ btn_example3.click(fn=on_example3_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
275
+ btn_example4.click(fn=on_example4_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
276
+
277
  btn_generate.click(fn=on_generate_click, inputs=query, outputs=[sql_out, plot_code_out])
278
  btn_execute.click(fn=on_execute_click, inputs=[sql_out, plot_code_out], outputs=[results_out, plot_out, error_out])
279