
    {-jv~                        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 d dlm	Z	m
Z
mZmZmZ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mZmZmZmZmZmZm Z   G d
 d          Z! G d d          Z" G d d          Z#d Z$ G d d          Z% G d d          Z& G d d          Z' G d d          Z( G d d          Z) G d d          Z* G d d          Z+ G d d           Z, G d! d"          Z-dS )#    N)abstractmethod)Path)AnyCallableDictListOptionalUnion)Image   )logging   )	AudioWriter	CSVWriter
HtmlWriterImageWriter
JsonWriterMarkdownWriter
TextWriterVideoWriter
XlsxWriterc                       e Zd ZdZdedefdZedee	e
f         fd            Zedee	e
f         fd            Zd
d	ZdS )	WordMixinzB
    Mixin class for adding Word (.docx) export capabilities.
    argskwargsc                 D    | j                             | j                   d S N)_save_funcsappendsave_to_wordselfr   r   s      e/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/common/result/mixin.py__init__zWordMixin.__init__.   s"     122222    returnc                     t           z
        Convert the result to a Word-compatible format.

        Returns:
            Dict[str, Any]: A dictionary containing Word-compatible blocks and image data.
        NotImplementedErrorr"   s    r#   _to_wordzWordMixin._to_word1   
     "!r%   c                 *    |                                  S r(   )r,   r+   s    r#   wordzWordMixin.word;        }}r%   Nc           	         ddl m}m} t          |                                           }t          |          }||j         dz  }|                                 } ||d         |          }	|                    |d         |	|                    dd          |                    dd	          
          }
|
	                    |
                                           dS )z Save the result to a .docx file.   )WordConvertersave_imagesz.docximagesword_blocksoriginal_image_widthi  original_image_heightr   )abs_image_pathsr7   r8   N)	converterr3   r4   r   _get_input_fnstemr,   convertgetsaveas_posix)r"   	save_pathr   r   r3   r4   fn	save_file	word_datar9   docs              r#   r    zWordMixin.save_to_wordE   s    99999999$$$&&''OO	27 1 1 11	MMOO	%+i&99EE##m$+!*/Es!K!K"+--0G"K"K	 $ 
 
 	##%%&&&&&r%   r&   N)__name__
__module____qualname____doc__listdictr$   r   r   strr   r,   propertyr/   r     r%   r#   r   r   )   s         3d 3d 3 3 3 3 "$sCx. " " " ^" d38n    X' ' ' ' ' 'r%   r   c                   ~    e Zd ZdedefdZedeee	f         fd            Z
edeee	f         fd            Zd	dZdS )

LatexMixinr   r   c                 D    | j                             | j                   d S r   )r   r   save_to_latexr!   s      r#   r$   zLatexMixin.__init__Y   s"     233333r%   r&   c                     t           )z
        Convert the result to a LaTeX-compatible format.

        Returns:
            Dict[str, Any]: A dictionary containing LaTeX-compatible blocks and image data.
        r)   r+   s    r#   	_to_latexzLatexMixin._to_latex\   r-   r%   c                 *    |                                  S )zProperty to access the LaTeX-compatible data.

        Returns:
            Dict[str, Any]: A dictionary containing LaTeX-compatible blocks and image data.
        )rU   r+   s    r#   latexzLatexMixin.latexf        ~~r%   Nc                    ddl m}m} t          |                                           }t          |          }||j         dz  }|                                 } ||d         |          }	|                    |d         |	          }
t          |	                                dd	          5 }|
                    |
           d
d
d
           d
S # 1 swxY w Y   d
S )zSave the result to a .tex file.r2   )LatexConverterr4   z.texr5   latex_blocks)r9   wzutf-8)encodingN)r:   rZ   r4   r   r;   r<   rU   r=   openr@   write)r"   rA   r   r   rZ   r4   rB   rC   
latex_datar9   rW   fs               r#   rS   zLatexMixin.save_to_latexo   s0   ::::::::$$$&&''OO	27 0 0 00	^^%%
%+j&:IFF&&~&+ ' 
 

 )$$&&g>>> 	!GGENNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   -CCCrF   )rG   rH   rI   rK   rL   r$   r   r   rM   r   rU   rN   rW   rS   rO   r%   r#   rQ   rQ   X   s        4d 4d 4 4 4 4 "4S> " " " ^"  tCH~       X      r%   rQ   c                   L    e Zd ZdZedeeef         fd            Zd ZddZdS )StrMixinz6Mixin class for adding string conversion capabilities.r&   c                 *    |                                  S )zProperty to get the string representation of the result.

        Returns:
            Dict[str, str]: The string representation of the result.
        )_to_strr+   s    r#   rM   zStrMixin.str   s     ||~~r%   c                 
    d| iS )a  Convert the given result data to a string representation.

        Args:
            json_format (bool): If True, return a JSON formatted string. Default is False.
            indent (int): Number of spaces to indent for JSON formatting. Default is 4.
            ensure_ascii (bool): If True, ensure all characters are ASCII. Default is False.

        Returns:
            Dict[str, str]: The string representation of the result.
        resrO   r+   s    r#   re   zStrMixin._to_str   s     t}r%   Nc                 R    t          j        |                                            dS )z.Print the string representation of the result.N)r   infore   r+   s    r#   printzStrMixin.print   s     T\\^^$$$$$r%   rF   )	rG   rH   rI   rJ   rN   r   rM   re   rj   rO   r%   r#   rc   rc      sj        @@T#s(^    X  % % % % % %r%   rc   c                    t          | t          j        t          j        f          rt	          |           S t          | t          j        t          j        f          rt          |           S t          | t          j                  rd | 	                                D             S t          | t          j                  r)t          j        |                     dd                    S t          | t                    r|                                 S t          | t"                    r+t#          d |                                 D                       S t          | t&          t(          f          rd | D             S | S )zHelper function to format data into a JSON-serializable format.

    Args:
        obj: The object to be formatted.

    Returns:
        Any: The formatted object.
    c                 ,    g | ]}t          |          S rO   _format_data).0items     r#   
<listcomp>z _format_data.<locals>.<listcomp>   s     <<<tT""<<<r%   recordsF)orientforce_asciic                 4    i | ]\  }}|t          |          S rO   rm   )ro   kvs      r#   
<dictcomp>z _format_data.<locals>.<dictcomp>   s$    @@@DAqQQ@@@r%   c                 ,    g | ]}t          |          S rO   rm   )ro   is     r#   rq   z _format_data.<locals>.<listcomp>   s    ---AQ---r%   )
isinstancenpfloat32float64floatint32int64intndarraytolistpd	DataFramejsonloadsto_jsonr   r@   rL   itemsrK   tuple)objs    r#   rn   rn      s;    #
BJ/00 Szz	C"(BH-	.	. 3xx	C	$	$ <<szz||<<<<	C	&	& 	z#++YE+JJKKK	C		 ||~~	C		 @@CIIKK@@@AAA	C$	'	' ------
r%   c                       e Zd ZdZddZdeeeeef         f         fdZe	deeeeef         f         fd            Z
	 	 dd	ed
ededededdfdZ	 	 	 dded
edefdZ	 dded
ededdfdZdS )	JsonMixinz7Mixin class for adding JSON serialization capabilities.r&   Nc                 j    t                      | _        | j                            | j                   d S r   )r   _json_writerr   r   save_to_jsonr+   s    r#   r$   zJsonMixin.__init__   s.    &LL 122222r%   c                 H    dt          t          j        |                     iS )zConvert the object to a JSON-serializable format.

        Returns:
            Dict[str, Dict[str, Any]]: A dictionary representation of the object that is JSON-serializable.
        rg   )rn   copydeepcopyr+   s    r#   _to_jsonzJsonMixin._to_json   s!     |DM$$7$78899r%   c                 *    |                                  S )zProperty to get the JSON representation of the result.

        Returns:
            Dict[str, Dict[str, Any]]: The dict type JSON representation of the result.
        )r   r+   s    r#   r   zJsonMixin.json   r0   r%   r   FrA   indentensure_asciir   r   c                     d }|                                  } ||          s{t          |                                           }|j        }	t          |          }
|D ]?}|
|	 d| dz  } | j        j        |                                ||         g|R ||d| @d	S t          |          dk    rt          j	        d| d            | j        j        ||t          |                                          d                  g|R ||d| d	S )
ae  Save the JSON representation of the object to a file.

        Args:
            save_path (str): The path to save the JSON file. If the save path does not end with '.json', it appends the base name and suffix of the input path.
            indent (int): The number of spaces to indent for pretty printing. Default is 4.
            ensure_ascii (bool): If False, non-ASCII characters will be included in the output. Default is False.
            *args: Additional positional arguments to pass to the underlying writer.
            **kwargs: Additional keyword arguments to pass to the underlying writer.
        c                 D    t          j        |           \  }}|d uo|dk    S )Nzapplication/json	mimetypes
guess_type	file_path	mime_type_s      r#   _is_json_filez-JsonMixin.save_to_json.<locals>._is_json_file   s,    $/	::LIqD(LY:L-LLr%   r   z.jsonr   r   r2   z`The result has multiple json files need to be saved. But the `save_path` has been specified as ``!r   N)r   r   r;   r<   r   r_   r@   lenr   warningrK   keys)r"   rA   r   r   r   r   r   	json_datarB   r<   base_save_pathkeys               r#   r   zJsonMixin.save_to_json   s   $	M 	M 	M MMOO	}Y'' 	d((**++B7D!)__N  	 	*-A-As-A-A-AA	'!'&&((cN
   "!-	     	 	 9~~!! E  xA  E  E  E   $D#$y~~//0034
   )	      r%   json_formatc                 Z    |r&t          j        t          d| i          ||          S d| iS )a  Convert the given result data to a string representation.
        Args:
            data (dict): The data would be converted to str.
            json_format (bool): If True, return a JSON formatted string. Default is False.
            indent (int): Number of spaces to indent for JSON formatting. Default is 4.
            ensure_ascii (bool): If True, ensure all characters are ASCII. Default is False.
        Returns:
            Dict[str, str]: The string representation of the result.
        rg   r   )r   dumpsrn   )r"   r   r   r   s       r#   re   zJsonMixin._to_str  sD      	!:eT]++F    4= r%   c                 ^    |                      |||          }t          j        |           dS )aU  Print the string representation of the result.

        Args:
            json_format (bool): If True, print a JSON formatted string. Default is False.
            indent (int): Number of spaces to indent for JSON formatting. Default is 4.
            ensure_ascii (bool): If True, ensure all characters are ASCII. Default is False.
        )r   r   r   N)re   r   ri   )r"   r   r   r   str_s        r#   rj   zJsonMixin.print#  s;     ||#F  
 
 	Tr%   rF   )r   F)Fr   F)rG   rH   rI   rJ   r$   r   rM   r   r   rN   r   r   boolr   r   re   rj   rO   r%   r#   r   r      sp       AA3 3 3 3:$sDcN23 : : : : d3S#X./    X "	3 33 3 	3
 3 3 
3 3 3 3n ""	! !! ! 	! ! ! !. PU 14HL	     r%   r   c                       e Zd ZdZdededdfdZedeeef         fd            Z	e
deeef         fd            Zd	edededdfd
ZdS )Base64Mixinz4Mixin class for adding Base64 encoding capabilities.r   r   r&   Nc                 f    t          |i || _        | j                            | j                   dS )zInitializes the Base64Mixin.

        Args:
            *args: Positional arguments to pass to the TextWriter.
            **kwargs: Keyword arguments to pass to the TextWriter.
        N)r   _base64_writerr   r   save_to_base64r!   s      r#   r$   zBase64Mixin.__init__6  s9     )$9&99 344444r%   c                     t           )zAbstract method to convert the result to Base64.

        Returns:
            Dict[str, str]: The str type Base64 representation result.
        r)   r+   s    r#   
_to_base64zBase64Mixin._to_base64@  
     "!r%   c                 *    |                                  S )z
        Property that returns the Base64 encoded content.

        Returns:
            Dict[str, str]: The base64 representation of the result.
        )r   r+   s    r#   base64zBase64Mixin.base64I  s        r%   rA   c                 `   |                                  }t          |                                                              d          sxt	          |                                           }|j        }t	          |          }|D ]<}|| d| dz  } | j        j        |	                                ||         g|R i | =dS t          |          dk    rt          j        d| d            | j        j        ||t          |                                          d                  g|R i | dS )a  Saves the Base64 encoded content to the specified path.

        Args:
            save_path (str): The path to save the base64 representation result. If the save path does not end with '.b64', it appends the base name and suffix of the input path.

            *args: Additional positional arguments that will be passed to the base64 writer.
            **kwargs: Additional keyword arguments that will be passed to the base64 writer.
        z.b64r   r2   zbThe result has multiple base64 files need to be saved. But the `save_path` has been specified as `r   r   N)r   rM   lowerendswithr   r;   r<   r   r_   r@   r   r   r   rK   r   )	r"   rA   r   r   r   rB   r<   r   r   s	            r#   r   zBase64Mixin.save_to_base64S  s    ""9~~##%%..88 	d((**++B7D!)__N  *-@-@s-@-@-@@	)#)&&((&+8<  @F     6{{Q G  zC  G  G  G   &D%6$v{{}}"5"5a"89<@  DJ    r%   )rG   rH   rI   rJ   r   r   r$   r   rM   r   rN   r   r   rO   r%   r#   r   r   3  s        >>5d 5d 5t 5 5 5 5 "DcN " " " ^" !S#X ! ! ! X! D D T      r%   r   c            	           e Zd ZdZddedededdfdZedeee	j	        f         fd	            Z
edeee	j	        f         fd
            ZdedededdfdZdS )ImgMixinz3Mixin class for adding image handling capabilities.pillowbackendr   r   r&   Nc                 j    t          |d|i|| _        | j                            | j                   dS )a%  Initializes ImgMixin.

        Args:
            backend (str): The backend to use for image processing. Defaults to "pillow".
            *args: Additional positional arguments to pass to the ImageWriter.
            **kwargs: Additional keyword arguments to pass to the ImageWriter.
        r   N)r   _img_writerr   r   save_to_imgr"   r   r   r   s       r#   r$   zImgMixin.__init__s  s>     'HwHHH 011111r%   c                     t           )zAbstract method to convert the result to an image.

        Returns:
            Dict[str, Image.Image]: The image representation result.
        r)   r+   s    r#   _to_imgzImgMixin._to_img~  r   r%   c                 *    |                                  S )zProperty to get the image representation of the result.

        Returns:
            Dict[str, Image.Image]: The image representation of the result.
        )r   r+   s    r#   imgzImgMixin.img       ||~~r%   rA   c                 >   d }|                                  } ||          st          |                                           } ||          r|j        nd}|j        }t          |          }	|D ]=}
|	| d|
 | z  } | j        j        |                                ||
         g|R i | >dS t          |          dk    rt          j
        d| d            | j        j        ||t          |                                          d                  g|R i | dS )	a  Saves the image representation of the result to the specified path.

        Args:
            save_path (str): The path to save the image. If the save path does not end with .jpg or .png, it appends the input path's stem and suffix to the save path.
            *args: Additional positional arguments that will be passed to the image writer.
            **kwargs: Additional keyword arguments that will be passed to the image writer.
        c                 b    t          j        |           \  }}|d uo|                    d          S )Nzimage/r   r   
startswithr   s      r#   _is_image_filez,ImgMixin.save_to_img.<locals>._is_image_file  4    $/	::LIqD(KY-A-A(-K-KKr%   z.pngr   r2   z_The result has multiple img files need to be saved. But the `save_path` has been specified as `r   r   N)r   r   r;   suffixr<   r   r_   r@   r   r   r   rK   r   )r"   rA   r   r   r   r   rB   r   r<   r   r   s              r#   r   zImgMixin.save_to_img  s|   	L 	L 	L llnn~i(( 	Yd((**++B"0."4"4@RYY&F7D!)__N X X*-D-Ds-DF-D-DD	& &y'9'9';';SXWWWWPVWWWWX X 3xx!|| Dv  D  D  D   #D"9c$sxxzz2B2B12E.FXXXXQWXXXXXr%   )r   )rG   rH   rI   rJ   rM   r   r   r$   r   r   r   rN   r   r   rO   r%   r#   r   r   p  s        ==	2 	2 	2t 	2t 	2PT 	2 	2 	2 	2 "c5;./ " " " ^" T#u{*+    XYS Y Y Y$ Y Y Y Y Y Yr%   r   c            	           e Zd ZdZddedededdfdZedeee	j
        f         fd	            Zedeee	j
        f         fd
            ZdedededdfdZdS )CSVMixinz1Mixin class for adding CSV handling capabilities.pandasr   r   r   r&   Nc                     t          |d|i|| _        t          | d          sg | _        | j                            | j                   dS )a  Initializes the CSVMixin.

        Args:
            backend (str): The backend to use for CSV operations (default is "pandas").
            *args: Optional positional arguments to pass to the CSVWriter.
            **kwargs: Optional keyword arguments to pass to the CSVWriter.
        r   r   N)r   _csv_writerhasattrr   r   save_to_csvr   s       r#   r$   zCSVMixin.__init__  sX     %tFWFvFFt]++ 	"!D 011111r%   c                 *    |                                  S )zProperty to get the pandas Dataframe representation of the result.

        Returns:
            Dict[str, pd.DataFrame]: The pandas.DataFrame representation of the result.
        )_to_csvr+   s    r#   csvzCSVMixin.csv  r   r%   c                     t           )zAbstract method to convert the result to pandas.DataFrame.

        Returns:
            Dict[str, pd.DataFrame]: The pandas.DataFrame representation result.
        r)   r+   s    r#   r   zCSVMixin._to_csv  r   r%   rA   c                    d }|                                  } ||          sxt          |                                           }|j        }t          |          }|D ]<}	|| d|	 dz  } | j        j        |                                ||	         g|R i | =dS t          |          dk    rt          j	        d| d            | j        j        ||t          |                                          d                  g|R i | dS )	a  Saves the result to a CSV file.

        Args:
            save_path (str): The path to save the CSV file. If the path does not end with ".csv",
                the stem of the input path attribute (self['input_path']) will be used as the filename.
            *args: Optional positional arguments to pass to the CSV writer's write method.
            **kwargs: Optional keyword arguments to pass to the CSV writer's write method.
        c                 D    t          j        |           \  }}|d uo|dk    S )Nztext/csvr   r   s      r#   _is_csv_filez*CSVMixin.save_to_csv.<locals>._is_csv_file  s+    $/	::LIqD(DY*-DDr%   r   z.csvr2   z_The result has multiple csv files need to be saved. But the `save_path` has been specified as `r   r   N)r   r   r;   r<   r   r_   r@   r   r   r   rK   r   )
r"   rA   r   r   r   r   rB   r<   r   r   s
             r#   r   zCSVMixin.save_to_csv  s`   	E 	E 	E llnn|I&& 	Yd((**++B7D!)__N X X*-@-@s-@-@-@@	& &y'9'9';';SXWWWWPVWWWWX X 3xx!|| Dv  D  D  D   #D"9c$sxxzz2B2B12E.FXXXXQWXXXXXr%   )r   )rG   rH   rI   rJ   rM   r   r   r$   rN   r   r   r   r   r   r   rO   r%   r#   r   r     s        ;;2 2 2t 2t 2PT 2 2 2 2 T#r|+,    X "c2</0 " " " ^"YS Y Y Y$ Y Y Y Y Y Yr%   r   c                       e Zd ZdZdededdfdZedeeef         fd            Z	e
deeef         fd            Zd	edededdfd
ZdS )	HtmlMixinz2Mixin class for adding HTML handling capabilities.r   r   r&   Nc                 f    t          |i || _        | j                            | j                   dS )a   
        Initializes the HTML writer and appends the save_to_html method to the save functions list.

        Args:
            *args: Positional arguments passed to the HtmlWriter.
            **kwargs: Keyword arguments passed to the HtmlWriter.
        N)r   _html_writerr   r   save_to_htmlr!   s      r#   r$   zHtmlMixin.__init__  s9     '777 122222r%   c                 *    |                                  S )zProperty to get the HTML representation of the result.

        Returns:
            str: The str type HTML representation of the result.
        )_to_htmlr+   s    r#   htmlzHtmlMixin.html       }}r%   c                     t           )zAbstract method to convert the result to str type HTML representation.

        Returns:
            Dict[str, str]: The str type HTML representation result.
        r)   r+   s    r#   r   zHtmlMixin._to_html  r   r%   rA   c                    d }|                                  } ||          sxt          |                                           }|j        }t          |          }|D ]<}	|| d|	 dz  } | j        j        |                                ||	         g|R i | =dS t          |          dk    rt          j	        d| d            | j        j        ||t          |                                          d                  g|R i | dS )	zSaves the HTML representation of the object to the specified path.

        Args:
            save_path (str): The path to save the HTML file.
            *args: Additional positional arguments.
            **kwargs: Additional keyword arguments.
        c                 D    t          j        |           \  }}|d uo|dk    S )Nz	text/htmlr   r   s      r#   _is_html_filez-HtmlMixin.save_to_html.<locals>._is_html_file  s+    $/	::LIqD(EY+-EEr%   r   z.htmlr2   z`The result has multiple html files need to be saved. But the `save_path` has been specified as `r   r   N)r   r   r;   r<   r   r_   r@   r   r   r   rK   r   )
r"   rA   r   r   r   r   rB   r<   r   r   s
             r#   r   zHtmlMixin.save_to_html  s   	F 	F 	F }}}Y'' 	d((**++B7D!)__N  *-A-As-A-A-AA	'!'&&(($s)6:  >D     4yy1}} E  xA  E  E  E   $D#4TYY[[ 1 1! 458<  @F    r%   )rG   rH   rI   rJ   r   r   r$   rN   rM   r   r   r   r   rO   r%   r#   r   r     s        <<	3d 	3d 	3t 	3 	3 	3 	3 d38n    X "$sCx. " " " ^"c $ $ 4      r%   r   c                       e Zd ZdZdededdfdZedeeef         fd            Z	e
deeef         fd            Zd	edededdfd
ZdS )	XlsxMixinz2Mixin class for adding XLSX handling capabilities.r   r   r&   Nc                 f    t          |i || _        | j                            | j                   dS )a  Initializes the XLSX writer and appends the save_to_xlsx method to the save functions.

        Args:
            *args: Positional arguments to be passed to the XlsxWriter constructor.
            **kwargs: Keyword arguments to be passed to the XlsxWriter constructor.
        N)r   _xlsx_writerr   r   save_to_xlsxr!   s      r#   r$   zXlsxMixin.__init__2  s9     '777 122222r%   c                 *    |                                  S )zProperty to get the XLSX representation of the result.

        Returns:
            Dict[str, str]: The str type XLSX representation of the result.
        )_to_xlsxr+   s    r#   xlsxzXlsxMixin.xlsx<  r   r%   c                     t           )zAbstract method to convert the result to str type XLSX representation.

        Returns:
            Dict[str, str]: The str type HTML representation result.
        r)   r+   s    r#   r   zXlsxMixin._to_xlsxE  r   r%   rA   c                    d }|                                  } ||          sxt          |                                           }|j        }t          |          }|D ]<}	|| d|	 dz  } | j        j        |                                ||	         g|R i | =dS t          |          dk    rt          j	        d| d            | j        j        ||t          |                                          d                  g|R i | dS )	a  Saves the HTML representation to an XLSX file.

        Args:
            save_path (str): The path to save the XLSX file. If the path does not end with ".xlsx",
                             the filename will be set to the stem of the input path with ".xlsx" extension.
            *args: Additional positional arguments to pass to the XLSX writer.
            **kwargs: Additional keyword arguments to pass to the XLSX writer.
        c                 D    t          j        |           \  }}|d uo|dk    S )NzAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetr   r   s      r#   _is_xlsx_filez-XlsxMixin.save_to_xlsx.<locals>._is_xlsx_fileX  s5    $/	::LIq% WVWr%   r   z.xlsxr2   z`The result has multiple xlsx files need to be saved. But the `save_path` has been specified as `r   r   N)r   r   r;   r<   r   r_   r@   r   r   r   rK   r   )
r"   rA   r   r   r   r   rB   r<   r   r   s
             r#   r   zXlsxMixin.save_to_xlsxN  s   	 	 	 }}}Y'' 	d((**++B7D!)__N  *-A-As-A-A-AA	'!'&&(($s)6:  >D     4yy1}} E  xA  E  E  E   $D#4TYY[[ 1 1! 458<  @F    r%   )rG   rH   rI   rJ   r   r   r$   rN   rM   r   r   r   r   rO   r%   r#   r   r   /  s        <<3d 3d 3t 3 3 3 3 d38n    X "$sCx. " " " ^"#c #$ #$ #4 # # # # # #r%   r   c            	           e Zd ZdZddedededdfdZedeee	j
        f         fd	            Zedeee	j
        f         fd
            ZdedededdfdZdS )
VideoMixinz3Mixin class for adding Video handling capabilities.opencvr   r   r   r&   Nc                 R    || _         | j                            | j                   dS )a'  Initializes VideoMixin.

        Args:
            backend (str): The backend to use for video processing. Defaults to "opencv".
            *args: Additional positional arguments to pass to the VideoWriter.
            **kwargs: Additional keyword arguments to pass to the VideoWriter.
        N)_backendr   r   save_to_videor   s       r#   r$   zVideoMixin.__init__w  s+       233333r%   c                     t           )zAbstract method to convert the result to a video.

        Returns:
            Dict[str, np.array]: The video representation result.
        r)   r+   s    r#   	_to_videozVideoMixin._to_video  r   r%   c                 *    |                                  S )zProperty to get the video representation of the result.

        Returns:
            Dict[str, np.array]: The video representation of the result.
        )r   r+   s    r#   videozVideoMixin.video  rX   r%   rA   c                 P   d }t          |d| j        i|}|                                 } ||          st          |                                           }|j        } ||          r|j        nd}	t          |          }
|D ]8}|
| d| |	 z  } |j        |                                ||         g|R i | 9d	S t          |          dk    rt          j        d| d            |j        ||t          |                                          d                  g|R i | d	S )
a  Saves the video representation of the result to the specified path.

        Args:
            save_path (str): The path to save the video. If the save path does not end with .mp4 or .avi, it appends the input path's stem and suffix to the save path.
            *args: Additional positional arguments that will be passed to the video writer.
            **kwargs: Additional keyword arguments that will be passed to the video writer.
        c                 b    t          j        |           \  }}|d uo|                    d          S )Nzvideo/r   r   s      r#   _is_video_filez0VideoMixin.save_to_video.<locals>._is_video_file  r   r%   r   z.mp4r   r2   zaThe result has multiple video files need to be saved. But the `save_path` has been specified as `r   r   N)r   r   r   r   r;   r<   r   r_   r@   r   r   r   rK   r   )r"   rA   r   r   r  video_writerr  rB   r<   r   r   r   s               r#   r   zVideoMixin.save_to_video  s   	L 	L 	L #4J4=J6JJ  ~i(( 	Yd((**++B7D"0."4"4@RYY&F!)__N V V*-D-Ds-DF-D-DD	""9#5#5#7#7sUdUUUfUUUUV V 5zzA~~ F  yB  F  F  F   Ly%UZZ\\0B0B10E*FXXXXQWXXXXXr%   )r   )rG   rH   rI   rJ   rM   r   r   r$   r   r|   arrayr   rN   r  r   rO   r%   r#   r   r   t  s        ==	4 	4 	4t 	4t 	4PT 	4 	4 	4 	4 "4RX. " " " ^"  tCM*       X Ys Y4 Y4 YD Y Y Y Y Y Yr%   r   c                       e Zd ZdZdededdfdZedeee	j
        f         fd            Zedeee	j
        f         fd            Zd	edededdfd
ZdS )
AudioMixinz3Mixin class for adding Audio handling capabilities.r   r   r&   Nc                     || _         | j                            | j                   t	          |d| j         i|| _        dS )zInitializes AudioMixin.

        Args:
            *args: Additional positional arguments to pass to the AudioWriter.
            **kwargs: Additional keyword arguments to pass to the AudioWriter.
        r   N)r   r   r   save_to_audior   _audio_writerr   s       r#   r$   zAudioMixin.__init__  sG       2333(PPPPr%   c                     t           )zAbstract method to convert the result to a audio.

        Returns:
            Dict[str, np.array]: The audio representation result.
        r)   r+   s    r#   	_to_audiozAudioMixin._to_audio  r   r%   c                 *    |                                  S )zProperty to get the audio representation of the result.

        Returns:
            Dict[str, np.array]: The audio representation of the result.
        )r  r+   s    r#   audiozAudioMixin.audio  rX   r%   rA   c                 >   d }|                                  } ||          st          |                                           }|j        } ||          r|j        nd}t          |          }	|D ]=}
|	| d|
 | z  } | j        j        |                                ||
         g|R i | >dS t          |          dk    rt          j
        d| d            | j        j        ||t          |                                          d                  g|R i | dS )	a  Saves the audio representation of the result to the specified path.

        Args:
            save_path (str): The path to save the audio. If the save path does not end with .mp4 or .avi, it appends the input path's stem and suffix to the save path.
            *args: Additional positional arguments that will be passed to the audio writer.
            **kwargs: Additional keyword arguments that will be passed to the audio writer.
        c                 b    t          j        |           \  }}|d uo|                    d          S )Nzaudio/r   r   s      r#   _is_audio_filez0AudioMixin.save_to_audio.<locals>._is_audio_file  r   r%   z.wavr   r2   zaThe result has multiple audio files need to be saved. But the `save_path` has been specified as `r   r   N)r  r   r;   r<   r   r  r_   r@   r   r   r   rK   r   )r"   rA   r   r   r  r  rB   r<   r   r   r   s              r#   r  zAudioMixin.save_to_audio  s   	L 	L 	L   ~i(( 	d((**++B7D"0."4"4@RYY&F!)__N  *-D-Ds-DF-D-DD	("(&&((%*7;  ?E     5zzA~~ F  yB  F  F  F   %D$5ejjll!3!3A!67:>  BH    r%   )rG   rH   rI   rJ   r   r   r$   r   rM   r|   r  r  rN   r  r  rO   r%   r#   r	  r	    s        ==	Qt 	Qt 	Q 	Q 	Q 	Q 	Q "4RX. " " " ^"  tCM*       X s 4 4 D      r%   r	  c                   :   e Zd ZdZdgZdedefdZe	 dde	e
ee
e	e
ef         f         f         fd	            Zede	e
ee
e	e
ef         f         f         fd
            Z	 d	 ddZdededee
ef         dee	e
ee
e	e
ef         f         f                  ddf
dZdS )MarkdownMixinz6Mixin class for adding Markdown handling capabilities.markdown_textsr   r   c                     t          |i || _        t          |i || _        | j                            | j                   dS )a&  Initializes the Markdown writer and appends the save_to_markdown method to the save functions.

        Args:
            *args: Positional arguments to be passed to the MarkdownWriter constructor.
            **kwargs: Keyword arguments to be passed to the MarkdownWriter constructor.
        N)r   _markdown_writerr   r   r   r   save_to_markdownr!   s      r#   r$   zMarkdownMixin.__init__  sN     !/ ? ? ?&777 566666r%   TFr&   c                     t           )a  
        Convert the result to markdown format.

        Args:
            pretty (Optional[bool]): whether to pretty markdown by HTML, default by True.

        Returns:
            Dict[str, Union[str, Dict[str, Any]]]: A dictionary containing markdown text and image data.
        r)   )r"   prettyshow_formula_numbers      r#   _to_markdownzMarkdownMixin._to_markdown  s
     "!r%   c                 *    |                                  S )zProperty to access the markdown data.

        Returns:
            Dict[str, Union[str, Dict[str, Any]]]: A dictionary containing markdown text and image data.
        )r  r+   s    r#   markdownzMarkdownMixin.markdown  s       """r%   Nc           	      |   dt           fd} ||          s\t          |                                           } ||          r|j        nd}|j        }	t          |          }
|
|	 | z  }|| _        n|| _         | j        | j        j        | j	        j        | j        | 
                    ||          g|R i | dS )a  Save the markdown data to a file.

        Args:
            save_path (Union[str, Path]): The path where the markdown file will be saved.
            *args: Additional positional arguments for saving.
            **kwargs: Additional keyword arguments for saving.
        r&   c                     h d}t           j                            t          |                     \  }}|                                |v rdS t          j        t          |                     \  }}|dk    S )a  Check if a file is a markdown file based on its extension or MIME type.

            Args:
                file_path (Union[str, Path]): The path to the file.

            Returns:
                bool: True if the file is a markdown file, False otherwise.
            >   .mkd.mdown	.markdown.mdTztext/markdown)ospathsplitextrM   r   r   r   )r   markdown_extensionsr   extr   s        r#   _is_markdown_filez9MarkdownMixin.save_to_markdown.<locals>._is_markdown_file(  sn     #I"H"HW%%c)nn55FAsyy{{111t$/I??LIq//r%   r%  )r  r  N)r   r   r;   r   r<   rA   
_save_datar  r_   r   r  )r"   rA   r  r  r   r   r+  rB   r   r<   r   s              r#   r  zMarkdownMixin.save_to_markdown  s	   	0D 	0 	0 	0 	0  ! ++ 	'd((**++B"3"3B"7"7BRYYUF7D!)__N&D):&):)::I&DNN&DN!'"NVATUU		

 	
 	
 	
 	
 	
 	
 	
 	
r%   save_mkd_funcsave_img_funcrA   datac                 d   t          |          }|dS |                                D ]\  }}|| j        v r ||                                |g|R i | t	          |t
                    rE|j        }	|                                D ])\  }
}|r" ||	|
z                                  |g|R i | *dS )a  Internal method to save markdown and image data.

        Args:
            save_mkd_func (Callable): Function to save markdown text.
            save_img_func (Callable): Function to save image data.
            save_path (Union[str, Path]): The base path where the data will be saved.
            data (Optional[Dict[str, Union[str, Dict[str, Any]]]]): The markdown data to save.
            *args: Additional positional arguments for saving.
            **kwargs: Additional keyword arguments for saving.
        N)r   r   MARKDOWN_SAVE_KEYSr@   r{   rL   parent)r"   r-  r.  rA   r/  r   r   r   valuer   img_pathimg_datas               r#   r,  zMarkdownMixin._save_dataJ  s   & OO	<F**,, 	 	JCd---i0022EKDKKKFKKK%&& 	!*!1*/++--  &Hh %+h6@@BB$ "   %	  	 	r%   )TFrF   )rG   rH   rI   rJ   r1  rK   rL   r$   r   r   rM   r
   r   r  rN   r  r  r   r   r	   r,  rO   r%   r#   r  r    se       @@*+	7d 	7d 	7 	7 	7 	7 /4" "	c5d38n,--	." " " ^" #$sE#tCH~*=$>>? # # # X# ;@+
	+
 +
 +
 +
Z""  " d#	"
 tCsDcN':!;;<=" 
" " " " " "r%   r  ).r   r   r   r&  abcr   pathlibr   typingr   r   r   r   r	   r
   numpyr|   r   r   PILr   utilsr   utils.ior   r   r   r   r   r   r   r   r   r   rQ   rc   rn   r   r   r   r   r   r   r   r	  r  rO   r%   r#   <module>r=     sb         				             = = = = = = = = = = = = = = = =                    
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
,' ,' ,' ,' ,' ,' ,' ,'^' ' ' ' ' ' ' 'T% % % % % % % %B  6r r r r r r r rj: : : : : : : :z;Y ;Y ;Y ;Y ;Y ;Y ;Y ;Y|=Y =Y =Y =Y =Y =Y =Y =Y@> > > > > > > >BB B B B B B B BJ<Y <Y <Y <Y <Y <Y <Y <Y~? ? ? ? ? ? ? ?Dw w w w w w w w w wr%   