Spaces:
Build error
Build error
Update generate.py
Browse files12345679 unmodified lines17181920212238 unmodified lines6162636465666720 unmodified lines88899091929394959697989910016 unmodified lines11711811912012112227 unmodified lines150151152153154155156157158159# generate.py# --- VERSION 8 (FINAL) ---print("--- RUNNING GENERATE.PY VERSION 8 (FINAL) ---")import torchimport cv29 unmodified linesfrom huggingface_hub import hf_hub_downloadfrom storage3.utils import StorageExceptionimport configfrom database import supabase# --- Setup Logging ---38 unmodified linesraise RuntimeError(f"Could not initialize GenerationService: {e}") from edef generate_magic_image(self, face_images: list, gender: str, prompt: str, plan: str = 'free') -> str | None:logger.info("Starting image generation process...")full_prompt = f"{prompt}, 4k, high-resolution, photorealistic, masterpiece, single person, solo portrait, centered composition"negative_prompt = "multiple people, group photo, crowd, two faces, three faces, multiple faces, collage, ugly, deformed, blurry, low quality"20 unmodified lineslogger.info("Calling the generation pipeline...")try:# --- FINAL FIX: Create negative embeddings ---# The IP-Adapter expects both positive and negative image embeddings.# We create a zeroed-out tensor for the negative part.positive_embedding = average_embedding.unsqueeze(0)negative_embedding = torch.zeros_like(positive_embedding)# Combine them for the pipelinefinal_embedding = torch.cat([negative_embedding, positive_embedding], dim=0)output = self.pipe(16 unmodified lineslocal_path = os.path.join(temp_dir, f"{uuid.uuid4()}.png")image.save(local_path)storage_path = f"public/{os.path.basename(local_path)}"logger.info(f"Uploading {local_path} to Supabase bucket '{config.SUPABASE_BUCKET_NAME}' at path '{storage_path}'")27 unmodified linesresult_url = service.generate_magic_image(face_images=["test_face.jpg"],gender="Female",prompt="A beautiful portrait of a princess in a magical forest, fantasy art")if result_url:print(f"\n✅ Test successful! Image URL: {result_url}")else:print(f"\n❌ Test failed. Please check the new traceback logs for details.")else:12345679 unmodified lines1718192021222338 unmodified lines6263646566676820 unmodified lines899091929394959616 unmodified lines11311411511611711811912012112212327 unmodified lines151152153154155156157158159160161162# generate.py# --- VERSION 9 (with Watermarking) ---print("--- RUNNING GENERATE.PY VERSION 9 (with Watermarking) ---")import torchimport cv29 unmodified linesfrom huggingface_hub import hf_hub_downloadfrom storage3.utils import StorageExceptionimport configimport utils # <-- IMPORT THE NEW UTILS FILEfrom database import supabase# --- Setup Logging ---38 unmodified linesraise RuntimeError(f"Could not initialize GenerationService: {e}") from edef generate_magic_image(self, face_images: list, gender: str, prompt: str, plan: str = 'free') -> str | None:logger.info(f"Starting image generation process for a user on the '{plan}' plan.")full_prompt = f"{prompt}, 4k, high-resolution, photorealistic, masterpiece, single person, solo portrait, centered composition"negative_prompt = "multiple people, group photo, crowd, two faces, three faces, multiple faces, collage, ugly, deformed, blurry, low quality"20 unmodified lineslogger.info("Calling the generation pipeline...")try:positive_embedding = average_embedding.unsqueeze(0)negative_embedding = torch.zeros_like(positive_embedding)final_embedding = torch.cat([negative_embedding, positive_embedding], dim=0)output = self.pipe(16 unmodified lineslocal_path = os.path.join(temp_dir, f"{uuid.uuid4()}.png")image.save(local_path)# --- APPLY WATERMARK FOR FREE USERS ---if plan == 'free':utils.add_watermark(local_path, "@MagicFaceBot")# --- Upload to Supabase Storage ---storage_path = f"public/{os.path.basename(local_path)}"logger.info(f"Uploading {local_path} to Supabase bucket '{config.SUPABASE_BUCKET_NAME}' at path '{storage_path}'")27 unmodified linesresult_url = service.generate_magic_image(face_images=["test_face.jpg"],gender="Female",prompt="A beautiful portrait of a princess in a magical forest, fantasy art",plan='free' # <-- Test the 'free' plan to see the watermark)if result_url:print(f"\n✅ Test successful! Image URL: {result_url}")print("Check the image at the URL to see if it has a watermark.")else:print(f"\n❌ Test failed. Please check the new traceback logs for details.")else:
generate.py+12-9