Spaces:
Running
Running
Commit
·
fd6cc91
1
Parent(s):
6c627b7
ui fix
Browse files
app.py
CHANGED
|
@@ -116,31 +116,43 @@ async def process_fal_request(request_id, model_endpoint, fal_arguments, api_key
|
|
| 116 |
def run_async_task(request_id, model_endpoint, fal_arguments, api_key):
|
| 117 |
"""Run async task with proper event loop management"""
|
| 118 |
print(f"[DEBUG run_async_task] Starting for request {request_id}")
|
|
|
|
|
|
|
|
|
|
| 119 |
try:
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
| 123 |
print(f"[DEBUG run_async_task] Completed with result keys: {result.keys() if result else 'None'}")
|
| 124 |
-
|
| 125 |
-
# Handle case where an event loop is already running (shouldn't happen with asyncio.run)
|
| 126 |
-
if "There is no current event loop" in str(e) or "Event loop is closed" in str(e):
|
| 127 |
-
print(f"[ERROR run_async_task] Event loop error, creating new loop: {str(e)}")
|
| 128 |
-
# Fallback: create a completely new event loop
|
| 129 |
-
loop = asyncio.new_event_loop()
|
| 130 |
-
try:
|
| 131 |
-
result = loop.run_until_complete(process_fal_request(request_id, model_endpoint, fal_arguments, api_key))
|
| 132 |
-
print(f"[DEBUG run_async_task] Completed with fallback loop")
|
| 133 |
-
finally:
|
| 134 |
-
loop.close()
|
| 135 |
-
else:
|
| 136 |
-
print(f"[ERROR run_async_task] Runtime error: {str(e)}")
|
| 137 |
-
import traceback
|
| 138 |
-
traceback.print_exc()
|
| 139 |
-
raise
|
| 140 |
except Exception as e:
|
| 141 |
print(f"[ERROR run_async_task] Failed: {str(e)}")
|
| 142 |
import traceback
|
| 143 |
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
@app.route('/api/generate', methods=['POST'])
|
| 146 |
def generate():
|
|
@@ -299,26 +311,37 @@ async def upload_file_to_fal_async(file_path, api_key):
|
|
| 299 |
return await fal_client.upload_file_async(file_path)
|
| 300 |
|
| 301 |
def upload_to_fal_sync(file_path, api_key):
|
| 302 |
-
"""Synchronous wrapper for FAL upload
|
|
|
|
| 303 |
try:
|
| 304 |
-
#
|
| 305 |
-
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
| 307 |
return result
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
| 314 |
try:
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
finally:
|
| 319 |
loop.close()
|
| 320 |
-
|
| 321 |
-
|
| 322 |
|
| 323 |
@app.route('/api/upload-to-fal', methods=['POST'])
|
| 324 |
def upload_to_fal():
|
|
|
|
| 116 |
def run_async_task(request_id, model_endpoint, fal_arguments, api_key):
|
| 117 |
"""Run async task with proper event loop management"""
|
| 118 |
print(f"[DEBUG run_async_task] Starting for request {request_id}")
|
| 119 |
+
|
| 120 |
+
# Always create a fresh event loop for each task to avoid conflicts
|
| 121 |
+
loop = None
|
| 122 |
try:
|
| 123 |
+
# Create a new event loop for this thread
|
| 124 |
+
loop = asyncio.new_event_loop()
|
| 125 |
+
asyncio.set_event_loop(loop)
|
| 126 |
+
|
| 127 |
+
# Run the async task in the new loop
|
| 128 |
+
result = loop.run_until_complete(process_fal_request(request_id, model_endpoint, fal_arguments, api_key))
|
| 129 |
print(f"[DEBUG run_async_task] Completed with result keys: {result.keys() if result else 'None'}")
|
| 130 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
except Exception as e:
|
| 132 |
print(f"[ERROR run_async_task] Failed: {str(e)}")
|
| 133 |
import traceback
|
| 134 |
traceback.print_exc()
|
| 135 |
+
# Update the request status to error
|
| 136 |
+
if request_id in active_requests:
|
| 137 |
+
active_requests[request_id]['status'] = 'error'
|
| 138 |
+
active_requests[request_id]['error'] = str(e)
|
| 139 |
+
finally:
|
| 140 |
+
# Always close and cleanup the event loop
|
| 141 |
+
if loop:
|
| 142 |
+
try:
|
| 143 |
+
# Cancel any remaining tasks
|
| 144 |
+
pending = asyncio.all_tasks(loop)
|
| 145 |
+
for task in pending:
|
| 146 |
+
task.cancel()
|
| 147 |
+
# Run until all tasks are cancelled
|
| 148 |
+
if pending:
|
| 149 |
+
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
|
| 150 |
+
except Exception as cleanup_error:
|
| 151 |
+
print(f"[WARNING] Error during loop cleanup: {cleanup_error}")
|
| 152 |
+
finally:
|
| 153 |
+
loop.close()
|
| 154 |
+
# Clear the event loop from the thread
|
| 155 |
+
asyncio.set_event_loop(None)
|
| 156 |
|
| 157 |
@app.route('/api/generate', methods=['POST'])
|
| 158 |
def generate():
|
|
|
|
| 311 |
return await fal_client.upload_file_async(file_path)
|
| 312 |
|
| 313 |
def upload_to_fal_sync(file_path, api_key):
|
| 314 |
+
"""Synchronous wrapper for FAL upload with isolated event loop"""
|
| 315 |
+
loop = None
|
| 316 |
try:
|
| 317 |
+
# Always create a fresh event loop for uploads to avoid conflicts
|
| 318 |
+
loop = asyncio.new_event_loop()
|
| 319 |
+
asyncio.set_event_loop(loop)
|
| 320 |
+
|
| 321 |
+
# Run the upload in the new loop
|
| 322 |
+
result = loop.run_until_complete(upload_file_to_fal_async(file_path, api_key))
|
| 323 |
return result
|
| 324 |
+
|
| 325 |
+
except Exception as e:
|
| 326 |
+
print(f"[ERROR upload_to_fal_sync] Upload failed: {str(e)}")
|
| 327 |
+
raise
|
| 328 |
+
finally:
|
| 329 |
+
# Always close and cleanup the event loop
|
| 330 |
+
if loop:
|
| 331 |
try:
|
| 332 |
+
# Cancel any remaining tasks
|
| 333 |
+
pending = asyncio.all_tasks(loop)
|
| 334 |
+
for task in pending:
|
| 335 |
+
task.cancel()
|
| 336 |
+
# Run until all tasks are cancelled
|
| 337 |
+
if pending:
|
| 338 |
+
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
|
| 339 |
+
except Exception as cleanup_error:
|
| 340 |
+
print(f"[WARNING] Error during upload loop cleanup: {cleanup_error}")
|
| 341 |
finally:
|
| 342 |
loop.close()
|
| 343 |
+
# Clear the event loop from the thread
|
| 344 |
+
asyncio.set_event_loop(None)
|
| 345 |
|
| 346 |
@app.route('/api/upload-to-fal', methods=['POST'])
|
| 347 |
def upload_to_fal():
|