Quazim0t0 commited on
Commit
8912157
Β·
verified Β·
1 Parent(s): d4bd626

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -1,15 +1,15 @@
1
  import os
2
  import gradio as gr
3
- from sqlalchemy import text, inspect
4
  from smolagents import tool, CodeAgent, HfApiModel
5
  import pandas as pd
6
  import tempfile
7
  from database import engine, initialize_database
8
 
9
- # Ensure the database initializes
10
  initialize_database()
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.
@@ -25,14 +25,14 @@ def execute_sql_script(file_path):
25
  with open(file_path, "r") as f:
26
  sql_script = f.read()
27
  con.execute(text(sql_script))
28
- return "SQL file executed successfully."
29
  except Exception as e:
30
  return f"Error: {str(e)}"
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.
@@ -40,7 +40,7 @@ def get_table_names():
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.
@@ -55,10 +55,10 @@ def get_table_schema(table_name):
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.
@@ -80,17 +80,17 @@ def get_table_data(table_name):
80
  except Exception as e:
81
  return pd.DataFrame({"Error": [str(e)]})
82
 
83
- # SQL Execution Tool (FIXED Docstring for `smolagents`)
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 string to execute.
91
 
92
  Returns:
93
- str: The formatted query results, or an error message if the query fails.
94
  """
95
  try:
96
  with engine.connect() as con:
@@ -101,16 +101,16 @@ def sql_engine(query: str) -> str:
101
  except Exception as e:
102
  return f"Error: {str(e)}"
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:
@@ -144,10 +144,10 @@ def handle_query(user_input: str) -> str:
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.
@@ -197,3 +197,4 @@ with gr.Blocks() as demo:
197
  if __name__ == "__main__":
198
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
199
 
 
 
1
  import os
2
  import gradio as gr
3
+ from sqlalchemy import text, inspect, create_engine
4
  from smolagents import tool, CodeAgent, HfApiModel
5
  import pandas as pd
6
  import tempfile
7
  from database import engine, initialize_database
8
 
9
+ # Ensure the database initializes (won't crash if empty)
10
  initialize_database()
11
 
12
+ # Function to execute an uploaded SQL script
13
  def execute_sql_script(file_path):
14
  """
15
  Executes an uploaded SQL file to initialize the database.
 
25
  with open(file_path, "r") as f:
26
  sql_script = f.read()
27
  con.execute(text(sql_script))
28
+ return "SQL file executed successfully. Database updated."
29
  except Exception as e:
30
  return f"Error: {str(e)}"
31
 
32
+ # Function to get table names dynamically
33
  def get_table_names():
34
  """
35
+ Returns a list of tables available in the database.
36
 
37
  Returns:
38
  list: List of table names.
 
40
  inspector = inspect(engine)
41
  return inspector.get_table_names()
42
 
43
+ # Function to get table schema dynamically
44
  def get_table_schema(table_name):
45
  """
46
  Returns a list of column names for a given table.
 
55
  columns = inspector.get_columns(table_name)
56
  return [col["name"] for col in columns]
57
 
58
+ # Function to fetch data dynamically from any table
59
  def get_table_data(table_name):
60
  """
61
+ Retrieves all rows from a specified table as a Pandas DataFrame.
62
 
63
  Args:
64
  table_name (str): Name of the table.
 
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:
 
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:
 
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
  """
150
+ Handles SQL file upload, executes SQL, and updates database schema.
151
 
152
  Args:
153
  file (File): Uploaded SQL file.
 
197
  if __name__ == "__main__":
198
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
199
 
200
+