Quazim0t0 commited on
Commit
08d132d
ยท
verified ยท
1 Parent(s): 6d10b4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -1
app.py CHANGED
@@ -12,6 +12,37 @@ from database import (
12
  get_table_schema
13
  )
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def process_sql_file(file_path):
16
  """
17
  Process an SQL file and execute its contents.
@@ -41,7 +72,121 @@ def process_sql_file(file_path):
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:
 
12
  get_table_schema
13
  )
14
 
15
+ def get_data_table():
16
+ """
17
+ Fetches all data from the current table and returns it as a Pandas DataFrame.
18
+ """
19
+ try:
20
+ # Get list of tables
21
+ with engine.connect() as con:
22
+ tables = con.execute(text(
23
+ "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
24
+ )).fetchall()
25
+
26
+ if not tables:
27
+ return pd.DataFrame()
28
+
29
+ # Use the first table found
30
+ table_name = tables[0][0]
31
+
32
+ with engine.connect() as con:
33
+ result = con.execute(text(f"SELECT * FROM {table_name}"))
34
+ rows = result.fetchall()
35
+
36
+ if not rows:
37
+ return pd.DataFrame()
38
+
39
+ columns = result.keys()
40
+ df = pd.DataFrame(rows, columns=columns)
41
+ return df
42
+
43
+ except Exception as e:
44
+ return pd.DataFrame({"Error": [str(e)]})
45
+
46
  def process_sql_file(file_path):
47
  """
48
  Process an SQL file and execute its contents.
 
72
  except Exception as e:
73
  return False, f"Error processing SQL file: {str(e)}"
74
 
75
+ def process_csv_file(file_path):
76
+ """
77
+ Process a CSV file and load it into the database.
78
+ """
79
+ try:
80
+ # Read the CSV file
81
+ df = pd.read_csv(file_path)
82
+
83
+ if len(df.columns) == 0:
84
+ return False, "Error: File contains no columns"
85
+
86
+ # Clear existing database and create new table
87
+ clear_database()
88
+ table = create_dynamic_table(df)
89
+
90
+ # Convert DataFrame to list of dictionaries and insert
91
+ records = df.to_dict('records')
92
+ insert_rows_into_table(records, table)
93
+
94
+ return True, "CSV file successfully loaded! Click 'Continue' to proceed to query interface..."
95
+
96
+ except Exception as e:
97
+ return False, f"Error processing CSV file: {str(e)}"
98
+
99
+ def process_uploaded_file(file):
100
+ """
101
+ Process the uploaded file (either SQL or CSV).
102
+ """
103
+ try:
104
+ if file is None:
105
+ return False, "Please upload a file."
106
+
107
+ # Get file extension
108
+ file_ext = os.path.splitext(file)[1].lower()
109
+
110
+ if file_ext == '.sql':
111
+ return process_sql_file(file)
112
+ elif file_ext == '.csv':
113
+ return process_csv_file(file)
114
+ else:
115
+ return False, "Error: Unsupported file type. Please upload either a .sql or .csv file."
116
+
117
+ except Exception as e:
118
+ return False, f"Error processing file: {str(e)}"
119
+
120
+ @tool
121
+ def sql_engine(query: str) -> str:
122
+ """
123
+ Executes an SQL query and returns formatted results.
124
+
125
+ Args:
126
+ query: The SQL query string to execute on the database. Must be a valid SELECT query.
127
+
128
+ Returns:
129
+ str: The formatted query results as a string.
130
+ """
131
+ try:
132
+ with engine.connect() as con:
133
+ rows = con.execute(text(query)).fetchall()
134
+
135
+ if not rows:
136
+ return "No results found."
137
+
138
+ if len(rows) == 1 and len(rows[0]) == 1:
139
+ return str(rows[0][0])
140
+
141
+ return "\n".join([", ".join(map(str, row)) for row in rows])
142
+
143
+ except Exception as e:
144
+ return f"Error: {str(e)}"
145
+
146
+ agent = CodeAgent(
147
+ tools=[sql_engine],
148
+ model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
149
+ )
150
+
151
+ def query_sql(user_query: str) -> str:
152
+ """
153
+ Converts natural language input to an SQL query using CodeAgent.
154
+ """
155
+ schema = get_table_schema()
156
+ if not schema:
157
+ return "Error: No data table exists. Please upload a file first."
158
+
159
+ schema_info = (
160
+ f"The database has the following schema:\n"
161
+ f"{schema}\n"
162
+ "Generate a valid SQL SELECT query using ONLY these column names.\n"
163
+ "DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
164
+ )
165
+
166
+ generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
167
+
168
+ if not isinstance(generated_sql, str):
169
+ return f"{generated_sql}"
170
+
171
+ if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
172
+ return "Error: Only SELECT queries are allowed."
173
+
174
+ result = sql_engine(generated_sql)
175
+
176
+ try:
177
+ float_result = float(result)
178
+ return f"{float_result:.2f}"
179
+ except ValueError:
180
+ return result
181
+
182
+ def update_schema():
183
+ """
184
+ Updates the schema display.
185
+ """
186
+ schema = get_table_schema()
187
+ if schema:
188
+ return f"### Current Schema:\n```\n{schema}\n```"
189
+ return "No data loaded"
190
 
191
  # Create the Gradio interface
192
  with gr.Blocks() as demo: