
    j              
       P   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 d dlmZ d dl	Z	d dl
mZmZmZmZ d dlmZ g dZej        j        j        ej        j        j        z   ej        j        j        ej        j        j        dZdd	Zd
ededededdf
dZd
edededdfdZdedefdZdS )    N)Path)Any)HfApi	get_tokenhf_hub_downloadlogin)models)login_to_hubpush_to_hf_hubfrom_hub!_save_model_and_config_for_hf_hubclassification	detectionrecognitionreturnc                      t                      } | %t          j        d           t          |            nt                       	 t	          j        g d           dS # t          $ r t          d          w xY w)zLogin to huggingface hubNz%Huggingface Hub token found and valid)token)gitlfsversionzLooks like you do not have git-lfs installed, please install.                       You can install from https://git-lfs.github.com/.                       Then run `git lfs install` (you only have to do this once).)r   logginginfor   
subprocesscallFileNotFoundErrorOSError)access_tokens    [/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/models/factory/hub.pyr
   r
   $   s    ;;L<===L!!!!!
11122222 
 
 
S
 
 	

s   A A7modelsave_dirarchtaskc                 4   t          |          }|dz  }t          j        |                                 |           |dz  }| j        }||d<   ||d<   |                    d          5 }t          j        ||dd           d	d	d	           d	S # 1 swxY w Y   d	S )
zSave model and config to disk for pushing to huggingface hub

    Args:
        model: PyTorch model to be saved
        save_dir: directory to save model and config
        arch: architecture name
        task: task name
    pytorch_model.binconfig.jsonr"   r#   w   Findentensure_asciiN)r   torchsave
state_dictcfgopenjsondump)	r    r!   r"   r#   save_directoryweights_pathconfig_pathmodel_configfs	            r   r   r   7   s     (^^N!$77L	Ju!!<000 =0K 9LLL			#		 A!	,!%@@@@A A A A A A A A A A A A A A A A A As   'BBB
model_namec           	      B   |                     dd          }|                     dd          }||t          d          |dvrt          d          t          j        d| d          }|C|j        }|t          j        d	t          j        t          |          d
d                     z  }|t          |         vrt          d| d| dt                     d| d}t                      }|
                    |t                      d           t          j                    5 }	t          | |	||           t          |	          dz  }
|
                    |           |                    |	||t                                 ddd           dS # 1 swxY w Y   dS )a'  Save model and its configuration on HF hub

    >>> from doctr.models import login_to_hub, push_to_hf_hub
    >>> from doctr.models.recognition import crnn_mobilenet_v3_small
    >>> login_to_hub()
    >>> model = crnn_mobilenet_v3_small(pretrained=True)
    >>> push_to_hf_hub(model, 'my-model', 'recognition', arch='crnn_mobilenet_v3_small')

    Args:
        model: PyTorch model to be saved
        model_name: name of the model which is also the repository name
        task: task name
        **kwargs: keyword arguments for push_to_hf_hub
    
run_configNr"   z$run_config or arch must be specifiedr   z:task must be one of classification, detection, recognitiona  

    language: en


    <p align="center">
    <img src="https://doctr-static.mindee.com/models?id=v0.3.1/Logo_doctr.gif&src=0" width="60%">
    </p>

    **Optical Character Recognition made seamless & accessible to anyone, powered by PyTorch**

    ## Task: a_  

    https://github.com/mindee/doctr

    ### Example usage:

    ```python
    >>> from doctr.io import DocumentFile
    >>> from doctr.models import ocr_predictor, from_hub

    >>> img = DocumentFile.from_images(['<image_path>'])
    >>> # Load your model from the hub
    >>> model = from_hub('mindee/my-model')

    >>> # Pass it to the predictor
    >>> # If your model is a recognition model:
    >>> predictor = ocr_predictor(det_arch='db_mobilenet_v3_large',
    >>>                           reco_arch=model,
    >>>                           pretrained=True)

    >>> # If your model is a detection model:
    >>> predictor = ocr_predictor(det_arch=model,
    >>>                           reco_arch='crnn_mobilenet_v3_small',
    >>>                           pretrained=True)

    >>> # Get your predictions
    >>> res = predictor(img)
    ```
    z9### Run Configuration
                                  
r(   Fr)   zArchitecture: z for task: z> not found.                         
Available architectures: zAdd z model)r   exist_ok)r"   r#   z	README.md)folder_pathrepo_idcommit_messager   )get
ValueErrortextwrapdedentr"   r1   dumpsvarsAVAILABLE_ARCHSr   create_repor   tempfileTemporaryDirectoryr   r   
write_textupload_folder)r    r8   r#   kwargsr:   r"   readmer>   apitmp_dirreadme_paths              r   r   r   O   sK    L$//J::fd##Ddl?@@@AAAUVVV _'	 '	 '	 '	) )FX (/d%)ZZ0@0@Y^%_%_%_d d
 
 	

 ?4(((GT G Gd G G5DG G
 
 	

 /J...N ''COOJikkEOBBB 
	$	&	& 
')%t$OOOO7mmk1v&&& 	)++	 	 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   'A FFFr=   rK   c                    t          t          | fddi|d          5 }t          j        |          }ddd           n# 1 swxY w Y   |d         }|d         }|                    d           |                    d           |dk    r/t          j        j        |         d|d	         |d
                   }n[|dk    r!t          j        j        |         d          }n4|dk    r.t          j	        j        |         d|d         |d                   }||_
        t          | fddi|}|                    |           |S )aX  Instantiate & load a pretrained model from HF hub.

    >>> from doctr.models import from_hub
    >>> model = from_hub("mindee/fasterrcnn_mobilenet_v3_large_fpn")

    Args:
        repo_id: HuggingFace model hub repo
        kwargs: kwargs of `hf_hub_download` or `snapshot_download`

    Returns:
        Model loaded with the checkpoint
    filenamer&   rbNr"   r#   r   Fclassesnum_classes)
pretrainedrS   rT   r   )rU   r   input_shapevocab)rU   rV   rW   r%   )r0   r   r1   loadpopr	   r   __dict__r   r   r/   from_pretrained)r=   rK   r7   r/   r"   r#   r    weightss           r   r   r      s    
ogHHHHH$	O	O STill               v;Dv;DGGFOOOGGFOOO%.t4c)n#mBT
 
 
 
		 )$/5AAA			"+D1UPSTaPbjmnujvwww EIgNN0CNvNNG	'"""Ls   ?AA)r   N)r1   r   r   rG   rA   pathlibr   typingr   r,   huggingface_hubr   r   r   r   doctrr	   __all__r   zooARCHSORIENTATION_ARCHSr   r   rE   r
   strr   r   r        r   <module>rh      s                                        
]
]
] +/58M8Q8cc!%+%)/ 
 
 
 
&AS AC As ARU AZ^ A A A A0c
# c
3 c
c c
 c
 c
 c
 c
L%c %S % % % % % %rg   