Update app.py
Browse files
app.py
CHANGED
@@ -21,8 +21,11 @@ def process_sql_file(file_path):
|
|
21 |
with open(file_path, 'r') as file:
|
22 |
sql_content = file.read()
|
23 |
|
|
|
|
|
|
|
24 |
# Split into individual statements
|
25 |
-
statements = sql_content.split(';')
|
26 |
|
27 |
# Clear existing database
|
28 |
clear_database()
|
@@ -30,7 +33,7 @@ def process_sql_file(file_path):
|
|
30 |
# Execute each statement
|
31 |
with engine.begin() as conn:
|
32 |
for statement in statements:
|
33 |
-
if statement.strip():
|
34 |
conn.execute(text(statement))
|
35 |
|
36 |
return True, "SQL file successfully executed! Click 'Continue' to proceed to query interface..."
|
@@ -38,151 +41,12 @@ def process_sql_file(file_path):
|
|
38 |
except Exception as e:
|
39 |
return False, f"Error processing SQL file: {str(e)}"
|
40 |
|
41 |
-
|
42 |
-
"""
|
43 |
-
Process a CSV file and load it into the database.
|
44 |
-
"""
|
45 |
-
try:
|
46 |
-
# Read the CSV file
|
47 |
-
df = pd.read_csv(file_path)
|
48 |
-
|
49 |
-
if len(df.columns) == 0:
|
50 |
-
return False, "Error: File contains no columns"
|
51 |
-
|
52 |
-
# Clear existing database and create new table
|
53 |
-
clear_database()
|
54 |
-
table = create_dynamic_table(df)
|
55 |
-
|
56 |
-
# Convert DataFrame to list of dictionaries and insert
|
57 |
-
records = df.to_dict('records')
|
58 |
-
insert_rows_into_table(records, table)
|
59 |
-
|
60 |
-
return True, "CSV file successfully loaded! Click 'Continue' to proceed to query interface..."
|
61 |
-
|
62 |
-
except Exception as e:
|
63 |
-
return False, f"Error processing CSV file: {str(e)}"
|
64 |
-
|
65 |
-
def process_uploaded_file(file):
|
66 |
-
"""
|
67 |
-
Process the uploaded file (either SQL or CSV).
|
68 |
-
"""
|
69 |
-
try:
|
70 |
-
if file is None:
|
71 |
-
return False, "Please upload a file."
|
72 |
-
|
73 |
-
# Get file extension
|
74 |
-
file_ext = os.path.splitext(file)[1].lower()
|
75 |
-
|
76 |
-
if file_ext == '.sql':
|
77 |
-
return process_sql_file(file)
|
78 |
-
elif file_ext == '.csv':
|
79 |
-
return process_csv_file(file)
|
80 |
-
else:
|
81 |
-
return False, "Error: Unsupported file type. Please upload either a .sql or .csv file."
|
82 |
-
|
83 |
-
except Exception as e:
|
84 |
-
return False, f"Error processing file: {str(e)}"
|
85 |
-
|
86 |
-
def get_data_table():
|
87 |
-
"""
|
88 |
-
Fetches all data from the current table and returns it as a Pandas DataFrame.
|
89 |
-
"""
|
90 |
-
try:
|
91 |
-
# Get list of tables
|
92 |
-
with engine.connect() as con:
|
93 |
-
tables = con.execute(text(
|
94 |
-
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
|
95 |
-
)).fetchall()
|
96 |
-
|
97 |
-
if not tables:
|
98 |
-
return pd.DataFrame()
|
99 |
-
|
100 |
-
# Use the first table found
|
101 |
-
table_name = tables[0][0]
|
102 |
-
|
103 |
-
with engine.connect() as con:
|
104 |
-
result = con.execute(text(f"SELECT * FROM {table_name}"))
|
105 |
-
rows = result.fetchall()
|
106 |
-
|
107 |
-
if not rows:
|
108 |
-
return pd.DataFrame()
|
109 |
-
|
110 |
-
columns = result.keys()
|
111 |
-
df = pd.DataFrame(rows, columns=columns)
|
112 |
-
return df
|
113 |
-
|
114 |
-
except Exception as e:
|
115 |
-
return pd.DataFrame({"Error": [str(e)]})
|
116 |
-
|
117 |
-
@tool
|
118 |
-
def sql_engine(query: str) -> str:
|
119 |
-
"""
|
120 |
-
Executes an SQL query and returns formatted results.
|
121 |
-
|
122 |
-
Args:
|
123 |
-
query: The SQL query string to execute on the database. Must be a valid SELECT query.
|
124 |
-
|
125 |
-
Returns:
|
126 |
-
str: The formatted query results as a string.
|
127 |
-
"""
|
128 |
-
try:
|
129 |
-
with engine.connect() as con:
|
130 |
-
rows = con.execute(text(query)).fetchall()
|
131 |
-
|
132 |
-
if not rows:
|
133 |
-
return "No results found."
|
134 |
-
|
135 |
-
if len(rows) == 1 and len(rows[0]) == 1:
|
136 |
-
return str(rows[0][0])
|
137 |
-
|
138 |
-
return "\n".join([", ".join(map(str, row)) for row in rows])
|
139 |
-
|
140 |
-
except Exception as e:
|
141 |
-
return f"Error: {str(e)}"
|
142 |
-
|
143 |
-
agent = CodeAgent(
|
144 |
-
tools=[sql_engine],
|
145 |
-
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
|
146 |
-
)
|
147 |
-
|
148 |
-
def query_sql(user_query: str) -> str:
|
149 |
-
"""
|
150 |
-
Converts natural language input to an SQL query using CodeAgent.
|
151 |
-
"""
|
152 |
-
schema = get_table_schema()
|
153 |
-
if not schema:
|
154 |
-
return "Error: No data table exists. Please upload a file first."
|
155 |
-
|
156 |
-
schema_info = (
|
157 |
-
f"The database has the following schema:\n"
|
158 |
-
f"{schema}\n"
|
159 |
-
"Generate a valid SQL SELECT query using ONLY these column names.\n"
|
160 |
-
"DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
|
161 |
-
)
|
162 |
-
|
163 |
-
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
164 |
-
|
165 |
-
if not isinstance(generated_sql, str):
|
166 |
-
return f"{generated_sql}"
|
167 |
-
|
168 |
-
if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
|
169 |
-
return "Error: Only SELECT queries are allowed."
|
170 |
-
|
171 |
-
result = sql_engine(generated_sql)
|
172 |
-
|
173 |
-
try:
|
174 |
-
float_result = float(result)
|
175 |
-
return f"{float_result:.2f}"
|
176 |
-
except ValueError:
|
177 |
-
return result
|
178 |
|
179 |
# Create the Gradio interface
|
180 |
with gr.Blocks() as demo:
|
181 |
-
current_state = gr.State("upload")
|
182 |
-
|
183 |
# Upload Interface Components
|
184 |
-
|
185 |
-
with upload_group:
|
186 |
gr.Markdown("""
|
187 |
# Data Query Interface
|
188 |
|
@@ -212,30 +76,31 @@ with gr.Blocks() as demo:
|
|
212 |
continue_btn = gr.Button("Continue", visible=False)
|
213 |
|
214 |
# Query Interface Components
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
-
|
221 |
-
The AI will convert your questions into SQL queries.
|
222 |
-
""")
|
223 |
|
|
|
224 |
with gr.Row():
|
225 |
-
with gr.Column(
|
226 |
-
user_input = gr.Textbox(
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
gr.
|
231 |
-
|
232 |
-
value=get_data_table(),
|
233 |
-
label="Data Table",
|
234 |
interactive=False
|
235 |
)
|
236 |
|
237 |
-
schema_display = gr.Markdown(value="Loading schema...")
|
238 |
-
|
239 |
with gr.Row():
|
240 |
refresh_table_btn = gr.Button("Refresh Table")
|
241 |
refresh_schema_btn = gr.Button("Refresh Schema")
|
@@ -243,19 +108,28 @@ with gr.Blocks() as demo:
|
|
243 |
|
244 |
def handle_upload(file):
|
245 |
success, message = process_uploaded_file(file)
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
|
254 |
def switch_to_query():
|
|
|
|
|
255 |
return {
|
256 |
upload_group: gr.update(visible=False),
|
257 |
query_group: gr.update(visible=True),
|
258 |
-
|
|
|
259 |
}
|
260 |
|
261 |
def switch_to_upload():
|
@@ -269,18 +143,32 @@ with gr.Blocks() as demo:
|
|
269 |
# Event handlers
|
270 |
file_input.upload(
|
271 |
fn=handle_upload,
|
272 |
-
|
273 |
-
|
|
|
|
|
|
|
|
|
274 |
)
|
275 |
|
276 |
continue_btn.click(
|
277 |
fn=switch_to_query,
|
278 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
279 |
)
|
280 |
|
281 |
back_btn.click(
|
282 |
fn=switch_to_upload,
|
283 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
284 |
)
|
285 |
|
286 |
user_input.change(
|
|
|
21 |
with open(file_path, 'r') as file:
|
22 |
sql_content = file.read()
|
23 |
|
24 |
+
# Replace AUTO_INCREMENT with AUTOINCREMENT for SQLite compatibility
|
25 |
+
sql_content = sql_content.replace('AUTO_INCREMENT', 'AUTOINCREMENT')
|
26 |
+
|
27 |
# Split into individual statements
|
28 |
+
statements = [stmt.strip() for stmt in sql_content.split(';') if stmt.strip()]
|
29 |
|
30 |
# Clear existing database
|
31 |
clear_database()
|
|
|
33 |
# Execute each statement
|
34 |
with engine.begin() as conn:
|
35 |
for statement in statements:
|
36 |
+
if statement.strip():
|
37 |
conn.execute(text(statement))
|
38 |
|
39 |
return True, "SQL file successfully executed! Click 'Continue' to proceed to query interface..."
|
|
|
41 |
except Exception as e:
|
42 |
return False, f"Error processing SQL file: {str(e)}"
|
43 |
|
44 |
+
# ... (other functions remain the same) ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Create the Gradio interface
|
47 |
with gr.Blocks() as demo:
|
|
|
|
|
48 |
# Upload Interface Components
|
49 |
+
with gr.Group() as upload_group:
|
|
|
50 |
gr.Markdown("""
|
51 |
# Data Query Interface
|
52 |
|
|
|
76 |
continue_btn = gr.Button("Continue", visible=False)
|
77 |
|
78 |
# Query Interface Components
|
79 |
+
with gr.Group(visible=False) as query_group:
|
80 |
+
gr.Markdown("## Data Query Interface")
|
81 |
+
|
82 |
+
# Data Display Section
|
83 |
+
gr.Markdown("### Current Data")
|
84 |
+
data_table = gr.Dataframe(
|
85 |
+
value=get_data_table(),
|
86 |
+
label="Data Table",
|
87 |
+
interactive=False
|
88 |
+
)
|
89 |
|
90 |
+
schema_display = gr.Markdown(value="Loading schema...")
|
|
|
|
|
91 |
|
92 |
+
# Query Section
|
93 |
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
user_input = gr.Textbox(
|
96 |
+
label="Ask a question about the data",
|
97 |
+
placeholder="Enter your question here..."
|
98 |
+
)
|
99 |
+
query_output = gr.Textbox(
|
100 |
+
label="Result",
|
|
|
|
|
101 |
interactive=False
|
102 |
)
|
103 |
|
|
|
|
|
104 |
with gr.Row():
|
105 |
refresh_table_btn = gr.Button("Refresh Table")
|
106 |
refresh_schema_btn = gr.Button("Refresh Schema")
|
|
|
108 |
|
109 |
def handle_upload(file):
|
110 |
success, message = process_uploaded_file(file)
|
111 |
+
if success:
|
112 |
+
df = get_data_table()
|
113 |
+
schema = get_table_schema()
|
114 |
+
return {
|
115 |
+
upload_status: message,
|
116 |
+
continue_btn: gr.update(visible=True),
|
117 |
+
data_table: df,
|
118 |
+
schema_display: f"### Current Schema:\n```\n{schema}\n```" if schema else "No schema available"
|
119 |
+
}
|
120 |
+
return {
|
121 |
+
upload_status: message,
|
122 |
+
continue_btn: gr.update(visible=False)
|
123 |
+
}
|
124 |
|
125 |
def switch_to_query():
|
126 |
+
df = get_data_table()
|
127 |
+
schema = get_table_schema()
|
128 |
return {
|
129 |
upload_group: gr.update(visible=False),
|
130 |
query_group: gr.update(visible=True),
|
131 |
+
data_table: df,
|
132 |
+
schema_display: f"### Current Schema:\n```\n{schema}\n```" if schema else "No schema available"
|
133 |
}
|
134 |
|
135 |
def switch_to_upload():
|
|
|
143 |
# Event handlers
|
144 |
file_input.upload(
|
145 |
fn=handle_upload,
|
146 |
+
outputs=[
|
147 |
+
upload_status,
|
148 |
+
continue_btn,
|
149 |
+
data_table,
|
150 |
+
schema_display
|
151 |
+
]
|
152 |
)
|
153 |
|
154 |
continue_btn.click(
|
155 |
fn=switch_to_query,
|
156 |
+
outputs=[
|
157 |
+
upload_group,
|
158 |
+
query_group,
|
159 |
+
data_table,
|
160 |
+
schema_display
|
161 |
+
]
|
162 |
)
|
163 |
|
164 |
back_btn.click(
|
165 |
fn=switch_to_upload,
|
166 |
+
outputs=[
|
167 |
+
upload_group,
|
168 |
+
query_group,
|
169 |
+
continue_btn,
|
170 |
+
upload_status
|
171 |
+
]
|
172 |
)
|
173 |
|
174 |
user_input.change(
|