o
    wia                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZ G dd	 d	eZejG d
d dZedddg d ejdddZedddg d ejdddZeddddgddggdejddZedddg d ejd d!d!d"	Zed#dd$g d ejd%d!d&d"	Zed'dd$g d ejd%d!d&d"	Zed'd(d)g d d*ej d+d*d,	Z!ed-d(d.g d d*ej"d/d*d,	Z#ed-dd$g d ejd-d!d&d"	Z$ed0dd$g d ejd%d!d&d"	Z%ed1dd2g d ejd%d!d&d"	Z&ed3d4d5g d ej'd6d7Z(ed8d9d:g d ej)d6d7Z*ed-d;d<g d ej+d=d7Z,ed-d>g d ej-d-ddd?Z.eddg d ejddZ/ed@dg d ejddAdBZ0edddg d ejd d!dZ1ed@dg d ejd d!dCdDZ2edddEg d ej3d-d!d!d"	Z4edFd4d5g d ej'd6d7Z5edGdHd5g d ej'd!d7Z6edId4d5g d ej'd6d7Z7edId4d5g d ej'd6d7Z8eZ9i dJedKededLedMedNe#dOe$dPe5dQe6dRe7dSe4dTe.dUe.dVe8dWe/dXe0dYe1e2ee!e%e&e$e(e*e,eeedZZ:e;d[kre<e9=  dS dS )\    N)defaultdict)Enumauto)BytesIO)AnyListOptionalUnion)Image)AutoTokenizerc                   @   sX   e Zd ZdZe Ze Ze Ze Ze Z	e Z
e Ze Ze Ze Ze Ze ZdS )SeparatorStylezDifferent separator style.N)__name__
__module____qualname____doc__r   SINGLETWOMPTPLAINCHATMLLLAMA_2LLAMA_3MLLAMAMISTRALNVGPTQWENGEMMA r   r   h/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/collections/vlm/neva/data/conversation.pyr      s    
r   c                   @   s  e Zd ZU dZee ed< eeef ed< eee  ed< e	ed< e
jZe
ed< dZeed< d	Zeed
< dZeed< d	Zeed< d	Zeeee f ed< d	Zee	 ed< dZeed< dd Zdd Zdd Zdd Zd%ddZd&ddZdd  Zd!d" Zd#d$ Zd	S )'Conversationz,A class that keeps all conversation history.systemrolesmessagesoffset	sep_style###sepNsep2Unknownversiontokenizer_name_or_pathstop_strstop_token_idsF	skip_nextc                 C   s   |S Nr   )selfr"   r   r   r   process_prompt_with_images?   s   z'Conversation.process_prompt_with_imagesc                 C   s^   t |}| jd u rg }nd| jdg}|D ]\}}|| |d q|j|ddd}|S )Nr    )rolecontentF)tokenizeadd_generation_prompt)r   from_pretrainedr    appendlowerapply_chat_template)r/   r*   r"   	tokenizerchatr1   messageretr   r   r   process_chat_templateC   s   

z"Conversation.process_chat_templatec                 C   s.  | j }| jtjkr7| j| j }|D ]#\}}|r.t|tu r"|\}}}||d | | j 7 }q||d 7 }q|S | jtjkr	 | j| j	g}| j|d  }t
|D ]6\}\}}|rt|tu rb|\}}}||d | ||d   7 }d| j | d  | j | d< qO||d 7 }qO|S | jtjkr| jdkr	 dd	 }d
d	 }	d}t
|D ]Q\}\}}|dkr|sJ d|| jd ksJ d|rt|tu r|\}}}|dkr|| j| }|d dkr|	|}|| j| 7 }q||| j	 7 }q|d7 }q|S | jtjkr	 | jpd}
| |
|}|S | jtjkr"	 | jpd}
| |
|}|S | jtjkr7| jp.d}
| |
|}|S | jtjkru| j	| j | j }|D ])\}}|rkt|tu r\|\}}}||d | d | j 7 }qI||d 7 }qI|S | jtjkr| j| j	g}| j}t
|D ]&\}\}}|rt|tu r|\}}}||||d   7 }q|d7 }q|S | jtjkr	 | jpd}
| |
|}|S | jtjkr	 | jdkrdn| j| j d }|D ]0\}}|rt|tu r|\}}dt| | }||d | | j d 7 }q||d 7 }q|S | jtjkrK	 | j| j }|D ]#\}}|rCt|tu r8|\}}}||| | j 7 }q%||7 }q%|S | jtjkr	 d}t
|D ]3\}\}}|| j|d  ksmJ d|rt|tu r||\}}}||| | j 7 }qY||7 }qY|S td| j )Nz: :r          vilac                 S   s   |  | rd S d S )N
 r   msgr   r   r   <lambda>o   s    z)Conversation.get_prompt.<locals>.<lambda>c                 S   s   d|  dS )Nz[INST] z [/INST]r   rE   r   r   r   rG   p   s    <s>z first message should not be nonez#first message should come from userrD   zmeta-llama/Llama-2-7b-chat-hf#meta-llama/Meta-Llama-3-8B-Instruct(meta-llama/Llama-3.2-11B-Vision-InstructrC   z"mistralai/Mistral-7B-Instruct-v0.2<image>z?Conversation should alternate user/assistant/user/assistant/...zInvalid style: )r"   r$   r   r   r    r&   typetupler   r'   	enumerater   r)   r!   r   r*   r=   r   r   r   r   r   lenr   r   
ValueError)r/   r"   r<   r1   r;   _sepsiwrap_sys	wrap_instr*   imagesr   r   r   
get_promptN   s   
  
  

i^OK
A
6/ 

zConversation.get_promptc                 C   s   | j ||g d S r.   )r"   r6   )r/   r1   r;   r   r   r   append_message   s   zConversation.append_messagePNGc                 C   s  |dkrddd}||}n|dv rn|dkr| d}ntd| t|tjur3t|d	}t|jt|j}}|| }d
\}	}
t	t|	| |
|}t	|| }|j\}}||kre||}}n||}}| ||f}|ru|S t
 }|j||d t|  }|S )NPadz   t   h   c                 S   s~   | j \}}||kr| S ||kr't| j||f|}|| d|| d f |S t| j||f|}|| || d df |S )Nr   r?   )sizer
   newmodepaste)pil_imgbackground_colorwidthheightresultr   r   r   expand2square   s   
z1Conversation.process_image.<locals>.expand2square)DefaultCropResize)P  rl   zInvalid image_process_mode: RGB)i  i  )format)r[   )resizerP   rL   r
   openconvertmaxr_   minintr   savebase64	b64encodegetvaluedecode)r/   imageimage_process_mode
return_pilimage_formatrh   max_hwmin_hwaspect_ratiomax_lenmin_lenshortest_edgelongest_edgeWHbufferedimg_b64_strr   r   r   process_image   s4   



zConversation.process_imagec           
      C   s   g }t | j| jd  D ]4\}\}}|d dkr@t|tu r@|\}}}t|tkr,|g}|D ]}	|s:| j|	||d}	||	 q.q|S )Nr?   r   )r|   )rN   r"   r#   rL   rM   listr   r6   )
r/   r|   return_pathrV   rS   r1   rF   rz   r{   imgr   r   r   
get_images  s    
zConversation.get_imagesc           
      C   s   g }t | j| jd  D ]q\}\}}|d dkrwt|tu ro|\}}}t|tkr,|g}t|dkr=d|dd  }nt	
dd|}|D ]}| j|d	d
dd}d| d}	|d|	d }qFt|dkrn||d g q||d g q||d d< q|S )Nr?   r   rA   z<image>
rK   rD   z(<image>)\n(?=<image>)z\1 ri   FJPEG)r|   r}   z!<img src="data:image/jpeg;base64,z"/>)rN   r"   r#   rL   rM   r   rO   replacestripresubr   r6   )
r/   r<   rS   r1   rF   rz   r{   r   r   img_strr   r   r   to_gradio_chatbot,  s(    
zConversation.to_gradio_chatbotc              
   C   s2   t | j| jdd | jD | j| j| j| j| jdS )Nc                 S   s   g | ]\}}||gqS r   r   .0xyr   r   r   
<listcomp>H  s    z%Conversation.copy.<locals>.<listcomp>r    r!   r"   r#   r$   r&   r'   r)   )	r   r    r!   r"   r#   r$   r&   r'   r)   r/   r   r   r   copyD  s   zConversation.copyc                 C   sV   t |  dkr| j| jdd | jD | j| j| jdS | j| j| j| j| j| jdS )Nr   c                 S   s,   g | ]\}}|t |tu r|d  n|gqS )r   )rL   rM   r   r   r   r   r   U  s   , z%Conversation.dict.<locals>.<listcomp>)r    r!   r"   r#   r&   r'   )rO   r   r    r!   r"   r#   r&   r'   r   r   r   r   dictP  s   	zConversation.dict)FrY   )FF) r   r   r   r   r   str__annotations__rM   r   rt   r   r   r$   r&   r'   r)   r*   r   r+   r	   r,   r-   boolr0   r=   rW   rX   r   r   r   r   r   r   r   r   r   r   ,   s0   
   

.r   zA chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.

)User	Assistantnvgptz<extra_id_1>z<extra_id_0>System
)r    r!   r)   r"   r#   r$   r&   r'   rC   nv_dpozA chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.)Humanr   r   zPWhat are the key differences between renewable and non-renewable energy sources?r   ag  Renewable energy sources are those that can be replenished naturally in a relatively short amount of time, such as solar, wind, hydro, geothermal, and biomass. Non-renewable energy sources, on the other hand, are finite and will eventually be depleted, such as coal, oil, and natural gas. Here are some key differences between renewable and non-renewable energy sources:
1. Availability: Renewable energy sources are virtually inexhaustible, while non-renewable energy sources are finite and will eventually run out.
2. Environmental impact: Renewable energy sources have a much lower environmental impact than non-renewable sources, which can lead to air and water pollution, greenhouse gas emissions, and other negative effects.
3. Cost: Renewable energy sources can be more expensive to initially set up, but they typically have lower operational costs than non-renewable sources.
4. Reliability: Renewable energy sources are often more reliable and can be used in more remote locations than non-renewable sources.
5. Flexibility: Renewable energy sources are often more flexible and can be adapted to different situations and needs, while non-renewable sources are more rigid and inflexible.
6. Sustainability: Renewable energy sources are more sustainable over the long term, while non-renewable sources are not, and their depletion can lead to economic and social instability.
r?   r%   )r    r!   r"   r#   r$   r&   zA chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.)USER	ASSISTANTv1r@   z</s>)	r    r!   r)   r"   r#   r$   r&   r'   r+   a  You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe.  Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.

If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.llama_v2rH   z </s>zYou are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.)user	assistantllama_v3z
<|eot_id|>rI   )	r    r!   r)   r"   r#   r&   r$   r*   r+   rD   
llama_v3_2rJ   zEAnswer the questions about the visual content that the user provides.zAnswer the questions about the visual content that the user provides.The visual content will be provided with the following format: <Image>visual content</Image>.llama_v2_mmtagz<|im_start|>system
A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.)z<|im_start|>user
z<|im_start|>assistant
mptz
<|im_end|>)r    r!   r)   r"   r#   r$   r&   z/<|im_start|>system
You are a helpful assistant.)z<|im_start|>userz<|im_start|>assistantqwen)z<start_of_turn>user
z<start_of_turn>model
gemmaz<end_of_turn>
)rD   rD   )r    r!   r"   r#   r$   r&   r'   r+   a7  A chat between a curious user and an artificial intelligence assistant. The assistant is able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.The visual content will be provided with the following format: <Image>visual content</Image>.v0_mmtag)r    r!   r"   r#   r$   r&   r)   v1_mmtagr   rB   z<|im_start|>system
You are MistralOrca, a large language model trained by Alignment Lab AI. Write out your reasoning step-by-step to be sure you get the right answers!z*<|system|>
You are a helpful AI assistant.)z	<|user|>
z<|assistant|>
z(<|im_start|>system
Answer the questions.defaultv0	vicuna_v1llama_2mllamamistral_instructmistral_orcamistral_zephyrmistral_directmistralplainv0_plainchatml_directllava_v0llava_v0_mmtagllava_v1)llava_v1_mmtagllava_llama_2llava_llama_3llava_llama_2_simplellava_llama_2_mmtagllava_mistral_instructr   qwen_1_5gemma_instructr   
nv_steerlmr   __main__)>rv   dataclassesr   collectionsr   enumr   r   ior   typingr   r   r   r	   PILr
   transformersr   r   	dataclassr   r   
conv_nvgptconv_nv_dpor   conv_vicuna_v0r   conv_vicuna_v1r   conv_llama_2conv_llava_llama_2r   conv_llava_llama_3r   conv_mllamaconv_mistral_instructconv_llava_llama_2_simpleconv_llava_llama_2_mmtagr   conv_mptr   	conv_qwenr   conv_gemma_instructr   conv_llava_plainconv_llava_v0conv_llava_v0_mmtagconv_llava_v1conv_llava_v1_mmtagr   conv_mistral_vilaconv_mistral_orcaconv_mistral_zephyrconv_mistral_directconv_chatml_directdefault_conversationconv_templatesr   printrW   r   r   r   r   <module>   sf    :!

	

 