U
    9%e                     @   s   d dl mZ d dlmZmZ d dlZddlmZmZm	Z	m
Z
mZ ddlmZmZ e rjd dlZd dlmZ e	 r|dd	lmZ e
eZeeG d
d deZdS )    )BytesIO)ListUnionN   )add_end_docstringsis_decord_availableis_torch_availableloggingrequires_backends   )PIPELINE_INIT_ARGSPipeline)VideoReader),MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMESc                       sd   e Zd ZdZ fddZdddZeeee f d fdd	Z	dddZ
dd ZdddZ  ZS )VideoClassificationPipelinea  
    Video classification pipeline using any `AutoModelForVideoClassification`. This pipeline predicts the class of a
    video.

    This video classification pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"video-classification"`.

    See the list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=video-classification).
    c                    s&   t  j|| t| d | t d S )Ndecord)super__init__r
   Zcheck_model_typer   )selfargskwargs	__class__ j/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/transformers/pipelines/video_classification.pyr   "   s    
z$VideoClassificationPipeline.__init__Nc                 C   sB   i }|d k	r||d< |d k	r$||d< i }|d k	r8||d< |i |fS )Nframe_sampling_rate
num_framestop_kr   )r   r   r   r   Zpreprocess_paramsZpostprocess_paramsr   r   r   _sanitize_parameters'   s    z0VideoClassificationPipeline._sanitize_parameters)videosc                    s   t  j|f|S )a$  
        Assign labels to the video(s) passed as inputs.

        Args:
            videos (`str`, `List[str]`):
                The pipeline handles three types of videos:

                - A string containing a http link pointing to a video
                - A string containing a local path to a video

                The pipeline accepts either a single video or a batch of videos, which must then be passed as a string.
                Videos in a batch must all be in the same format: all as http links or all as local paths.
            top_k (`int`, *optional*, defaults to 5):
                The number of top labels that will be returned by the pipeline. If the provided number is higher than
                the number of labels available in the model configuration, it will default to the number of labels.
            num_frames (`int`, *optional*, defaults to `self.model.config.num_frames`):
                The number of frames sampled from the video to run the classification on. If not provided, will default
                to the number of frames specified in the model configuration.
            frame_sampling_rate (`int`, *optional*, defaults to 1):
                The sampling rate used to select frames from the video. If not provided, will default to 1, i.e. every
                frame will be used.

        Return:
            A dictionary or a list of dictionaries containing result. If the input is a single video, will return a
            dictionary, if the input is a list of several videos, will return a list of dictionaries corresponding to
            the videos.

            The dictionaries contain the following keys:

            - **label** (`str`) -- The label identified by the model.
            - **score** (`int`) -- The score attributed by the model for that label.
        )r   __call__)r   r   r   r   r   r   r    3   s    !z$VideoClassificationPipeline.__call__r   c           	      C   s   |d kr| j jj}|ds&|dr6tt|j}t|}|	d d}|| d }t
j|||t
jd}|| }t|}| j|| jd}|S )Nzhttp://zhttps://r   r   )numZdtype)Zreturn_tensors)modelconfigr   
startswithr   requestsgetcontentr   seeknpZlinspaceZint64Z	get_batchZasnumpylistZimage_processor	framework)	r   Zvideor   r   ZvideoreaderZ	start_idxZend_idxindicesmodel_inputsr   r   r   
preprocessV   s    

z&VideoClassificationPipeline.preprocessc                 C   s   | j f |}|S )N)r"   )r   r-   model_outputsr   r   r   _forwardj   s    z$VideoClassificationPipeline._forward   c                    sz   | j jjkr j jj} jdkrB|jdd }||\}}ntd j | }| } fddt	||D S )Nptr   zUnsupported framework: c                    s$   g | ]\}}| j jj| d qS ))scorelabel)r"   r#   Zid2label).0r4   Z_idr   r   r   
<listcomp>z   s     z;VideoClassificationPipeline.postprocess.<locals>.<listcomp>)
r"   r#   Z
num_labelsr+   ZlogitsZsoftmaxZtopk
ValueErrortolistzip)r   r/   r   ZprobsZscoresZidsr   r7   r   postprocessn   s    

z'VideoClassificationPipeline.postprocess)NNN)Nr   )r1   )__name__
__module____qualname____doc__r   r   r   strr   r    r.   r0   r<   __classcell__r   r   r   r   r      s   
#
r   )ior   typingr   r   r%   utilsr   r   r   r	   r
   baser   r   numpyr)   r   r   Zmodels.auto.modeling_autor   Z
get_loggerr=   loggerr   r   r   r   r   <module>   s   
