Quazim0t0 commited on
Commit
fed63c4
·
verified ·
1 Parent(s): e566b0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -58
app.py CHANGED
@@ -15,12 +15,6 @@ from database import (
15
  def process_uploaded_file(file):
16
  """
17
  Process the uploaded CSV file and load it into the database.
18
-
19
- Args:
20
- file: Path to the uploaded file
21
-
22
- Returns:
23
- tuple: (success_flag, message)
24
  """
25
  try:
26
  if file is None:
@@ -57,7 +51,6 @@ def get_data_table():
57
  if not rows:
58
  return pd.DataFrame()
59
 
60
- # Get column names from the result
61
  columns = result.keys()
62
  df = pd.DataFrame(rows, columns=columns)
63
  return df
@@ -74,9 +67,7 @@ def sql_engine(query: str) -> str:
74
  query: The SQL query string to execute on the database. Must be a valid SELECT query.
75
 
76
  Returns:
77
- str: The formatted query results as a string. For single value results, returns the value.
78
- For multiple rows/columns, returns newline-separated rows with comma-separated values.
79
- Returns error message as string if query execution fails.
80
  """
81
  try:
82
  with engine.connect() as con:
@@ -100,8 +91,7 @@ agent = CodeAgent(
100
 
101
  def query_sql(user_query: str) -> str:
102
  """
103
- Converts natural language input to an SQL query using CodeAgent
104
- and returns the execution results.
105
  """
106
  schema = get_table_schema()
107
  if not schema:
@@ -130,44 +120,9 @@ def query_sql(user_query: str) -> str:
130
  except ValueError:
131
  return result
132
 
133
- # Create the upload interface
134
- def create_upload_interface():
135
- with gr.Blocks() as upload_interface:
136
- gr.Markdown("""
137
- # Data Query Interface
138
-
139
- Upload your CSV file to begin.
140
-
141
- ### Requirements:
142
- - File must be in CSV format
143
- - First column will be used as the primary key
144
- - All columns will be automatically typed based on their content
145
- """)
146
-
147
- file_input = gr.File(
148
- label="Upload CSV File",
149
- file_types=[".csv"],
150
- type="filepath" # Changed from 'file' to 'filepath'
151
- )
152
- status = gr.Textbox(label="Status", interactive=False)
153
-
154
- def handle_upload(file):
155
- success, message = process_uploaded_file(file)
156
- if success:
157
- return message, gr.Blocks.update(visible=False), gr.Blocks.update(visible=True)
158
- return message, gr.Blocks.update(visible=True), gr.Blocks.update(visible=False)
159
-
160
- file_input.upload(
161
- fn=handle_upload,
162
- inputs=[file_input],
163
- outputs=[status, upload_interface, query_interface]
164
- )
165
-
166
- return upload_interface
167
-
168
- # Create the query interface
169
- def create_query_interface():
170
- with gr.Blocks(visible=False) as query_interface:
171
  gr.Markdown("""
172
  ## Data Query Interface
173
 
@@ -188,7 +143,6 @@ def create_query_interface():
188
  interactive=False
189
  )
190
 
191
- # Show current schema
192
  schema_display = gr.Markdown(value="Loading schema...")
193
 
194
  def update_schema():
@@ -203,7 +157,6 @@ def create_query_interface():
203
  outputs=[query_output]
204
  )
205
 
206
- # Add refresh buttons
207
  with gr.Row():
208
  refresh_table_btn = gr.Button("Refresh Table")
209
  refresh_schema_btn = gr.Button("Refresh Schema")
@@ -218,18 +171,45 @@ def create_query_interface():
218
  outputs=[schema_display]
219
  )
220
 
221
- # Update schema on load
222
  query_interface.load(
223
  fn=update_schema,
224
  outputs=[schema_display]
225
  )
226
 
227
- return query_interface
 
 
 
 
 
 
228
 
229
- # Create the main interface
230
- with gr.Blocks() as demo:
231
- upload_interface = create_upload_interface()
232
- query_interface = create_query_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
  if __name__ == "__main__":
235
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
15
  def process_uploaded_file(file):
16
  """
17
  Process the uploaded CSV file and load it into the database.
 
 
 
 
 
 
18
  """
19
  try:
20
  if file is None:
 
51
  if not rows:
52
  return pd.DataFrame()
53
 
 
54
  columns = result.keys()
55
  df = pd.DataFrame(rows, columns=columns)
56
  return df
 
67
  query: The SQL query string to execute on the database. Must be a valid SELECT query.
68
 
69
  Returns:
70
+ str: The formatted query results as a string.
 
 
71
  """
72
  try:
73
  with engine.connect() as con:
 
91
 
92
  def query_sql(user_query: str) -> str:
93
  """
94
+ Converts natural language input to an SQL query using CodeAgent.
 
95
  """
96
  schema = get_table_schema()
97
  if not schema:
 
120
  except ValueError:
121
  return result
122
 
123
+ with gr.Blocks() as demo:
124
+ # First create both interfaces
125
+ with gr.Blocks() as query_interface:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  gr.Markdown("""
127
  ## Data Query Interface
128
 
 
143
  interactive=False
144
  )
145
 
 
146
  schema_display = gr.Markdown(value="Loading schema...")
147
 
148
  def update_schema():
 
157
  outputs=[query_output]
158
  )
159
 
 
160
  with gr.Row():
161
  refresh_table_btn = gr.Button("Refresh Table")
162
  refresh_schema_btn = gr.Button("Refresh Schema")
 
171
  outputs=[schema_display]
172
  )
173
 
 
174
  query_interface.load(
175
  fn=update_schema,
176
  outputs=[schema_display]
177
  )
178
 
179
+ # Set query interface initially invisible
180
+ query_interface.visible = False
181
+
182
+ # Create upload interface
183
+ with gr.Blocks() as upload_interface:
184
+ gr.Markdown("""
185
+ # Data Query Interface
186
 
187
+ Upload your CSV file to begin.
188
+
189
+ ### Requirements:
190
+ - File must be in CSV format
191
+ - First column will be used as the primary key
192
+ - All columns will be automatically typed based on their content
193
+ """)
194
+
195
+ file_input = gr.File(
196
+ label="Upload CSV File",
197
+ file_types=[".csv"],
198
+ type="filepath"
199
+ )
200
+ status = gr.Textbox(label="Status", interactive=False)
201
+
202
+ def handle_upload(file):
203
+ success, message = process_uploaded_file(file)
204
+ if success:
205
+ return message, gr.Blocks.update(visible=False), gr.Blocks.update(visible=True)
206
+ return message, gr.Blocks.update(visible=True), gr.Blocks.update(visible=False)
207
+
208
+ file_input.upload(
209
+ fn=handle_upload,
210
+ inputs=[file_input],
211
+ outputs=[status, upload_interface, query_interface]
212
+ )
213
 
214
  if __name__ == "__main__":
215
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)