
    Wik                     j    d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZ  G d de      Zy)	    N)pipeline)Pipeline)
csr_matrix)MappingListTupleAny)BaseRepresentationc                       e Zd ZdZdi dfdee   dedeeef   defdZ	d	e
j                  d
edeeeeeef      f   deeeeeef      f   fdZy)ZeroShotClassificationa  Zero-shot Classification on topic keywords with candidate labels.

    Arguments:
        candidate_topics: A list of labels to assign to the topics if they
                          exceed `min_prob`
        model: A transformers pipeline that should be initialized as
               "zero-shot-classification". For example,
               `pipeline("zero-shot-classification", model="facebook/bart-large-mnli")`
        pipeline_kwargs: Kwargs that you can pass to the transformers.pipeline
                         when it is called. NOTE: Use `{"multi_label": True}`
                         to extract multiple labels for each topic.
        min_prob: The minimum probability to assign a candidate label to a topic

    Usage:

    ```python
    from bertopic.representation import ZeroShotClassification
    from bertopic import BERTopic

    # Create your representation model
    candidate_topics = ["space and nasa", "bicycles", "sports"]
    representation_model = ZeroShotClassification(candidate_topics, model="facebook/bart-large-mnli")

    # Use the representation model in BERTopic on top of the default pipeline
    topic_model = BERTopic(representation_model=representation_model)
    ```
    zfacebook/bart-large-mnlig?candidate_topicsmodelpipeline_kwargsmin_probc                     || _         t        |t              rt        d|      | _        n#t        |t
              r|| _        nt        d      || _        || _        y )Nzzero-shot-classification)r   zwMake sure that the HF model that youpass is either a string referring to aHF model or a `transformers.pipeline` object.)	r   
isinstancestrr   r   r   
ValueErrorr   r   )selfr   r   r   r   s        m/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/representation/_zeroshot.py__init__zZeroShotClassification.__init__&   sZ     !1eS!!"<EJDJx(DJ@ 
  /     	documentsc_tf_idftopicsreturnc                    |j                         D cg c]%  }dj                  t        t        ||                ' }} | j                  || j
                  fi | j                  }i }t        |j                         |      D ]  \  }}	||   }
| j                  j                  d      r@g }
t        |	d   |	d         D ](  \  }}|| j                  kD  s|
j                  ||f       * n&|	d   d   | j                  kD  r|	d   d   |	d   d   fg}
t        |
      dk(  r||   }
n6t        |
      dk  r(|
t        dt        |
      z
        D cg c]  }d c}z  }
|
||<    |S c c}w c c}w )a  Extract topics.

        Arguments:
            topic_model: Not used
            documents: Not used
            c_tf_idf: Not used
            topics: The candidate topics as calculated with c-TF-IDF

        Returns:
            updated_topics: Updated topic representations
         multi_labellabelsscoresr   
   ) r   )keysjoinnextzipr   r   r   getr   appendlenrange)r   topic_modelr   r   r   topictopic_descriptionsclassificationsupdated_topicsclassificationtopic_descriptionlabelscore_s                 r   extract_topicsz%ZeroShotClassification.extract_topics;   s   & PV{{}]echhtC,?'@A]]$$**%79N9NgRVRfRfg %(%H 	6!E> &u ##''6$&!$'x(@.QYBZ$[ ALE5t}},)00%@A
  )!,t}}<&4X&>q&A>RZC[\]C^%_$`! $%*$*5M!&'",!uR#FWBX=X7Y%Z!g%ZZ!$5N5!'	6* 5 ^. &[s   *E"	E'N)__name__
__module____qualname____doc__r   r   r   r	   floatr   pd	DataFramer   r   r6    r   r   r   r   	   s    > 0-/!s)! ! !c*	!
 !*- <<- 	-
 T%U
"3445- 
d5e,--	.-r   r   )pandasr<   transformersr   transformers.pipelines.baser   scipy.sparser   typingr   r   r   r	   bertopic.representation._baser
   r   r>   r   r   <module>rE      s)     ! 0 # , , <_/ _r   