
    ;3h!                       d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlZd dlZd dlZd dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ ej8                  j;                          	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZddZ ddZ!ddZ"dddZ#d dZ$d!dZ%d"dZ&d"dZ'	 	 	 	 	 	 	 	 	 	 	 	 d#dZ(	 	 	 	 	 	 	 	 d$dZ)y)%    )annotationsN)BytesIO)Path)Literalcast)quote)get_mimetypeis_http_url_like)ImageOps)processing_utils)	ImageData)Errorc                    | | S |dk(  r| S |dk(  rt        j                  |       S |dk(  r	 t        j                  | |||      }|S t        dt        |      z   dz         # t        t
        f$ r t        j                  | ||d      }Y |S w xY w)z3Helper method to format an image based on self.typepilnumpyfilepath)	cache_dirnameformatpngzUnknown type: z1. Please choose from: 'numpy', 'pil', 'filepath'.)nparrayr   save_pil_to_cacheKeyError
ValueErrorstr)imtyper   r   r   paths         R/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/gradio/image_utils.pyformat_imager!      s     
z	u}		xx|			#55id6D $iAB
 	
 *% 	#55#	D 	s   A! !(BBc                   t        | t        j                        rt        j                  | ||      }|S t        | t
        j                  j                        r	 t        j                  | ||      }|S t        | t              rt        |       }|S t        | t              r| }|S t        dt        t        |             z         # t        t        f$ r t        j                  | |d      }Y |S w xY w)Nr   r   r   z6Cannot process this value as an Image, it is of type: )
isinstancer   ndarrayr   save_img_array_to_cachePILImager   r   r   r   r   r   )yr   r   r   s       r    
save_imager*   ;   s     !RZZ 776
, K' 
Asyy	'	#55YvD" K 
At	1v K 
As	 K	 Ds4PQ7|S
 	
 *% 	#55YuD K	s   C 'C0/C0c           	         | j                   \  }}||z  }||z  |kD  r|}||z  }n|}||z  }||z
  dz  }||z
  dz  }	| j                  t        |      t        |	      t        ||z         t        |	|z         f      }
|
j                  ||f      }|S )N   )sizecropintresize)imgfinal_widthfinal_heightoriginal_widthoriginal_heighttarget_aspect_ratiocrop_height
crop_widthlefttopimg_croppedimg_resizeds               r    
crop_scaler=   X   s    &)hh#NO%4'*==% #66
#
 #66Z'1,D[(A
-C((	TCHc$"34c#:K6LMK $$k<%@AK    c                l   t        j                  |       }t        j                  j	                  t        t        j                  |                  }	 t        t        d      rt        j                  |      }t        t        j                  j                  |      S # t        $ r t        d|       Y ;w xY w)Nexif_transposez0Failed to transpose image %s based on EXIF data.)r   extract_base64_datar'   r(   openr   base64	b64decodehasattrr   r@   	Exceptionprintr   )encodingimage_encodedr1   s      r    decode_base64_to_imagerJ   o   s    $88BM
))..!1!1-!@A
BC
8-.))#.C 		%%  
>	

s   %B B32B3c                B    t        |       }t        j                  |      S N)rJ   r   asarray)rH   r1   s     r    decode_base64_to_image_arrayrN   }   s    
 
*C::c?r>   c                2    t        |       }t        |||      S rL   )rJ   r*   )rH   r   r   r1   s       r    decode_base64_to_filerP      s    
 
*Cc9f--r>   c                R   t               5 }t        j                  j                  t	        j
                  | t        j                  d            }|j                  |d       |j                         }d d d        t        t        j                        d      }d|z   S # 1 sw Y   -xY w)NF)
force_copyJPEGutf-8data:image/jpeg;base64,)r   r'   r(   	fromarrayr   _convertr   uint8savegetvaluer   rC   	b64encode)image_arrayoutput_bytes	pil_image
bytes_data
base64_strs        r    encode_image_array_to_base64ra      s    	 -lII''%%k288N
	 	|V,!**,
- V%%j17;J$z11- -s   A&BB&c                    t               5 }| j                  |d       |j                         }d d d        t        t	        j
                        d      }d|z   S # 1 sw Y   -xY w)NrS   rT   rU   )r   rY   rZ   r   rC   r[   )imager]   r_   r`   s       r    encode_image_to_base64rd      s^    	 -l

<(!**,
- V%%j17;J$z11	- -s   #AA#c                    t        t        |             }t        | d      5 }|j                         }d d d        t        t	        j
                        d      }d| d|z   S # 1 sw Y   1xY w)NrbrT   data:z;base64,)r	   r   rB   readrC   r[   )
image_file	mime_typefr_   r`   s        r    encode_image_file_to_base64rl      sg    S_-I	j$	 1VVX
V%%j17;J9+X&33 s   A""A+c                    t        |       } t        |       r1t        j                  |       }|j	                          |j
                  S t        |       5 }|j                         }ddd       |S # 1 sw Y   S xY w)z
    Provided a path or URL to an SVG file, return the SVG content as a string.
    Parameters:
        image_file: Local file path or URL to an SVG file
    Returns:
        str: The SVG content as a string
    N)r   r
   httpxgetraise_for_statustextrB   rh   )ri   responsefilesvg_contents       r    extract_svg_contentru      sj     ZJ
#99Z(!!#}}* 	&))+K	&	&s   A..A8c                   | | S | j                   rk| j                   j                  d      rP|dk(  rt        | j                         S |dk(  rt        | j                         S |dk(  rt	        | j                   ||      S | j
                  t        d      t        | j
                        }| j                  rDt        | j                        }|j                  }|j                  j                  dd      }|dv rd	}nd
}d}|j                         dk(  r|dk(  rt        |      S t        d      t        j                   j#                  |      }	|dk(  r|d |	j$                  fv rt        |      S |	j'                         }
|
j)                  dd      dk7  r&t+        t,        d      r	 t-        j.                  |	      }	|j                         dk7  rG|	Et3        j6                         5  t3        j8                  d       ||	j;                  |      }	d d d        t=        |	t?        t@        d   |      |||      S # t0        $ r t3        j4                  d| d       Y w xY w# 1 sw Y   RxY w)Nrg   r   r   r   zImage path is None.. )jpgjpegrz   rc   webpsvgz9SVG files are not supported as input images for this app.i     r@   zFailed to transpose image z based on EXIF data.gifignore)r   r   r   )r   r   r   r   )!url
startswithrJ   rN   rP   r   r   r   	orig_namestemsuffixreplacelowerr   r   r'   r(   rB   modegetexifro   rE   r   r@   rF   warningswarncatch_warningssimplefilterconvertr!   r   r   )payloadr   r   
image_moder   	file_pathpr   r   r   exifs              r    preprocess_imager      s)    {{w{{--g65=)'++66W_/<<Z(iHH||.//W\\"I""#vv!!#r*_$F||~:y>!OPP			"BzzdBGG_<9~::<DxxQ13C!D	X((,B ||~2>$$& 	,!!(+%ZZ
+	, 
'45t<   	XMM6ykAUVW	X	, 	,s   H/ )I/"III c                j   | yt        | t              rV| j                         j                  d      r7t	        |       }t        t        |       j                  dt        |             S t        | ||      }t        |      j                         rt        |      j                  nd}t        ||      S )z
    Parameters:
        value: Expects a `numpy.array`, `PIL.Image`, or `str` or `pathlib.Path` filepath to an image which is displayed.
    Returns:
        Returns the image as a `FileData` object.
    Nz.svgzdata:image/svg+xml,)r   r   r#   )r   r   )r$   r   r   endswithru   r   r   r   r   r*   exists)valuer   r   rt   savedr   s         r    postprocess_imager      s     }%%++-"8"8"@)%05k&&%eK&8%9:
 	

 u	&AE$(K$6$6$8U  dI%955r>   )rc   r{   )r   zPIL.Image.Image | Noner   #Literal['numpy', 'pil', 'filepath']r   r   r   r   r   r   return)np.ndarray | PIL.Image.Image | str | None)r{   )r)   z)np.ndarray | PIL.Image.Image | str | Pathr   r   r   r   )r1   PIL.Image.Imager2   r/   r3   r/   )rH   r   r   r   )rH   r   r   
np.ndarray)rH   r   r   r   r   r   r   r   )r\   r   r   r   )rc   r   r   r   )ri   z
str | Pathr   r   )r   ImageData | Noner   r   r   r   r   zULiteral['1', 'L', 'P', 'RGB', 'RGBA', 'CMYK', 'YCbCr', 'LAB', 'HSV', 'I', 'F'] | Noner   r   r   r   )r   z0np.ndarray | PIL.Image.Image | str | Path | Noner   r   r   r   r   r   )*
__future__r   rC   r   ior   pathlibr   typingr   r   urllib.parser   rn   r   r   	PIL.Imager'   gradio_client.utilsr	   r
   r   gradior   gradio.data_classesr   gradio.exceptionsr   r(   initr!   r*   r=   rJ   rN   rP   ra   rd   rl   ru   r   r    r>   r    <module>r      s9   "           >  # ) # 		  !
!

-!
 !
 	!

 !
 /!
J QW0=@JM:.&
.
224&;;; ;	; .; /;|6;66 6 	6r>   