
    bi              	       &   d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
mZ d dlZd dlZd dlZddlmZ ddlmZmZ 	 ddeeej,                  j,                  f   deeej,                  j,                  gej,                  j,                  f      d	ej,                  j,                  fd
Z	 ddedeeeej,                  j,                     geej,                  j,                     f      d	eej,                  j,                     fdZded	eeef   fdZdefdZy)    N)AnyCallableListOptionalTupleUnion)unquoteurlparse   )DIFFUSERS_REQUEST_TIMEOUT)BACKENDS_MAPPINGis_imageio_availableimageconvert_methodreturnc                 j   t        | t              r| j                  d      s| j                  d      rDt        j                  j                  t        j                  | dt              j                        } nt        j                  j                  |       r t        j                  j                  |       } nAt        d|  d      t        | t        j                  j                        r| } nt        d      t        j                  j                  |       } |
 ||       } | S | j!                  d      } | S )	a  
    Loads `image` to a PIL Image.

    Args:
        image (`str` or `PIL.Image.Image`):
            The image to convert to the PIL Image format.
        convert_method (Callable[[PIL.Image.Image], PIL.Image.Image], *optional*):
            A conversion method to apply to the image after loading it. When set to `None` the image will be converted
            "RGB".

    Returns:
        `PIL.Image.Image`:
            A PIL Image.
    http://https://T)streamtimeoutIIncorrect path or URL. URLs must start with `http://` or `https://`, and  is not a valid path.zgIncorrect format used for the image. Should be a URL linking to an image, a local path, or a PIL image.RGB)
isinstancestr
startswithPILImageopenrequestsgetr   rawospathisfile
ValueErrorImageOpsexif_transposeconvert)r   r   s     X/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/utils/loading_utils.py
load_imager+      s    " %I&%*:*::*FIINN8<<dLe#f#j#jkEWW^^E"IINN5)E[\a[bbwx  
E399??	+u
 	
 LL''.E!u% L e$L    videoc                    | j                  d      xs | j                  d      }t        j                  j                  |       }d}|s|st	        d|  d      |rt        j                  | d      }|j                  dk7  rt	        d	|j                         t        |       }t        j                  j                  t        |j                              }t        j                  j                  |      d
   xs d}t        j                  |d      j                  }	d}|j                  d      }
t!        |	d      5 }|
D ]  }|j#                  |        	 ddd       |	} g }| j%                  d      rct&        j(                  j!                  |       }	 	 |j+                  |j-                                |j/                  |j1                         d
z          Bt5               rddl}n$t9        t:        d   d
   j=                  d            	 |j>                  j@                  jC                          |jG                  |       5 }|D ]0  }|j+                  t&        j(                  jI                  |             2 	 ddd       |rt        jJ                  	       | ||      }|S # 1 sw Y   MxY w# t2        $ r Y ;w xY w# tD        $ r tE        d      w xY w# 1 sw Y   `xY w)a  
    Loads `video` to a list of PIL Image.

    Args:
        video (`str`):
            A URL or Path to a video to convert to a list of PIL Image format.
        convert_method (Callable[[List[PIL.Image.Image]], List[PIL.Image.Image]], *optional*):
            A conversion method to apply to the video after loading it. When set to `None` the images will be converted
            to "RGB".

    Returns:
        `List[PIL.Image.Image]`:
            The video as a list of PIL images.
    r   r   Fr   r   T)r      z'Failed to download video. Status code: r   z.mp4)suffixdeletei    )
chunk_sizewbNz.gifr   imageio
load_videozf`Unable to find an ffmpeg installation on your machine. Please install via `pip install imageio-ffmpeg)&r   r#   r$   r%   r&   r    r!   status_coder
   basenamer	   splitexttempfileNamedTemporaryFilenameiter_contentr   writeendswithr   r   appendcopyseektellEOFErrorr   r4   ImportErrorr   formatpluginsffmpegget_exeAttributeError
get_reader	fromarrayremove)r-   r   is_urlis_filewas_tempfile_createdresponse
parsed_url	file_namer0   
video_path
video_datafchunk
pil_imagesgifr4   readerframes                     r*   r5   r5   9   s   $ i(HE,<,<Z,HFggnnU#G gWX]W^^st
 	
 <<d33&FxG[G[F\]^^e_
GG$$WZ__%=>	!!),Q/9600uMRR
#**d*;
*d# 	q# 	 J~~fiinnU#	!!#((*-a(   !.y9!<CCLQRR	OO""**, & 	>& >!!#))"5"5e"<=>	>
 
		*!#J/
Q	 	  		  	 x 	
	> 	>s7   >J#AJ0 $J? 6K#J-0	J<;J<?KK tensor_namec                     d|v rA|j                  d      }|d d D ]#  }t        | |      }|t        |  d| d      |} % |d   }| |fS )N.z has no attribute )splitgetattrr&   )moduler[   splitsr_   
new_modules        r*   get_module_from_namerd      su    
k""3'CR[ 	 E /J! F8+=eWA!FGGF		 
 Rj;r,   module_pathc                     | }|j                  d      }|D ]/  }|j                         rt        |      }||   }$t        ||      }1 |S )Nr]   )r_   isdigitintr`   )root_modulere   currentpartspartidxs         r*   get_submodule_by_namern      sT    Gc"E -<<>d)CclGgt,G- Nr,   )N)r#   r9   typingr   r   r   r   r   r   urllib.parser	   r
   	PIL.Imager   PIL.ImageOpsr    	constantsr   import_utilsr   r   r   r   r+   r5   rd   rn    r,   r*   <module>rv      s   	  > > *    0 @ rv(ciioo%&(8@399??J[]`]f]f]l]lJlAm8n(YY__(Z Z^QQXtCIIOO'<&=tCIIOO?T&TUVQ 
#))//Qj	c 	eCHo 		C 	r,   