Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -80,70 +80,6 @@ def get_table_data(table_name):
|
|
80 |
except Exception as e:
|
81 |
return pd.DataFrame({"Error": [str(e)]})
|
82 |
|
83 |
-
# SQL Execution Tool for natural language queries
|
84 |
-
@tool
|
85 |
-
def sql_engine(query: str) -> str:
|
86 |
-
"""
|
87 |
-
Executes an SQL SELECT query and returns the results.
|
88 |
-
|
89 |
-
Parameters:
|
90 |
-
query (str): The SQL query string to execute. Only SELECT queries are allowed.
|
91 |
-
|
92 |
-
Returns:
|
93 |
-
str: A formatted string containing the query results, or an error message if the query fails.
|
94 |
-
"""
|
95 |
-
try:
|
96 |
-
with engine.connect() as con:
|
97 |
-
rows = con.execute(text(query)).fetchall()
|
98 |
-
if not rows:
|
99 |
-
return "No results found."
|
100 |
-
return "\n".join([", ".join(map(str, row)) for row in rows])
|
101 |
-
except Exception as e:
|
102 |
-
return f"Error: {str(e)}"
|
103 |
-
|
104 |
-
# Function to handle natural language to SQL conversion
|
105 |
-
def query_sql(user_query: str) -> str:
|
106 |
-
"""
|
107 |
-
Converts a user's natural language query into an SQL query.
|
108 |
-
|
109 |
-
Args:
|
110 |
-
user_query (str): The question asked by the user.
|
111 |
-
|
112 |
-
Returns:
|
113 |
-
str: The results of the executed SQL query.
|
114 |
-
"""
|
115 |
-
tables = get_table_names()
|
116 |
-
if not tables:
|
117 |
-
return "Error: No tables found. Please upload an SQL file first."
|
118 |
-
|
119 |
-
schema_info = "Available tables and columns:\n"
|
120 |
-
|
121 |
-
for table in tables:
|
122 |
-
columns = get_table_schema(table)
|
123 |
-
schema_info += f"Table '{table}' has columns: {', '.join(columns)}.\n"
|
124 |
-
|
125 |
-
schema_info += "Generate a valid SQL SELECT query using ONLY these column names. DO NOT return anything other than the SQL query itself."
|
126 |
-
|
127 |
-
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
128 |
-
|
129 |
-
if not isinstance(generated_sql, str) or not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
|
130 |
-
return "Error: Only SELECT queries are allowed."
|
131 |
-
|
132 |
-
return sql_engine(generated_sql)
|
133 |
-
|
134 |
-
# Function to handle query input
|
135 |
-
def handle_query(user_input: str) -> str:
|
136 |
-
"""
|
137 |
-
Handles user input and returns the SQL query result.
|
138 |
-
|
139 |
-
Args:
|
140 |
-
user_input (str): User's natural language query.
|
141 |
-
|
142 |
-
Returns:
|
143 |
-
str: The query result or error message.
|
144 |
-
"""
|
145 |
-
return query_sql(user_input)
|
146 |
-
|
147 |
# Function to handle SQL file uploads and execute them
|
148 |
def handle_file_upload(file):
|
149 |
"""
|
|
|
80 |
except Exception as e:
|
81 |
return pd.DataFrame({"Error": [str(e)]})
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# Function to handle SQL file uploads and execute them
|
84 |
def handle_file_upload(file):
|
85 |
"""
|