o
    XhE                     @   sd   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	 d dl
mZ e  G dd dZdS )	    N)Groq)AsyncSession)
Prediction)encode_image)load_dotenvc                   @   sF   e Zd ZdefddZdededefddZd	ed
edefddZdS )ImageComparatordbc                 C   s   || _ ttdd| _d S )NGROQ_API_KEY)api_key)r   r   osgetenvclient)selfr    r   K/var/www/html/construction_image-detection-poc/src/components/comparison.py__init__   s   zImageComparator.__init__base64_1base64_2returnc                    s   z?t j| jjjjddddddddd	d
d| iddddd	d
d| iddddgdgdddI d H }|jd jjW S  t	yY } zdt
| W  Y d }~S d }~ww )Nz-meta-llama/llama-4-maverick-17b-128e-instructsystemzYou are a professional construction site analyst with expertise in identifying and assessing construction materials, equipment, and safety conditions.)rolecontentusertextaG  Compare these two construction site images and provide ONLY:

1. A concise bulleted list of changes between Image 1 and Image 2, focusing on:
   - New materials/equipment that have appeared
   - Materials/equipment that have been removed
   - Materials/equipment that have been relocated
   - Changes in quantity of materials
   - Progress indicators (e.g., "scaffolding extended", "bricks used")
   - Any new safety concerns

2. A single summary sentence about the overall progress made between the two images.

Focus on these construction elements:
- Wooden logs
- Concrete Mixer
- Rebar
- Scaffolding
- Doors
- Piles of Bricks
- Metallic Shuttering

Keep your response brief, factual, and structured for easy conversion to JSON format. Do not include any introductory text, analysis methodology, or conclusions beyond what is requested.)typer   	image_urlurlzdata:image/jpeg;base64,)r   r   zImage 1 (Earlier timestamp)zImage 2 (Later timestamp)g333333?i  )modelmessagestemperaturemax_completion_tokensr   zError during comparison: )asyncio	to_threadr   chatcompletionscreatechoicesmessager   	Exceptionstr)r   r   r   responseer   r   r   generate_comparison   s,   
%z#ImageComparator.generate_comparisonprediction_idbefore_path
after_pathc           	   
      s   zHt |I d H }t |I d H }| ||I d H }| jt|I d H }|r?d|i|_d|_| j I d H  | j|I d H  nt	d||jdW S  t	y^ } z	t	dt
| d }~ww )Ncomparison_report
processingzPrediction not found)r0   statusz#Failed to run the text comparison: )r   r,   r   getr   text_resultsr2   commitrefreshr(   r)   )	r   r-   r.   r/   r   r   comparison_text
predictionr+   r   r   r   run_comparison;   s&   
zImageComparator.run_comparisonN)	__name__
__module____qualname__r   r   r)   r,   intr9   r   r   r   r   r      s    +r   )r   r!   groqr   sqlalchemy.ext.asyncior   src.models.predictionr   src.utils.files_utilsr   dotenvr   r   r   r   r   r   <module>   s    