Abid Ali Awan commited on
Commit
12e2867
·
1 Parent(s): 697bdf0

feat: Add file size check and warnings in handle_upload function to improve user experience with large datasets

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -181,12 +181,34 @@ def extract_output_text(response) -> str:
181
  def handle_upload(file_path, request: gr.Request):
182
  """
183
  1) Take uploaded file path (string)
184
- 2) Copy to /tmp for a stable path
185
- 3) Build a public Gradio file URL that the MCP server can fetch via HTTP
 
186
  """
187
  if not file_path:
188
  return None
189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  local_path = file_path
191
  stable_path = os.path.join("/tmp", os.path.basename(local_path))
192
  try:
@@ -197,7 +219,7 @@ def handle_upload(file_path, request: gr.Request):
197
  pass
198
 
199
  base_url = str(request.base_url).rstrip("/")
200
- public_url = f"{base_url}/gradio_api/file={local_path}"
201
  return public_url
202
 
203
 
@@ -440,4 +462,5 @@ if __name__ == "__main__":
440
  allowed_paths=["/tmp"],
441
  ssr_mode=False,
442
  show_error=True,
 
443
  )
 
181
  def handle_upload(file_path, request: gr.Request):
182
  """
183
  1) Take uploaded file path (string)
184
+ 2) Check file size and show warnings for large datasets
185
+ 3) Copy to /tmp for a stable path
186
+ 4) Build a public Gradio file URL that the MCP server can fetch via HTTP
187
  """
188
  if not file_path:
189
  return None
190
 
191
+ # Check file size and add warning if > 1.5MB
192
+ url_params = ""
193
+ try:
194
+ file_size = os.path.getsize(file_path)
195
+ file_size_mb = file_size / (1024 * 1024) # Convert to MB
196
+
197
+ if file_size_mb > 1.5:
198
+ # Show Gradio warning with 5-second auto-dismiss
199
+ gr.Warning(
200
+ f"Large dataset detected! Your file is {file_size_mb:.1f}MB.",
201
+ duration=5,
202
+ )
203
+ gr.Warning(
204
+ "For optimal performance, training will use only the first 10,000 rows.",
205
+ duration=10,
206
+ )
207
+
208
+ except Exception:
209
+ # If we can't check file size, continue without warnings
210
+ pass
211
+
212
  local_path = file_path
213
  stable_path = os.path.join("/tmp", os.path.basename(local_path))
214
  try:
 
219
  pass
220
 
221
  base_url = str(request.base_url).rstrip("/")
222
+ public_url = f"{base_url}/gradio_api/file={local_path}{url_params}"
223
  return public_url
224
 
225
 
 
462
  allowed_paths=["/tmp"],
463
  ssr_mode=False,
464
  show_error=True,
465
+ max_file_size="10mb",
466
  )