bibibi12345 commited on
Commit
74c8c36
·
1 Parent(s): f754d60

change from base64 to file upload

Browse files
Files changed (1) hide show
  1. app.py +42 -19
app.py CHANGED
@@ -293,26 +293,36 @@ def upload_file():
293
  print(f"Error in upload endpoint: {str(e)}")
294
  return jsonify({'error': str(e)}), 500
295
 
296
- async def upload_file_to_fal(file_path, api_key):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
297
  """Helper function to upload a file to FAL storage"""
298
  os.environ['FAL_KEY'] = api_key
299
  return await fal_client.upload_file_async(file_path)
300
 
301
- def run_upload_task(file_path, api_key):
302
- """Run the upload task with proper event loop management"""
303
- try:
304
- # Create a new event loop for this thread
305
- loop = asyncio.new_event_loop()
306
- asyncio.set_event_loop(loop)
307
- try:
308
- result = loop.run_until_complete(upload_file_to_fal(file_path, api_key))
309
- return result
310
- finally:
311
- loop.close()
312
- except Exception as e:
313
- print(f"Error in run_upload_task: {str(e)}")
314
- raise
315
-
316
  @app.route('/api/upload-to-fal', methods=['POST'])
317
  def upload_to_fal():
318
  """Upload base64 image data to FAL storage and return the URL"""
@@ -345,13 +355,26 @@ def upload_to_fal():
345
  tmp_file_path = tmp_file.name
346
 
347
  try:
348
- # Upload to FAL using the helper function with proper event loop management
349
- fal_url = run_upload_task(tmp_file_path, api_key)
 
 
 
 
 
 
 
 
 
 
350
  print(f"[DEBUG] Uploaded to FAL: {fal_url}")
351
  return jsonify({'url': fal_url}), 200
352
  finally:
353
  # Clean up temporary file
354
- os.unlink(tmp_file_path)
 
 
 
355
  else:
356
  # If it's already a URL, return it as-is
357
  return jsonify({'url': image_data}), 200
 
293
  print(f"Error in upload endpoint: {str(e)}")
294
  return jsonify({'error': str(e)}), 500
295
 
296
+ # Global variable to store the event loop for FAL uploads
297
+ fal_upload_loop = None
298
+ fal_upload_thread = None
299
+
300
+ def get_or_create_fal_loop():
301
+ """Get or create a persistent event loop for FAL uploads"""
302
+ global fal_upload_loop, fal_upload_thread
303
+
304
+ if fal_upload_loop is None or not fal_upload_loop.is_running():
305
+ # Create new event loop in a separate thread
306
+ fal_upload_loop = asyncio.new_event_loop()
307
+
308
+ def run_loop():
309
+ asyncio.set_event_loop(fal_upload_loop)
310
+ fal_upload_loop.run_forever()
311
+
312
+ fal_upload_thread = Thread(target=run_loop, daemon=True)
313
+ fal_upload_thread.start()
314
+
315
+ # Give the loop a moment to start
316
+ import time
317
+ time.sleep(0.1)
318
+
319
+ return fal_upload_loop
320
+
321
+ async def upload_file_to_fal_async(file_path, api_key):
322
  """Helper function to upload a file to FAL storage"""
323
  os.environ['FAL_KEY'] = api_key
324
  return await fal_client.upload_file_async(file_path)
325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  @app.route('/api/upload-to-fal', methods=['POST'])
327
  def upload_to_fal():
328
  """Upload base64 image data to FAL storage and return the URL"""
 
355
  tmp_file_path = tmp_file.name
356
 
357
  try:
358
+ # Get or create the persistent event loop
359
+ loop = get_or_create_fal_loop()
360
+
361
+ # Schedule the upload coroutine and wait for result
362
+ future = asyncio.run_coroutine_threadsafe(
363
+ upload_file_to_fal_async(tmp_file_path, api_key),
364
+ loop
365
+ )
366
+
367
+ # Wait for the result with a timeout
368
+ fal_url = future.result(timeout=30)
369
+
370
  print(f"[DEBUG] Uploaded to FAL: {fal_url}")
371
  return jsonify({'url': fal_url}), 200
372
  finally:
373
  # Clean up temporary file
374
+ try:
375
+ os.unlink(tmp_file_path)
376
+ except:
377
+ pass
378
  else:
379
  # If it's already a URL, return it as-is
380
  return jsonify({'url': image_data}), 200