
    Wi                     V    d dl Zd dlmZmZ d dlmZ d dlmZ d dl	m
Z
  G d de
      Zy)    N)ListUnion)StaticModel)CountVectorizer)BaseEmbedderc            
            e Zd ZdZdi dfdeeef   dedededz  f fdZ	dd	e
e   d
edej                  fdZd Z xZS )Model2VecBackendaL  Model2Vec embedding model.

    Arguments:
        embedding_model: Either a model2vec model or a
                         string pointing to a model2vec model
        distill: Indicates whether to distill a sentence-transformers compatible model.
                 The distillation will happen during fitting of the topic model.
                 NOTE: Only works if `embedding_model` is a string.
        distill_kwargs: Keyword arguments to pass to the distillation process
                        of `model2vec.distill.distill`
        distill_vectorizer: A CountVectorizer used for creating a custom vocabulary
                            based on the same documents used for topic modeling.
                            NOTE: If "vocabulary" is in `distill_kwargs`, this will be ignored.

    Examples:
    To create a model, you can load in a string pointing to a
    model2vec model:

    ```python
    from bertopic.backend import Model2VecBackend

    sentence_model = Model2VecBackend("minishlab/potion-base-8M")
    ```

    or  you can instantiate a model yourself:

    ```python
    from bertopic.backend import Model2VecBackend
    from model2vec import StaticModel

    embedding_model = StaticModel.from_pretrained("minishlab/potion-base-8M")
    sentence_model = Model2VecBackend(embedding_model)
    ```

    If you want to distill a sentence-transformers model with the vocabulary of the documents,
    run the following:

    ```python
    from bertopic.backend import Model2VecBackend

    sentence_model = Model2VecBackend("sentence-transformers/all-MiniLM-L6-v2", distill=True)
    ```
    FNembedding_modeldistilldistill_kwargsdistill_vectorizerc                    t         |           || _        || _        || _        d| _        | j                  rN| j                          | j                  st               | _        t        |t              r|| _
        y t        d      t        |t              r|| _
        y t        |t              rt        j                  |      | _
        y t        d      )NFzNPlease pass a string pointing to a sentence-transformer model when distilling.zPlease select a correct Model2Vec model: 
`from model2vec import StaticModel` 
`model = StaticModel.from_pretrained('minishlab/potion-base-8M')`)super__init__r   r   r   _has_distilled_check_model2vec_installationr   
isinstancestrr
   
ValueErrorr   from_pretrained)selfr
   r   r   r   	__class__s        g/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/backend/_model2vec.pyr   zModel2VecBackend.__init__6   s     	,"4# <<..0***9*;'/3/'6$ !qrr 5#2D -#.#>#>#OD T     	documentsverbosereturnc                 T   | j                   r| j                  sddlm } | j                  j	                  d      s| j
                  j                  |      }t        j                  |j                  d            j                         }| j
                  j                         }t        t        ||      d d      D cg c]  \  }}|	 }	}}|	| j                  d<    || j                  fi | j                  | _        d| _        | j                  j                  ||      }
|
S c c}}w )	a  Embed a list of n documents/words into an n-dimensional
        matrix of embeddings.

        Arguments:
            documents: A list of documents or words to be embedded
            verbose: Controls the verbosity of the process

        Returns:
            Document/words embeddings with shape (n, m) with `n` documents/words
            that each have an embeddings size of `m`
        r   r   
vocabulary)axisc                     | d   S )N    )xs    r   <lambda>z(Model2VecBackend.embed.<locals>.<lambda>o   s    `abc`d r   T)keyreverse)show_progress_bar)r   r   model2vec.distillr   getr   fit_transformnparraysumflattenget_feature_names_outsortedzipr
   encode)r   r   r   r   Xword_countswordsword_r    
embeddingss              r   embedzModel2VecBackend.embedZ   s    << 3 31 &&**<8++99)D hhquu!u}5==?//EEG28UK9PVdnr2stwtQdt
t4>##L1 $+4+?+?#W4CVCV#WD  #'D ))00g0V
 us   >D$c                 B    	 ddl m} y # t        $ r t        d      w xY w)Nr   r   zTTo distill a model using model2vec, you need to run `pip install model2vec[distill]`)r*   r   ImportError)r   r   s     r   r   z.Model2VecBackend._check_model2vec_installation}   s(    	v1 	vtuu	vs   	 )F)__name__
__module____qualname____doc__r   r   r   booldictr   r   r-   ndarrayr;   r   __classcell__)r   s   @r   r	   r	   	   sw    *^ !)-"sK/0" " 	"
  $J"H!tCy !4 !BJJ !Fvr   r	   )numpyr-   typingr   r   	model2vecr   sklearn.feature_extraction.textr   bertopic.backendr   r	   r$   r   r   <module>rK      s$      ! ; )xv| xvr   