
    h$                     2   d dl Z d dlZd dlmZ d dlZd dlmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlZd dlmZ d dl d dlmZmZ d dlmZ d d	lmZ  e       Zd dlZd d
lmZ dedej:                  fdZdeee      deeej:                        fdZ ejC                  d ee      gedg      ejC                  d ee      gedg      ejC                  d ee      gedg       ee      dfde
dededee"   fd                     Z#ejC                  d ee      gdg      ejC                  d ee      gdg      ejC                  d ee      gedg       ed       ed       ee      dfde
dedee   d eee      dedee"   fd!                     Z$y)"    N)List)	APIRouterDependsFileHTTPExceptionRequestResponsestatus)ORJSONResponse)verbose_proxy_logger)*)UserAPIKeyAuthuser_api_key_auth)ProxyBaseLLMRequestProcessing)route_request)
UploadFileuploadreturnc                    K   | j                          d{   }t        j                  |      }| j                  |_        |S 7 ,w)zz
    Read a FastAPI UploadFile into a BytesIO and set .name so OpenAI SDK
    infers filename/content-type correctly.
    N)readioBytesIOfilenamename)r   databuffers      c/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/image_endpoints/endpoints.pyuploadfile_to_bytesior      s9     
 DZZF//FKM s   AA-Auploadsc                 `   K   | sy| D cg c]  }t        |       d{    c}S 7 	c c}w w)zN
    Convert a list of UploadFiles to a list of BytesIO buffers, or None.
    N)r   )r   us     r   batch_to_bytesior"   "   s1      4;<q'***<<*<s   .)'
).).z/v1/images/generationsimages)dependenciesresponse_classtagsz/images/generationsz3/openai/deployments/{model:path}/images/generationsrequestfastapi_responseuser_api_key_dictmodelc                   K   ddl m}m}m}m}m}m}	m}
 i }	 | j                          d {   }t        j                  |      } ||| |||
|       d {   }|xs* |j                  dd       xs |	xs |j                  dd       |d<   |	r|	|d<   |d   t        j                  v rt        j                  |d      |d<   |j                  ||d       d {   }t        |d||		       d {   }| d {   }t!        j"                  |j%                  |j                  d
d      d             t'        |di       xs i }|j                  dd       xs d}|j                  dd       xs d}|j                  dd       xs d}|j                  dd       xs d}|j                  d
d       xs d}|j(                  j+                  t-        j.                  |||||
|t'        |dd      |||
             |S 7 7 7 &7 7 # t0        $ r+}|j3                  |||       d {  7   t5        j6                  dj9                  t;        |                   t5        j<                  t?        j@                                tC        |tD              rRtG        t'        |dt;        |            t'        |dd      t'        |dd      t'        |dtH        jJ                              t;        |       }tG        t'        |d|      t'        |dd      t'        |dd      t'        |dd       t'        |dd            d }~ww xY ww) Nr   )add_litellm_data_to_requestgeneral_settings
llm_routerproxy_configproxy_logging_obj
user_modelversion)r   r'   r-   r)   r2   r/   image_generation_modelr*   image_generation)r)   r   	call_typeaimage_generation)r   
route_typer.   r1   litellm_call_id success)r8   r
   _hidden_paramsmodel_id	cache_keyapi_baseresponse_costallowed_model_region)
r)   r<   r=   r>   r2   r?   model_regioncall_idrequest_datahidden_params)r)   original_exceptionrC   zElitellm.proxy.proxy_server.image_generation(): Exception occured - {}messagetypeNoneparamstatus_code)rF   rG   rI   coderK   i  )rF   rG   rI   openai_coderK   )&litellm.proxy.proxy_serverr,   r-   r.   r/   r0   r1   r2   bodyorjsonloadsgetlitellmmodel_alias_mappre_call_hookr   asynciocreate_taskupdate_request_statusgetattrheadersupdater   get_custom_headers	Exceptionpost_call_failure_hookr   errorformatstrdebug	traceback
format_exc
isinstancer   ProxyExceptionr
   HTTP_400_BAD_REQUEST)r'   r(   r)   r*   r,   r-   r.   r/   r0   r1   r2   r   rN   llm_callresponserD   r<   r=   r>   r?   r8   e	error_msgs                          r   r4   r4   -   sq    0   Dc\\^#||D! 1-/%
 
  '##$<dC'' xx&	 	W &DM
 =G333#33DMBDM '44/dFX 5 
 

 '*!!	
 
 "> 	33 $):B ?	 4 	
  *:B?E2 $$Z6<"!%%k48>B	 $$Z6<"%))/4@FB'++,=tDJ  '')<<"3!#!+$%68NPRS'!+	
 O $
2


 "@  66/ATX 7 
 	
 	
 	""SZZA	

 	""9#7#7#9:a' 9c!f5Q/a&1Qv/J/JK	  q6(I 9i8Q/a&1#Avt4Qs3 's   L?H G8*H G;BH G>H /H0	H 9H:C=H 7L?8H ;H >H H H 
L<L7(H+)DL77L<<L?z/v1/images/edits)r$   r&   z/images/editsz-/openai/deployments/{model:path}/images/edits.imagemaskc                   K   ddl m}m}m}m}	m}
m}m}m}m	}m
}m}m}  ||        d{   }t        |       d{   }t        |       d{   }|r||d<   |r||d<   |xs* |j                  dd      xs |xs |j                  dd      |d<   t        |	      }	 |j!                  | ||d
|
|||	|d||||||       d{   S 7 7 7 }7 
# t"        $ r#}|j%                  |||
|       d{  7  d}~ww xY ww)a  
    Follows the OpenAI Images API spec: https://platform.openai.com/docs/api-reference/images/create

    ```bash
    curl -s -D >(grep -i x-request-id >&2)     -o >(jq -r '.data[0].b64_json' | base64 --decode > gift-basket.png)     -X POST "http://localhost:4000/v1/images/edits"     -H "Authorization: Bearer sk-1234"         -F "model=gpt-image-1"         -F "image[]=@soap.png"         -F 'prompt=Create a studio ghibli image of this'
    ```
    r   )_read_request_bodyr-   r.   r/   r0   select_data_generatoruser_api_baseuser_max_tokensr1   user_request_timeoutuser_temperaturer2   )r'   Nrk   rl   r3   r*   )r   aimage_edit)r'   r(   r)   r7   r0   r.   r-   r/   ro   r*   r1   rs   rr   rq   rp   r2   )ri   r)   r0   r2   )rM   rn   r-   r.   r/   r0   ro   rp   rq   r1   rr   rs   r2   r"   rQ   r   base_process_llm_requestr\   _handle_llm_api_exception)r'   r(   rk   rl   r)   r*   rn   r-   r.   r/   r0   ro   rp   rq   r1   rr   rs   r2   r   image_files
mask_files	processorri   s                          r   image_edit_apirz      sM    J   $ $G44D(//K'--J#W!V 	 	# 8$?	#	# 88GT"	 	M .48I
77-/$/!-%"7!-!5+'! 8 
 
 	
) 5/-$
$  
77//	 8 
 
 
 	

st   )DCDCDCAD"$C CC 
DDDC 	C?C:4C75C::C??D)%rU   rb   typingr   rO   fastapir   r   r   r   r   r	   r
   fastapi.responsesr   rR   litellm._loggingr   litellm.proxy._types$litellm.proxy.auth.user_api_key_authr   r   'litellm.proxy.common_request_processingr   litellm.proxy.route_llm_requestr   routerr   r   r   r   Optionalr"   postr`   r4   rz        r   <module>r      sT       V V V ,  1 " R Q 9	 	 
 rzz =d:&'=d2::= +,-!
	   +,-!
	   9+,-!
	   )00A(B	ttt &t C=	t$tn +,-
  
 +,-
  
 3+,-!
	   #3i'+Dz(/0A(BS
S
S
 
S
 4
#
$	S

 &S
 C=S
 S
r   