
    $i                         d dl mZ d dlmZ d dlmZ d dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZ  G d d          Zded	efd
Zdeded	efdZdS )    )genai)types)ImageN)load_dotenvc                   b    e Zd Zdee         dee         dedefdZdefdZdefdZ	d	 Z
d
 ZdS )
BBoxResultbounding_boxcentertokens_used	costs_usdc                 >    || _         || _        || _        || _        d S Nr	   r
   r   r   )selfr	   r
   r   r   s        ,/home/byschii/byschiidev/penelope/llm/llm.py__init__zBBoxResult.__init__   s$    (&"    returnc                 N    t          | j                                                  S r   )sumr   valuesr   s    r   token_totalzBBoxResult.token_total   s    4#**,,---r   c                 N    t          | j                                                  S r   )r   r   r   r   s    r   
cost_totalzBBoxResult.cost_total   s    4>((**+++r   c                 8    | j         | j        | j        | j        dS )Nr   r   r   s    r   __dict__zBBoxResult.__dict__   s'     -k+	
 
 	
r   c           	      H    d| j          d| j         d| j         d| j         d	S )NzBBoxResult(bounding_box=z	, center=z, tokens_used=z, costs_usd=)r   r   s    r   __repr__zBBoxResult.__repr__"   sa     Q$*;  Q  Qdk  Q  Qaeaq  Q  Q  @D  @N  Q  Q  Q  	Qr   N)__name__
__module____qualname__listintdictr   r   floatr   r   r     r   r   r   r      s        #T#Y #S	 #PT #ae # # # #.S . . . .,E , , , ,
 
 
Q Q Q Q Qr   r   	thresholdr   c                     t          j                     }|| k    rt          j        dd          S t          j        dd          S )z
    Returns GEMINI_API_KEY_PAGA if a random float is under the threshold,
    otherwise returns GEMINI_API_KEY_GRATIS.
    The bigger the threshold, the more likely to get the GRATIS key.
    GEMINI_API_KEY_PAGA GEMINI_API_KEY_GRATIS)randomosgetenv)r)   rs     r   &get_gemini_api_key_by_random_thresholdr2   &   s>     	A9}}y.3339,b111r   object_descriptionimage_base64c                 4   t                       t          j        t          d                    }t	          j        |          }t          j        t          j	        |                    }d|  d}t          j        dt          j        d          d	          }|j                            d
||g|          }|j        j        }|j        j        }	|j        j        }
d}d}|j        \  }}t)          d| d| d           t)          d|j                    t-          j        |j                  }t1          |d         d         dz  |z            t1          |d         d         dz  |z            t1          |d         d         dz  |z            t1          |d         d         dz  |z            g}t)          d|            |d         |d         z   dz  |d         |d         z   dz  d}t3          ||||	|
d||	z   dz  |z  |
dz  |z  d          }|S )a  
    Calls Gemini to detect bounding boxes in an image.
    
    Args:
        prompt: The prompt to send to Gemini
        image_base64: Base64-encoded image string
        
    Returns:
        List of bounding boxes in format [x1, y1, x2, y2] with absolute pixel coordinates
    g      ?)api_keyzU
    Detect the one object in the image that matches the following description: 
    z
    
    Return slight larger bounding box of the object in the following JSON format:
    {"box_2d": [ymin, xmin, ymax, xmax]}
    The box_2d should be [ymin, xmin, ymax, xmax] normalized to 0-1000. Only one object should be detected.
    zapplication/jsoni  )thinking_budgetMEDIA_RESOLUTION_MEDIUM)response_mime_typethinking_configmedia_resolutionzmodels/gemini-flash-latest)modelcontentsconfigg333333?g      @u   🖼️ Image size: xz pixelsu   💬 Gemini response: box_2d   i  r         u   📦 Converted bounding box: )r?   y)promptthoughtansweri@B )
input_costoutput_costr   )r   r   Clientr2   base64	b64decoder   openioBytesIOr   GenerateContentConfigThinkingConfigmodelsgenerate_contentusage_metadataprompt_token_countthoughts_token_countcandidates_token_countsizeprinttextjsonloadsr%   r   )r3   r4   client
image_dataimagerE   r>   response
prompt_lenthought_len
answer_leninput_cost_per_1M_tokensoutput_cost_per_1M_tokenswidthheightr	   converted_bounding_boxesr
   results                      r   detect_bounding_boxesrj   2   s    MMM\6s;;  F !,//JJrz*--..E  F (-.
 
 
 3  F }--* .  H (;J)>K(?J" #JME6	
8
8
8
8
8
8999	
28=
2
2333:hm,,L 	L"1%,u455L"1%,v566L"1%,u455L"1%,v566	  

D*B
D
DEEE 'q),DQ,GGAM&q),DQ,GGAM F - " 
 
 &3y@C[[%	14MM
 
  F Mr   )googler   google.genair   PILr   r[   r/   r.   rK   rN   dotenvr   r   r'   strr2   rj   r(   r   r   <module>rp      s                      				   				      Q Q Q Q Q Q Q Q2	2e 	2 	2 	2 	2 	2Vc V V V V V V V Vr   