Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,16 +12,40 @@ from database import (
|
|
12 |
get_table_schema
|
13 |
)
|
14 |
|
15 |
-
def
|
16 |
"""
|
17 |
-
Process
|
18 |
"""
|
19 |
try:
|
20 |
-
|
21 |
-
|
|
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Read the CSV file
|
24 |
-
df = pd.read_csv(
|
25 |
|
26 |
if len(df.columns) == 0:
|
27 |
return False, "Error: File contains no columns"
|
@@ -34,8 +58,29 @@ def process_uploaded_file(file):
|
|
34 |
records = df.to_dict('records')
|
35 |
insert_rows_into_table(records, table)
|
36 |
|
37 |
-
return True, "
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
except Exception as e:
|
40 |
return False, f"Error processing file: {str(e)}"
|
41 |
|
@@ -44,8 +89,20 @@ def get_data_table():
|
|
44 |
Fetches all data from the current table and returns it as a Pandas DataFrame.
|
45 |
"""
|
46 |
try:
|
|
|
47 |
with engine.connect() as con:
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
rows = result.fetchall()
|
50 |
|
51 |
if not rows:
|
@@ -98,7 +155,7 @@ def query_sql(user_query: str) -> str:
|
|
98 |
return "Error: No data table exists. Please upload a file first."
|
99 |
|
100 |
schema_info = (
|
101 |
-
"The database has
|
102 |
f"{schema}\n"
|
103 |
"Generate a valid SQL SELECT query using ONLY these column names.\n"
|
104 |
"DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
|
@@ -184,17 +241,26 @@ with gr.Blocks() as demo:
|
|
184 |
gr.Markdown("""
|
185 |
# Data Query Interface
|
186 |
|
187 |
-
Upload your
|
|
|
|
|
|
|
|
|
188 |
|
189 |
-
### Requirements:
|
190 |
-
-
|
191 |
- First column will be used as the primary key
|
192 |
-
-
|
|
|
|
|
|
|
|
|
|
|
193 |
""")
|
194 |
|
195 |
file_input = gr.File(
|
196 |
-
label="Upload
|
197 |
-
file_types=[".csv"],
|
198 |
type="filepath"
|
199 |
)
|
200 |
status = gr.Textbox(label="Status", interactive=False)
|
|
|
12 |
get_table_schema
|
13 |
)
|
14 |
|
15 |
+
def process_sql_file(file_path):
|
16 |
"""
|
17 |
+
Process an SQL file and execute its contents.
|
18 |
"""
|
19 |
try:
|
20 |
+
# Read the SQL file
|
21 |
+
with open(file_path, 'r') as file:
|
22 |
+
sql_content = file.read()
|
23 |
|
24 |
+
# Split into individual statements
|
25 |
+
# This assumes statements are separated by semicolons
|
26 |
+
statements = sql_content.split(';')
|
27 |
+
|
28 |
+
# Clear existing database
|
29 |
+
clear_database()
|
30 |
+
|
31 |
+
# Execute each statement
|
32 |
+
with engine.begin() as conn:
|
33 |
+
for statement in statements:
|
34 |
+
if statement.strip(): # Skip empty statements
|
35 |
+
conn.execute(text(statement))
|
36 |
+
|
37 |
+
return True, "SQL file successfully executed! Proceeding to query interface..."
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
return False, f"Error processing SQL file: {str(e)}"
|
41 |
+
|
42 |
+
def process_csv_file(file_path):
|
43 |
+
"""
|
44 |
+
Process a CSV file and load it into the database.
|
45 |
+
"""
|
46 |
+
try:
|
47 |
# Read the CSV file
|
48 |
+
df = pd.read_csv(file_path)
|
49 |
|
50 |
if len(df.columns) == 0:
|
51 |
return False, "Error: File contains no columns"
|
|
|
58 |
records = df.to_dict('records')
|
59 |
insert_rows_into_table(records, table)
|
60 |
|
61 |
+
return True, "CSV file successfully loaded! Proceeding to query interface..."
|
62 |
|
63 |
+
except Exception as e:
|
64 |
+
return False, f"Error processing CSV file: {str(e)}"
|
65 |
+
|
66 |
+
def process_uploaded_file(file):
|
67 |
+
"""
|
68 |
+
Process the uploaded file (either SQL or CSV).
|
69 |
+
"""
|
70 |
+
try:
|
71 |
+
if file is None:
|
72 |
+
return False, "Please upload a file."
|
73 |
+
|
74 |
+
# Get file extension
|
75 |
+
file_ext = os.path.splitext(file)[1].lower()
|
76 |
+
|
77 |
+
if file_ext == '.sql':
|
78 |
+
return process_sql_file(file)
|
79 |
+
elif file_ext == '.csv':
|
80 |
+
return process_csv_file(file)
|
81 |
+
else:
|
82 |
+
return False, "Error: Unsupported file type. Please upload either a .sql or .csv file."
|
83 |
+
|
84 |
except Exception as e:
|
85 |
return False, f"Error processing file: {str(e)}"
|
86 |
|
|
|
89 |
Fetches all data from the current table and returns it as a Pandas DataFrame.
|
90 |
"""
|
91 |
try:
|
92 |
+
# Get list of tables
|
93 |
with engine.connect() as con:
|
94 |
+
tables = con.execute(text(
|
95 |
+
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
|
96 |
+
)).fetchall()
|
97 |
+
|
98 |
+
if not tables:
|
99 |
+
return pd.DataFrame()
|
100 |
+
|
101 |
+
# Use the first table found
|
102 |
+
table_name = tables[0][0]
|
103 |
+
|
104 |
+
with engine.connect() as con:
|
105 |
+
result = con.execute(text(f"SELECT * FROM {table_name}"))
|
106 |
rows = result.fetchall()
|
107 |
|
108 |
if not rows:
|
|
|
155 |
return "Error: No data table exists. Please upload a file first."
|
156 |
|
157 |
schema_info = (
|
158 |
+
f"The database has the following schema:\n"
|
159 |
f"{schema}\n"
|
160 |
"Generate a valid SQL SELECT query using ONLY these column names.\n"
|
161 |
"DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
|
|
|
241 |
gr.Markdown("""
|
242 |
# Data Query Interface
|
243 |
|
244 |
+
Upload your data file to begin.
|
245 |
+
|
246 |
+
### Supported File Types:
|
247 |
+
- SQL (.sql): SQL file containing CREATE TABLE and INSERT statements
|
248 |
+
- CSV (.csv): CSV file with headers that will be automatically converted to a table
|
249 |
|
250 |
+
### CSV Requirements:
|
251 |
+
- Must include headers
|
252 |
- First column will be used as the primary key
|
253 |
+
- Column types will be automatically detected
|
254 |
+
|
255 |
+
### SQL Requirements:
|
256 |
+
- Must contain valid SQL statements
|
257 |
+
- Statements must be separated by semicolons
|
258 |
+
- Should include CREATE TABLE and data insertion statements
|
259 |
""")
|
260 |
|
261 |
file_input = gr.File(
|
262 |
+
label="Upload Data File",
|
263 |
+
file_types=[".csv", ".sql"],
|
264 |
type="filepath"
|
265 |
)
|
266 |
status = gr.Textbox(label="Status", interactive=False)
|