Quazim0t0 commited on
Commit
28200f6
·
verified ·
1 Parent(s): 4f8fcad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -8
app.py CHANGED
@@ -11,7 +11,15 @@ initialize_database()
11
 
12
  # Function to execute SQL script from uploaded file
13
  def execute_sql_script(file_path):
14
- """Executes an uploaded SQL file to initialize the database."""
 
 
 
 
 
 
 
 
15
  try:
16
  with engine.connect() as con:
17
  with open(file_path, "r") as f:
@@ -23,20 +31,41 @@ def execute_sql_script(file_path):
23
 
24
  # Function to fetch table names dynamically
25
  def get_table_names():
26
- """Returns a list of all tables in the database."""
 
 
 
 
 
27
  inspector = inspect(engine)
28
  return inspector.get_table_names()
29
 
30
  # Function to fetch table schema dynamically
31
  def get_table_schema(table_name):
32
- """Returns a list of column names for a given table."""
 
 
 
 
 
 
 
 
33
  inspector = inspect(engine)
34
  columns = inspector.get_columns(table_name)
35
  return [col["name"] for col in columns]
36
 
37
  # Function to fetch table data dynamically
38
  def get_table_data(table_name):
39
- """Retrieves all rows from the specified table as a Pandas DataFrame."""
 
 
 
 
 
 
 
 
40
  try:
41
  with engine.connect() as con:
42
  result = con.execute(text(f"SELECT * FROM {table_name}"))
@@ -54,7 +83,15 @@ def get_table_data(table_name):
54
  # SQL Execution Tool
55
  @tool
56
  def sql_engine(query: str) -> str:
57
- """Executes an SQL SELECT query and returns formatted results."""
 
 
 
 
 
 
 
 
58
  try:
59
  with engine.connect() as con:
60
  rows = con.execute(text(query)).fetchall()
@@ -66,7 +103,15 @@ def sql_engine(query: str) -> str:
66
 
67
  # Function to generate and execute SQL queries dynamically
68
  def query_sql(user_query: str) -> str:
69
- """Processes a user’s natural language query and generates an SQL query dynamically."""
 
 
 
 
 
 
 
 
70
  tables = get_table_names()
71
  if not tables:
72
  return "Error: No tables found. Please upload an SQL file first."
@@ -88,12 +133,28 @@ def query_sql(user_query: str) -> str:
88
 
89
  # Function to handle query input
90
  def handle_query(user_input: str) -> str:
91
- """Handles user input and returns the SQL query result."""
 
 
 
 
 
 
 
 
92
  return query_sql(user_input)
93
 
94
  # Function to handle SQL file uploads
95
  def handle_file_upload(file):
96
- """Handles file upload, executes SQL, and updates database schema dynamically."""
 
 
 
 
 
 
 
 
97
  temp_file_path = tempfile.mkstemp(suffix=".sql")[1]
98
  with open(temp_file_path, "wb") as temp_file:
99
  temp_file.write(file.read())
 
11
 
12
  # Function to execute SQL script from uploaded file
13
  def execute_sql_script(file_path):
14
+ """
15
+ Executes an uploaded SQL file to initialize the database.
16
+
17
+ Args:
18
+ file_path (str): Path to the SQL file.
19
+
20
+ Returns:
21
+ str: Success message or error description.
22
+ """
23
  try:
24
  with engine.connect() as con:
25
  with open(file_path, "r") as f:
 
31
 
32
  # Function to fetch table names dynamically
33
  def get_table_names():
34
+ """
35
+ Returns a list of all tables in the database.
36
+
37
+ Returns:
38
+ list: List of table names.
39
+ """
40
  inspector = inspect(engine)
41
  return inspector.get_table_names()
42
 
43
  # Function to fetch table schema dynamically
44
  def get_table_schema(table_name):
45
+ """
46
+ Returns a list of column names for a given table.
47
+
48
+ Args:
49
+ table_name (str): Name of the table.
50
+
51
+ Returns:
52
+ list: List of column names.
53
+ """
54
  inspector = inspect(engine)
55
  columns = inspector.get_columns(table_name)
56
  return [col["name"] for col in columns]
57
 
58
  # Function to fetch table data dynamically
59
  def get_table_data(table_name):
60
+ """
61
+ Retrieves all rows from the specified table as a Pandas DataFrame.
62
+
63
+ Args:
64
+ table_name (str): Name of the table.
65
+
66
+ Returns:
67
+ pd.DataFrame: Table data or an error message.
68
+ """
69
  try:
70
  with engine.connect() as con:
71
  result = con.execute(text(f"SELECT * FROM {table_name}"))
 
83
  # SQL Execution Tool
84
  @tool
85
  def sql_engine(query: str) -> str:
86
+ """
87
+ Executes an SQL SELECT query and returns the results.
88
+
89
+ Args:
90
+ query (str): The SQL query to execute.
91
+
92
+ Returns:
93
+ str: The query results as a formatted string, or an error message.
94
+ """
95
  try:
96
  with engine.connect() as con:
97
  rows = con.execute(text(query)).fetchall()
 
103
 
104
  # Function to generate and execute SQL queries dynamically
105
  def query_sql(user_query: str) -> str:
106
+ """
107
+ Processes a user’s natural language query and generates an SQL query dynamically.
108
+
109
+ Args:
110
+ user_query (str): The question asked by the user.
111
+
112
+ Returns:
113
+ str: SQL query results or an error message.
114
+ """
115
  tables = get_table_names()
116
  if not tables:
117
  return "Error: No tables found. Please upload an SQL file first."
 
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
148
  def handle_file_upload(file):
149
+ """
150
+ Handles file upload, executes SQL, and updates database schema dynamically.
151
+
152
+ Args:
153
+ file (File): Uploaded SQL file.
154
+
155
+ Returns:
156
+ tuple: Execution result message and updated table data.
157
+ """
158
  temp_file_path = tempfile.mkstemp(suffix=".sql")[1]
159
  with open(temp_file_path, "wb") as temp_file:
160
  temp_file.write(file.read())