from celery import Celery from flask import current_app from app2 import app, sse # Import your Flask app and SSE import uuid import os # import threading celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) # OUTPUT_FOLDER = 'path/to/output/folder' # image_results = {} # image_results_lock = threading.Lock() # def create_progress_callback(session_id): # def progress_callback(step, total_steps): # progress = int((step + 1) / total_steps * 100) # print(f"Publishing progress {progress} for session {session_id}") # sse.publish({"progress": progress}, type='progress', channel=session_id) # return progress_callback @celery.task(bind=True) def process_image(self, session_id, garm_path, vton_path, category): try: print(f"Starting process_image task for session {session_id}") progress_callback = create_progress_callback(session_id) output_images = process_dc(garm_img=garm_path, vton_img=vton_path, category=category, progress_callback=progress_callback) if not output_images: sse.publish({"error": "No output image generated"}, type='error', channel=session_id) return None output_image = output_images[0] # Generate a UUID for the output image image_uuid = str(uuid.uuid4()) # Create the output filename with the UUID output_filename = f"{image_uuid}.png" output_path = os.path.join(OUTPUT_FOLDER, output_filename) # Save the output image output_image.save(output_path, format='PNG') # Add the UUID and path to the image_results map with image_results_lock: image_results[image_uuid] = output_path sse.publish({"message": "Processing complete", "uuid": image_uuid}, type='complete', channel=session_id) return image_uuid except Exception as e: sse.publish({"error": str(e)}, type='error', channel=session_id) return print(f"panic in process_image: {str(e)}")