
    4gq                     x    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Z	de
de
fdZde
de
fd	Zde
fd
Zy)    )HTTPException)OpenAINc                    t        t        j                  d            }d}d}||k  r,	 |j                  j                  j                  ||| d      }	|	S t        d|z         # t        $ rR}
|d|z  z  }t        dt        |
       d	| d
       t        j                  |       |dz  }t        |
      }Y d }
~
nd }
~
ww xY w||k  rs)NOPENAI_API_KEY)api_keyr    *   )modeltemperaturemessagesseed   zEncountered z error. Retrying in z seconds...   z3Max retries reached due to errors. Last error was: )r   osgetenvchatcompletionscreate	Exceptionprintstrtimesleep)messages_to_air   
max_tokensr
   max_retries
base_delayclientretries
last_errorresponsee	wait_times               $/opt/aibudy/backend/extract_tools.py
chatOpenAIr%      s    BII&678FGJ
K
	 {{..55))	 6 H O IJV
WW  	 "a7l3ILQ(<YK{STJJy!qLGQJ	  K
s   *A# #	B>,AB99B>filepathreturnc                    K   d}	 t        j                  |       }t        |j                        D ]  }||   }||j	                         z  } 	 |S # t
        $ r }t        d|        t        dd      d }~ww xY ww)Nr   z Error extracting text from PDF:   z5An error occurred while extracting text from the PDF.status_codedetail)fitzopenrange
page_countget_textr   r   r   )r&   textpdf_documentpage_numpager"   s         r$   extract_text_from_pdfr6       s     Dmyy*l556 	$H)DDMMO#D	$ K	  m0454kllms)   A<AA A<	A9A44A99A<	file_pathc                 V   K   t        j                  |       j                  d      }|S w)Nutf-8)textractprocessdecode)r7   r2   s     r$   extract_text_from_filer=   .   s&     I&--g6DKs   ')c                 \  K   	 d } ||       }ddd|dddgdg}t        |d	d
d      }|j                  d   j                  j                  }|j	                  dd      j	                  dd      }t        d|        |S # t
        $ r }t        d|        t        dd      d }~ww xY ww)Nc                     t        | d      5 }t        j                  |j                               j	                  d      cd d d        S # 1 sw Y   y xY w)Nrbr9   )r.   base64	b64encodereadr<   )
image_path
image_files     r$   encode_imagez-extract_text_from_image.<locals>.encode_image5   sG    j$' K:''
(9:AA'JK K Ks   2A		Auserz<You are an AI assistant that analyzes the content of photos.z.Provide a confident description of the video:
i   )imageresizezA
Return only the description without any additional explanation.
)rolecontentgffffff?   gpt-4o)r
   r   'r   "z4Error extracting text from image with pytesseract : r)   z7An error occurred while extracting text from the image.r*   zExtracted text: )r%   choicesmessagerK   replacer   r   r   )r&   rF   image_to_openaiPROMPT_MESSAGESresultextracted_textr"   s          r$   extract_text_from_imagerW   2   s     o	K 'x0 RE-=Y	

 OS#XF*22::'//S9AA#sK 
^,
-.  oDQCHI4mnnos)   B,A,B  0B, 	B)	B$$B))B,)NrM      r   )fastapir   openair   r-   rA   r   r   r:   r%   r   r6   r=   rW        r$   <module>r]      sL    !  ' ' 'X6# # C C s r\   