U
    !?h)                     @   sH  d dl Z d dlZd dlZd dlmZmZmZ d dl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mZmZmZ d dlmZ d dlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddl m!Z! ej"dddej#ddde$dddej#ddde%dddej#ddde%dddej#ddde$dd de$e%e%e$dd!d"d#Z&dS )$    N)OptionalTuplecastasyncify)DependsFastAPIFileFormQuery)CORSMiddleware)Response   )get_versions)remove)new_session)sessions_names)BaseSessionszfor a http server)namehelpz-pz--portiX  Tport)defaulttypeZshow_defaultr   z-hz--host0.0.0.0hostz-lz--log_levelinfoz	log levelz-tz	--threadsznumber of worker threads)r   r   	log_levelthreadsreturnc              	      s~  i ddddddg}t ddt d	 d
dddddd|dd}|jtddgdgdgd G dd d}G dd d}t|tdfdd |dfdd}|jd dgd!d"d#td$d%d&t	 ft
|d' fd(d)}	|jd dgd*d+d#td$d,d&t	 ft|d- fd.d/}
d0d1 }td2|d3kr0d4n| d5 d td6|d3krTd4n| d5  tj||||d7 d8S )9z
    Command-line interface for running the FastAPI web server.

    This function starts the FastAPI web server with the specified port and log level.
    If the number of worker threads is specified, it sets the thread limiter accordingly.
    zBackground RemovalzGEndpoints that perform background removal with different image sources.zGitHub Sourcez$https://github.com/danielgatis/rembg)descriptionurl)r   r    ZexternalDocsZRembgz8Rembg is a tool to remove images background. That is it.versionzDaniel Gatiszhttps://github.com/danielgatiszdanielgatis@gmail.com)r   r!   emailzMIT Licensez:https://github.com/danielgatis/rembg/blob/main/LICENSE.txt)r   r!   z/api)titler    r"   contactZlicense_infoZopenapi_tagsZdocs_urlT*)Zallow_credentialsZallow_originsZallow_methodsZallow_headersc                   @   s   e Zd Zeddde d ddeddd	ed
ddddedddddeddddeddd	eddd	eddd	eddd	f	eeeeeeee	e e	e d	ddZ
dS )z$s_command.<locals>.CommonQueryParams"Model to use when processing image(|)u2netr    regexr   FEnable Alpha Mattingr   r       r      $Alpha Matting (Foreground Threshold)r   geler    
   $Alpha Matting (Background Threshold)$Alpha Matting (Erode Structure Size)r   r4   r    	Only MaskPost Process MaskNBackground ColorExtra parameters as JSON	modelaafabaeomppmbgcextrasc
           
      S   sd   || _ || _|| _|| _|| _|| _|| _|	| _|rZtt	t
t
t
t
f ttt
|dnd | _d S N,r?   r@   rA   rB   rC   rD   rE   rG   r   r   inttuplemapsplitrF   
selfr?   r@   rA   rB   rC   rD   rE   rF   rG    rQ   J/var/www/html/venv/lib/python3.8/site-packages/rembg/commands/s_command.py__init__f   s    (z-s_command.<locals>.CommonQueryParams.__init__)__name__
__module____qualname__r   joinr   strboolrK   r   rS   rQ   rQ   rQ   rR   CommonQueryParamse   sR   
  


 rZ   c                   @   s   e Zd Zeddde d ddeddd	ed
ddddedddddeddddeddd	eddd	eddd	eddd	f	eee	e	e	eee
e e
e d	ddZdS )z(s_command.<locals>.CommonQueryPostParamsr'   r(   r)   r*   r+   r,   Fr.   r/   r0   r   r1   r2   r3   r6   r7   r8   r9   r:   r;   Nr<   r=   r>   c
           
      S   sd   || _ || _|| _|| _|| _|| _|| _|	| _|rZtt	t
t
t
t
f ttt
|dnd | _d S rH   rJ   rO   rQ   rQ   rR   rS      s    (z1s_command.<locals>.CommonQueryPostParams.__init__)rT   rU   rV   r
   rW   r   r   rX   rY   rK   r   rS   rQ   rQ   rQ   rR   CommonQueryPostParams   sR   
  


 r[   )contentcommonsr   c                    s   i }|j r6z|t|j  W n tk
r4   Y nX tt| f |jt	|jf||j
|j|j|j|j|j|jd|ddS )N)sessionalpha_matting"alpha_matting_foreground_threshold"alpha_matting_background_thresholdalpha_matting_erode_size	only_maskpost_process_maskbgcolorz	image/png)
media_type)rG   updatejsonloads	Exceptionr   r   
setdefaultr?   r   r@   rA   rB   rC   rD   rE   rF   )r\   r]   kwargs)sessionsrQ   rR   im_without_bg   s4     z s_command.<locals>.im_without_bgstartupc                     s`   zt d   W n tk
r(   Y nX d k	r\ddlm}  ddlm} |d|  d S )Nzhttp://localhost:r   )CapacityLimiter)RunVarZ_default_thread_limiter)
webbrowseropenrj   Zanyiorp   Zanyio.lowlevelrq   set)rp   rq   )r   r   rQ   rR   ro      s    zs_command.<locals>.startupz/api/removezRemove from URLzCRemoves the background from an image obtained by retrieving an URL.)pathtagssummaryr    .z*URL of the image that has to be processed.r/   )r!   r]   c                    s   t  4 I d H l}|| 4 I d H H}| I d H }t ||I d H W  5 Q I d H R  W  5 Q I d H R  S Q I d H R X W 5 Q I d H R X d S N)aiohttpZClientSessiongetreadr   )r!   r]   r^   responsefilern   rQ   rR   	get_index   s    zs_command.<locals>.get_indexzRemove from StreamzDRemoves the background from an image sent within the request itself.z2Image file (byte stream) that has to be processed.r}   r]   c                    s   t  | |I d H S rx   r   r   r~   rQ   rR   
post_index   s    zs_command.<locals>.post_indexc                 S   s   dd }t j|t jjdddt jjtdddt jjd	d
dt jjdddddt jjdddddt jjdddddt jjdddt jjd	ddt jjddg	t jjdddddd}t j	| |dd} | S )Nc              
   W   s   d}|\}}}}}}	}
||||||	d}|
r<| t|
 t|f||d< t| d:}t|d$}| }t|f|}|| W 5 Q R X W 5 Q R X tj	
|S )Nz
output.png)r_   r`   ra   rb   rc   rd   r^   rbwb)rg   rh   ri   r   rs   r{   r   writeosru   rW   )Z
input_pathr?   argsZoutput_pathr@   rA   rB   rC   rD   rE   cmd_argsrl   ioinputoutputrQ   rQ   rR   	inference  s$    	z,s_command.<locals>.gr_app.<locals>.inferencefilepathZInput)r   labelr+   ZModels)valuer   TzAlpha mattingr0   r   r1   zForeground threshold)r   minimummaximumr   r6   zBackground threshold(   zErosion sizeFz	Only maskzPost process mask	Arguments)r   Output   )Zconcurrency_limitZanalytics_enabled/)ru   )
grZ	Interface
componentsZImageZDropdownr   ZCheckboxZSliderZTextboxZmount_gradio_app)appr   Z	interfacerQ   rQ   rR   gr_app  sD             zs_command.<locals>.gr_appz.To access the API documentation, go to http://r   	localhost:zTo access the UI, go to http://)r   r   r   N)r   r   Zadd_middlewarer   bytesr   Zon_eventrz   r   r   rX   postr	   printuvicornrun)r   r   r   r   Ztags_metadatar   rZ   r[   ro   r   r   r   rQ   )rn   r   rm   r   rR   	s_command   s    +
-- 	4r   )'rh   r   rr   typingr   r   r   ry   ZclickZgradior   r   Zasyncerr   Zfastapir   r   r	   r
   r   Zfastapi.middleware.corsr   Zstarlette.responsesr   _versionr   bgr   Zsession_factoryr   rm   r   Zsessions.baser   commandoptionrK   rX   r   rQ   rQ   rQ   rR   <module>   sj   