o
    i
                  
   @   s   d dl Z d dlZd dlmZ d dlmZ ee jd Z	e	d d Z
e	d d Ze	d d Ze d	d
e dddZdedefddZdedededeeef fddZdedededefddZdS )    N)Path)run_vision_json   promptszdetect_type.v1.mdzextractor.pos.v1.2.mdzextractor.corporate.v1.2.mdOPENAI_MODEL_MINIz
gpt-5-miniOPENAI_MODEL_THINKINGzgpt-5-thinking)minithinkingpathreturnc                 C   s:   t | ddd}| W  d    S 1 sw   Y  d S )Nrzutf-8)encoding)openread)r
   f r   A/var/www/html/flask_server/apps/wp_invoices/services/extractor.py_load   s   $r   
file_bytesfilenamemimetypec                 C   s   t t}td ttd || ||d}td t|tr!t|}t|	dd
 }t|	dp2d}|d	vr:d
}td| d|  ||fS )Nu<   🟡 detect_type: llamando a run_vision_json con modelo minir   modelpromptr   r   r   u$   🟡 detect_type: respuesta recibidainvoice_type 
confidence        )POS	CORPORATEr   u   🟡 detect_type: tipo=z, confidence=)r   PROMPT_DETECTprintr   	MODEL_MAP
isinstancestrjsonloadsgetupperfloat)r   r   r   r   resitypeconfr   r   r   detect_type   s$   

r-   dataenginec              
   C   s,  t d z
t| ||\}}W n ty+ } zt d|  d\}}W Y d }~nd }~ww |dkr3t}nt}t|}t|td }	t d|	 d| d|j  t	|	|| ||d	}
t d
 t
|
trft|
}
t
|
dtrt|
d dpwd}||
d d< t|||
d d< n||d|
d< t d |
S )Nu   🟢 run_extraction: iniciou   🔴 detect_type falló: )r   r   r   r   u#   🟢 run_extraction: usando modelo=z, tipo=z	, prompt=r   u3   🟢 run_extraction: respuesta extracción recibidar   r   r   verbatim)r0   r   u   🟢 run_extraction: fin OK)r!   r-   	ExceptionPROMPT_CORP
PROMPT_POSr   r"   r'   namer   r#   r$   r%   r&   dictr)   max)r.   r   r   r/   r+   r,   eprompt_pathr   r   r*   existing_confr   r   r   run_extraction0   s>   

r:   )osr%   pathlibr   shared.openai_managerr   __file__resolveparentsBASEr    r3   r2   getenvr"   r$   r   bytestupler)   r-   r:   r   r   r   r   <module>   s    

"