
    Wi*                         d dl Z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mZ d dlmZ d dlmZ  G d	 d
e      Zd Zd ZddZy)    N)Image)tqdm)
csr_matrix)MappingListTupleUnion)Pipelinepipeline)mmr)BaseRepresentationc                   *   e Zd ZdZ	 	 	 	 	 	 ddededeeef   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ddee   dedee   fdZd
ej                  dej(                  dej                  fdZd Zy)VisualRepresentationa   From a collection of representative documents, extract
    images to represent topics. These topics are represented by a
    collage of images.

    Arguments:
        nr_repr_images: Number of representative images to extract
        nr_samples: The number of candidate documents to extract per cluster.
        image_height: The height of the resulting collage
        image_square: Whether to resize each image in the collage
                      to a square. This can be visually more appealing
                      if all input images are all almost squares.
        image_to_text_model: The model to caption images.
        batch_size: The number of images to pass to the
                    `image_to_text_model`.

    Usage:

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

    # The visual representation is typically not a core representation
    # and is advised to pass to BERTopic as an additional aspect.
    # Aspects can be labeled with dictionaries as shown below:
    representation_model = {
        "Visual_Aspect": VisualRepresentation()
    }

    # Use the representation model in BERTopic as a separate aspect
    topic_model = BERTopic(representation_model=representation_model)
    ```
    Nnr_repr_images
nr_samplesimage_heightimage_squaresimage_to_text_model
batch_sizec                    || _         || _        || _        || _        t	        |t
              r|| _        || _
        y t	        |t              rt        d|      | _        || _
        y |d | _        || _
        y t        d      )Nzimage-to-text)modelzPlease select a correct transformers pipeline. For example:pipeline('image-to-text', model='nlpconnect/vit-gpt2-image-captioning'))r   r   r   r   
isinstancer
   r   strr   
ValueErrorr   )selfr   r   r   r   r   r   s          k/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/representation/_visual.py__init__zVisualRepresentation.__init__0   s     -$(* )84':D$ % +S1'/GZ'[D$ % !('+D$ %	 Z     	documentsc_tf_idftopicsreturnc                    |d   j                   j                         }|j                  |||| j                  | j                        \  }}}}t        t        |j                                     }i }	t        |      D ]  }
||
|j                  z      }t        dt        |      d      D cg c]
  }|||dz     }}|D cg c]@  }|D cg c]2  }t        ||   t              rt        j                  ||         n||   4 c}B }}}t!        || j"                  | j$                        }||	|
<   t        |d   t              s|D ]  }|D ]  }|j'                             |	S c c}w c c}w c c}}w )aS  Extract topics.

        Arguments:
            topic_model: A BERTopic model
            documents: All input documents
            c_tf_idf: The topic c-TF-IDF representation
            topics: The candidate topics as calculated with c-TF-IDF

        Returns:
            representative_images: Representative images per topic
        r   )r   nr_repr_docsr      )valuestolist_extract_representative_docsr   r   sortedlistkeysr   	_outliersrangelenr   r   r   openget_concat_tile_resizer   r   close)r   topic_modelr   r    r!   images_repr_docs_idsunique_topicsrepresentative_imagestopicsliced_examplarsisub_indicesindeximages_to_combinerepresentative_image
image_listimages                      r   extract_topicsz#VisualRepresentation.extract_topicsL   s   & 7#**113#.#K#K,, $L $
 Aq- tFKKM23 !#-( 	&E,U[5J5J-JKEJ1cRbNcefEgh 0QU ;hh $4!
   "- 2<F5M31OEJJve}-U[\aUbb! ! $::KTM^M^`d`r`r#s +?!%( &)S)"3 &J!+ &&&%	&, %$'  i!s   )E#?	E-7E(?E-(E-r3   verbosec                 8   | j                   ]g }t        | j                  |      |       D ]9  }| j                  |      }|D cg c]
  }|d   d    }}|j	                  |       ; |S | j                  |      }|D cg c]
  }|d   d    }}|S c c}w c c}w )zConvert a list of images to captions.

        Arguments:
            images: A list of images or words to be converted to text.
            verbose: Controls the verbosity of the process

        Returns:
            List of captions
        )disabler   generated_text)r   r   _chunksr   extend)r   r3   rB   r   batchoutputsoutputcaptionss           r   _convert_image_to_textz+VisualRepresentation._convert_image_to_text   s     ??&Idll62KH +2259FMNFF1I&67NN  *+  ..v6GCJK#34KIK O Ls   B?B
embeddingsc           	         |j                   j                  j                         }|j                  j                  j                         }t	        j
                  t        j                  t        j                  |      j                  dd      |g            }|j                  d      j                         j                  }i }t        t        t        |                  }t        ||      D ]  \  }	}
t        j                  t!        |      D cg c]  \  }}||	k(  s| c}}      }t#        | j$                  t'        |      g      }t)        |
j                  dd      ||   ||d      }|||	<    t	        j
                  g d      }d}t+        |j-                               D ]  \  }	}|D cg c]2  }t/        ||   t0              rt        j2                  ||         n||   4 }}| j5                  |      }t        ||      D ]-  \  }}|||	||   g|j6                  t'        |      ddf<   |dz  }/ t/        ||d      t0              s|D ]  }|j9                            |S c c}}w c c}w )	zConvert images to text.   r   g?)top_n	diversity)DocumentIDTopicr   )columnsN)rU   r&   r'   r   pd	DataFramenphstackarrayreshapegroupbymeanr)   r*   setzip	enumerateminr   r.   r   r   itemsr   r   r/   rL   locr1   )r   r   rM   r!   r3   dfimage_topic_embeddingsimage_centroidsr6   r8   topic_embeddingr<   tindicesrQ   
current_id	image_idsselected_imagestextdocimage_idr@   s                         r   image_to_textz"VisualRepresentation.image_to_text   sL    ''..0''..0\\"))RXXf%5%=%=b!%Dj$QRS!#A!3!3!5!<!< tCK01&)-9O&P 
	-"E?hhi6GV(%1PU:VWG,,c'l;<E''2.7#G &-OE"
	- LL)MN	
 $_%:%:%< = 	"E9luchZus-K

6%=)QWX]Q^^O  ..?D!$T9!5  X8$	4	c)na/0 a
  &1.4, "EKKM"#	"( E  Ws   I&I&.7I,c              #   ~   K   t        dt        |      | j                        D ]  }|||| j                  z      y w)Nr   )r-   r.   r   )r   r3   r:   s      r   rF   zVisualRepresentation._chunks   s<     q#f+t7 	2AQ011	2s   ;=)	   i  X  FN    )F)__name__
__module____qualname____doc__intr   boolr	   r   r
   r   rW   rX   r   r   r   floatrA   rL   rY   ndarrayrq   rF    r   r   r   r      s"   F  (+#48%% % CHo	%
 % #3=1% %85% <<5% 	5%
 T%U
"34455% 
d5e,--	.5%nT#Y  RVWZR[ 2.r|| . .PRP\P\ .`2r   r   c                    t        d | D              }t        d | D              }g }| D ]K  }|j                  t        |j                  |z  |j
                  z        |fd       |j                  |       M t        d |D              }t        j                  d||fd      }d}|D ]%  }|j                  ||df       ||j                  z  }' |S )HCode adapted from: https://note.nkmk.me/en/python-pillow-concat-images/.c              3   4   K   | ]  }|j                     y wNheight.0ims     r   	<genexpr>z,get_concat_h_multi_resize.<locals>.<genexpr>        12RYY1   c              3   4   K   | ]  }|j                     y wr   r   r   s     r   r   z,get_concat_h_multi_resize.<locals>.<genexpr>   r   r   r   resamplec              3   4   K   | ]  }|j                     y wr   widthr   s     r   r   z,get_concat_h_multi_resize.<locals>.<genexpr>   s     82bhh8r   RGB   r   r   )rb   maxresizerz   r   r   appendsumr   newpaste)im_list
min_heightim_list_resizer   total_widthdstpos_xs          r   get_concat_h_multi_resizer      s    111J111JN "
		3rxx*,ryy89:FQR	Sb!" 888K
))EK4o
FCE 		"uaj! Jr   c           
         t        d | D              }t        d | D              }| D cg c]:  }|j                  |t        |j                  |z  |j
                  z        fd      < }}t        d |D              }t        j                  d||fd      }d}|D ]%  }|j                  |d|f       ||j                  z  }' |S c c}w )r   c              3   4   K   | ]  }|j                     y wr   r   r   s     r   r   z,get_concat_v_multi_resize.<locals>.<genexpr>        /BHH/r   c              3   4   K   | ]  }|j                     y wr   r   r   s     r   r   z,get_concat_v_multi_resize.<locals>.<genexpr>   r   r   r   r   c              3   4   K   | ]  }|j                     y wr   r   r   s     r   r   z,get_concat_v_multi_resize.<locals>.<genexpr>   s     :Rryy:r   r   r   )
rb   r   r   rz   r   r   r   r   r   r   )r   	min_widthr   r   total_heightr   pos_ys          r   get_concat_v_multi_resizer      s    /w//I/w//IjqrdfbiiC		I0E0P,Q R]^i_rNr:>::L
))EI|4o
FCE 		"q%j! J ss   ?Cc                 b   | D cg c]   }|D cg c]  }|j                          c}" c}}}|rOt        |dz        }t        |dz        }| D cg c]#  }|D cg c]  }|j                  ||f       c}% c}}}ngt        | D cg c]%  }t        |D cg c]  }|j                   c}      ' c}}      }	t        | D cg c]%  }t        |D cg c]  }|j
                   c}      ' c}}      }
t        |      D ]  \  }}t        |      D ]  \  }}|j
                  |j                  kD  r?|j                  t        |j                  |
z  |j
                  z        |
fd      ||   |<   ^|j                  |j
                  kD  r?|j                  |	t        |j
                  |	z  |j                  z        fd      ||   |<   |j                  |	|	f      ||   |<     |D cg c]  }t        |       }}t        |      }|t        |j                  d         z  }t        t        |j                  d         t        |      z        }|j                  ||ft        j                  j                        }|S c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w c c}}w c c}w )r   r%   r   r   rP   )copyrz   r   rb   r   r   ra   r   r   r|   sizer   
ResamplingLANCZOS)
im_list_2dr   r   r3   r@   r   r   imgsimgr   r   r:   j	im_list_hheight_percentageadjusted_widths                   r   r0   r0      sU   ?IJV0uzz|0JF L1$%\A%&T^_&fEU5<<0E_ jQd48Ccii89QR	
S#T:cszz:;ST
 ( 
	FGAt#D/ 	F3::		)#&::SYY3cjj@A:N!" $. $F1IaL YY+#&::y#cjj9>TWZW`W`>`:a.bmn:#oF1IaL#&::y).D#EF1IaL	F
	F EKKy'	2KFK
#F
+C$uSXXa['99%,u5F/GGIN
**nl3U5E5E5M5M
NCJ? 1J F_ 9Q:S Lsc   	JJ J	JJ5J
J
J+	J
J&
J!'	J&
+J, JJJ
!J&
)rt   F)numpyrY   pandasrW   PILr   r   scipy.sparser   typingr   r   r   r	   transformers.pipelinesr
   r   bertopic.representation._mmrr   bertopic.representation._baser   r   r   r   r0   r~   r   r   <module>r      sB        # . . 5 , <@2- @2F$!r   