
    h
                     >    d dl Z d dlZd dlZd dlZd dlmZ d dlZd Zy)    N)Imagec                    	 ddl m} ddl}t        j                  j                         st        d      d}d}t        d       |j                  |t        j                        }|j                  |      }d	| j                         vr| d
z  } t        d|          ||       j                  d   }t        j                  j                  t!        j"                         d      }|j%                  |       t        d       t'        j(                  |      }	|j+                  |	      }
t'        j,                  d|
j.                  d      }t'        j0                  ||
      }|j3                  d      }|j%                  |       t        j*                  |       t        d|        |S # t4        $ r}t        dt7        |               d}~ww xY w)a  
    Generate image from text prompt using Stable Diffusion
    
    Args:
        text_prompt (str): Text prompt for image generation
        output_image_path (str): Path to save the generated image
        
    Returns:
        str: Path to generated image
    r   )StableDiffusionPipelineNz2CUDA-compatible GPU is required to run this pluginzCompVis/stable-diffusion-v1-4cudaz!Loading Stable Diffusion model...)torch_dtypezwhite backgroundz( with a completely pure white backgroundzGenerating image from prompt: ztemp_generated.pngzCleaning up background...RGBA)   r	   r	   r	   RGBz+Image successfully generated and saved to: z"Error generating image from text: )	diffusersr   rembgtorchr   is_availableRuntimeErrorprintfrom_pretrainedfloat16tolowerimagesospathjointempfile
gettempdirsaver   openremovenewsizealpha_compositeconvert	Exceptionstr)text_promptoutput_image_pathr   r   model_iddevicepipeimage	temp_pathinput_imageoutput_imagewhite_bgfinal_imagees                 1/data/cameron/tmprepos/DreamMesh/text_to_image.pygenerate_image_from_textr1   	   s   75 zz&&(STT 3 	12&66xU]]6[wwv [%6%6%88EEK 	.{m<=[!((+ GGLL!4!4!68LM	

9 	)*jj+||K0 99V\%6%68LM ++HlC "))%0 	*+ 			);<M;NOP   23q6(;<s   FF" "	G+GG)	r   sysr   r   PILr   numpynpr1        r0   <module>r8      s    	 
    Br7   