Spaces:
Sleeping
Sleeping
File size: 10,551 Bytes
b474ae1 ec9d21a 06f01b3 b474ae1 d33fe62 dfe1769 1fa796c 5b4c268 00c05fa 24b6a6d 00c05fa ae610aa f146007 5b4c268 d33fe62 5a73339 92494e9 d33fe62 ae610aa f146007 ae610aa b474ae1 d33fe62 5b4c268 ae610aa a1792a1 d33fe62 88c83f6 dfe1769 a1792a1 88c83f6 a1792a1 1fa796c dfe1769 f0741dc d9a0200 13f0f94 88c83f6 dfe1769 24b6a6d dfe1769 ae610aa dfe1769 88c83f6 d33fe62 88c83f6 dfe1769 a1792a1 d33fe62 88c83f6 d33fe62 dfe1769 a1792a1 dfe1769 1fa796c dfe1769 1fa796c dfe1769 b474ae1 d33fe62 88c83f6 d33fe62 1fa796c 88c83f6 1fa796c 8760634 d33fe62 8760634 dfe1769 1fa796c dfe1769 88c83f6 dfe1769 ae610aa 06f01b3 dfe1769 8760634 dfe1769 00c05fa 88c83f6 d9a0200 06f01b3 b474ae1 8760634 88c83f6 d9a0200 8760634 dfe1769 8760634 d33fe62 dfe1769 b474ae1 8760634 88c83f6 8cb3a33 00c05fa a1792a1 00c05fa a1792a1 dfe1769 00c05fa dfe1769 00c05fa dfe1769 d9a0200 88c83f6 5b4c268 00c05fa ae610aa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
import json
import openai
import gradio as gr
import duckdb
from functools import lru_cache
import pandas as pd
import plotly.express as px
import os
# Set OpenAI API key
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# =========================
# Configuration and Setup
# =========================
# Load the Parquet dataset path
dataset_path = 'sample_contract_df.parquet' # Update with your Parquet file path
# Provided schema
schema = [
{"column_name": "department_ind_agency", "column_type": "VARCHAR"},
{"column_name": "cgac", "column_type": "BIGINT"},
{"column_name": "sub_tier", "column_type": "VARCHAR"},
{"column_name": "fpds_code", "column_type": "VARCHAR"},
{"column_name": "office", "column_type": "VARCHAR"},
{"column_name": "aac_code", "column_type": "VARCHAR"},
{"column_name": "posteddate", "column_type": "VARCHAR"},
{"column_name": "type", "column_type": "VARCHAR"},
{"column_name": "basetype", "column_type": "VARCHAR"},
{"column_name": "popstreetaddress", "column_type": "VARCHAR"},
{"column_name": "popcity", "column_type": "VARCHAR"},
{"column_name": "popstate", "column_type": "VARCHAR"},
{"column_name": "popzip", "column_type": "VARCHAR"},
{"column_name": "popcountry", "column_type": "VARCHAR"},
{"column_name": "active", "column_type": "VARCHAR"},
{"column_name": "awardnumber", "column_type": "VARCHAR"},
{"column_name": "awarddate", "column_type": "VARCHAR"},
{"column_name": "award", "column_type": "DOUBLE"},
{"column_name": "awardee", "column_type": "VARCHAR"},
{"column_name": "state", "column_type": "VARCHAR"},
{"column_name": "city", "column_type": "VARCHAR"},
{"column_name": "zipcode", "column_type": "VARCHAR"},
{"column_name": "countrycode", "column_type": "VARCHAR"}
]
@lru_cache(maxsize=1)
def get_schema():
return schema
COLUMN_TYPES = {col['column_name']: col['column_type'] for col in get_schema()}
# =========================
# Database Interaction
# =========================
def load_dataset_schema():
"""
Loads the dataset schema into DuckDB by creating a view.
"""
con = duckdb.connect()
try:
con.execute("DROP VIEW IF EXISTS contract_data")
con.execute(f"CREATE VIEW contract_data AS SELECT * FROM '{dataset_path}'")
return True
except Exception as e:
print(f"Error loading dataset schema: {e}")
return False
finally:
con.close()
# =========================
# OpenAI API Integration
# =========================
async def parse_query(nl_query):
"""
Converts a natural language query into a SQL query using OpenAI's API.
"""
messages = [
{"role": "system", "content": "Convert natural language queries to SQL queries for 'contract_data'."},
{"role": "user", "content": f"Schema:\n{json.dumps(schema, indent=2)}\n\nQuery:\n\"{nl_query}\"\n\nSQL:"}
]
try:
response = openai.chat.completions.create(
model="gpt-4",
messages=messages,
temperature=0,
max_tokens=150,
)
sql_query = response.choices[0].message.content.strip()
return sql_query
except Exception as e:
return f"Error generating SQL query: {e}"
# =========================
# Plotting Utilities
# =========================
def detect_plot_intent(nl_query):
"""
Detects if the user's query involves plotting.
"""
plot_keywords = ['plot', 'graph', 'chart', 'distribution', 'visualize']
return any(keyword in nl_query.lower() for keyword in plot_keywords)
async def generate_sql_and_plot_code(query):
"""
Generates SQL query and optional plotting code.
"""
is_plot = detect_plot_intent(query)
sql_query = await parse_query(query)
plot_code = ""
if is_plot and not sql_query.startswith("Error"):
plot_code = """
import plotly.express as px
fig = px.bar(result_df, x='x_column', y='y_column', title='Generated Plot')
fig.update_layout(title_x=0.5)
"""
return sql_query, plot_code
def execute_query(sql_query):
"""
Executes the SQL query and returns the results.
"""
if sql_query.startswith("Error"):
return None, sql_query
try:
con = duckdb.connect()
con.execute(f"CREATE OR REPLACE VIEW contract_data AS SELECT * FROM '{dataset_path}'")
result_df = con.execute(sql_query).fetchdf()
con.close()
return result_df, ""
except Exception as e:
return None, f"Error executing query: {e}"
def generate_plot(plot_code, result_df):
"""
Executes the plot code to generate a plot from the result DataFrame.
"""
if not plot_code.strip():
return None, "No plot code provided."
try:
columns = result_df.columns.tolist()
if len(columns) < 2:
return None, "Not enough columns to plot."
plot_code = plot_code.replace('x_column', columns[0])
plot_code = plot_code.replace('y_column', columns[1])
local_vars = {'result_df': result_df, 'px': px}
exec(plot_code, {}, local_vars)
fig = local_vars.get('fig', None)
return fig, "" if fig else "Plot could not be generated."
except Exception as e:
return None, f"Error generating plot: {e}"
# =========================
# Gradio Application UI
# =========================
with gr.Blocks() as demo:
gr.Markdown("""
# Parquet SQL Query and Plotting App
**Query and visualize data** in `sample_contract_df.parquet`
## Instructions
1. **Describe the data you want**: e.g., `Show awards over 1M in CA`
2. **Use Example Queries**: Click on any example query button below to execute.
3. **Generate SQL**: Or, enter your own query and click "Generate SQL" to see the SQL query.
4. **Execute Query**: Run the query to view results and plots.
5. **Dataset Schema**: See available columns and types in the "Schema" tab.
## Example Queries
""")
with gr.Tabs():
with gr.TabItem("Query Data"):
with gr.Row():
with gr.Column(scale=1):
query = gr.Textbox(label="Natural Language Query", placeholder='e.g., "Awards > 1M in CA"')
# Example query buttons
gr.Markdown("### Click on an example query:")
with gr.Row():
btn_example1 = gr.Button("Show awards over 1M in CA")
btn_example2 = gr.Button("List all contracts in New York")
btn_example3 = gr.Button("Show top 5 departments by award amount")
btn_example4 = gr.Button("Execute: SELECT * from contract_data LIMIT 10;")
btn_generate = gr.Button("Generate SQL")
sql_out = gr.Code(label="Generated SQL Query", language="sql")
plot_code_out = gr.Code(label="Generated Plot Code", language="python")
btn_execute = gr.Button("Execute Query")
error_out = gr.Markdown("", visible=False)
with gr.Column(scale=2):
results_out = gr.Dataframe(label="Query Results", interactive=False)
plot_out = gr.Plot(label="Plot")
with gr.TabItem("Dataset Schema"):
gr.Markdown("### Dataset Schema")
schema_display = gr.JSON(label="Schema", value=json.loads(json.dumps(get_schema(), indent=2)))
# =========================
# Click Event Handlers
# =========================
async def on_generate_click(nl_query):
"""
Handles the "Generate SQL" button click event.
"""
sql_query, plot_code = await generate_sql_and_plot_code(nl_query)
return sql_query, plot_code
def on_execute_click(sql_query, plot_code):
"""
Handles the "Execute Query" button click event.
"""
result_df, error_msg = execute_query(sql_query)
if error_msg:
return None, None, error_msg
if plot_code.strip():
fig, plot_error = generate_plot(plot_code, result_df)
if plot_error:
return result_df, None, plot_error
else:
return result_df, fig, ""
else:
return result_df, None, ""
# Functions for example query buttons
async def on_example_nl_click(query_text):
sql_query, plot_code = await generate_sql_and_plot_code(query_text)
result_df, error_msg = execute_query(sql_query)
fig = None
if error_msg:
return sql_query, plot_code, None, None, error_msg
if plot_code.strip():
fig, plot_error = generate_plot(plot_code, result_df)
if plot_error:
error_msg = plot_error
else:
error_msg = ""
else:
fig = None
error_msg = ""
return sql_query, plot_code, result_df, fig, error_msg
def on_example_sql_click(sql_query):
result_df, error_msg = execute_query(sql_query)
fig = None
plot_code = ""
if error_msg:
return sql_query, plot_code, None, None, error_msg
else:
return sql_query, plot_code, result_df, fig, ""
async def on_example1_click():
return await on_example_nl_click("Show awards over 1M in CA")
async def on_example2_click():
return await on_example_nl_click("List all contracts in New York")
async def on_example3_click():
return await on_example_nl_click("Show top 5 departments by award amount")
def on_example4_click():
return on_example_sql_click("SELECT * from contract_data LIMIT 10;")
btn_example1.click(fn=on_example1_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
btn_example2.click(fn=on_example2_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
btn_example3.click(fn=on_example3_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
btn_example4.click(fn=on_example4_click, inputs=[], outputs=[sql_out, plot_code_out, results_out, plot_out, error_out])
btn_generate.click(fn=on_generate_click, inputs=query, outputs=[sql_out, plot_code_out])
btn_execute.click(fn=on_execute_click, inputs=[sql_out, plot_code_out], outputs=[results_out, plot_out, error_out])
# =========================
# Launch the Gradio App
# =========================
demo.launch()
|