Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update ocr_cpu.py
Browse files- ocr_cpu.py +98 -97
    	
        ocr_cpu.py
    CHANGED
    
    | @@ -1,97 +1,98 @@ | |
| 1 | 
            -
            import os
         | 
| 2 | 
            -
            from transformers import AutoModel, AutoTokenizer
         | 
| 3 | 
            -
            import torch
         | 
| 4 | 
            -
             | 
| 5 | 
            -
            # Load model and tokenizer
         | 
| 6 | 
            -
            model_name = "ucaslcl/GOT-OCR2_0"
         | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
                 | 
| 15 | 
            -
                 | 
| 16 | 
            -
                 | 
| 17 | 
            -
                 | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
                 | 
| 31 | 
            -
             | 
| 32 | 
            -
                     | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
                     | 
| 37 | 
            -
                     | 
| 38 | 
            -
                     | 
| 39 | 
            -
                     | 
| 40 | 
            -
                     | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
                     | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
                             | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
                     | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
                             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
                     | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
                                 | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
                     | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
                             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
                     | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
|  | 
|  | |
| 1 | 
            +
            import os
         | 
| 2 | 
            +
            from transformers import AutoModel, AutoTokenizer
         | 
| 3 | 
            +
            import torch
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # Load model and tokenizer
         | 
| 6 | 
            +
            # model_name = "ucaslcl/GOT-OCR2_0"
         | 
| 7 | 
            +
            model_name = "srimanth-d/GOT_CPU"
         | 
| 8 | 
            +
            tokenizer = AutoTokenizer.from_pretrained(
         | 
| 9 | 
            +
                model_name, trust_remote_code=True, return_tensors='pt'
         | 
| 10 | 
            +
            )
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            # Load the model
         | 
| 13 | 
            +
            model = AutoModel.from_pretrained(
         | 
| 14 | 
            +
                model_name,
         | 
| 15 | 
            +
                trust_remote_code=True,
         | 
| 16 | 
            +
                low_cpu_mem_usage=True,
         | 
| 17 | 
            +
                use_safetensors=True,
         | 
| 18 | 
            +
                pad_token_id=tokenizer.eos_token_id,
         | 
| 19 | 
            +
            )
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            # Ensure the model is in evaluation mode and loaded on CPU
         | 
| 22 | 
            +
            device = torch.device("cpu")
         | 
| 23 | 
            +
            dtype = torch.float32  # Use float32 on CPU
         | 
| 24 | 
            +
            model = model.eval()
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            # OCR function
         | 
| 27 | 
            +
             | 
| 28 | 
            +
             | 
| 29 | 
            +
            def extract_text_got(uploaded_file):
         | 
| 30 | 
            +
                """Use GOT-OCR2.0 model to extract text from the uploaded image."""
         | 
| 31 | 
            +
                try:
         | 
| 32 | 
            +
                    temp_file_path = 'temp_image.jpg'
         | 
| 33 | 
            +
                    with open(temp_file_path, 'wb') as temp_file:
         | 
| 34 | 
            +
                        temp_file.write(uploaded_file.read())  # Save file
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                    # OCR attempts
         | 
| 37 | 
            +
                    ocr_types = ['ocr', 'format']
         | 
| 38 | 
            +
                    fine_grained_options = ['ocr', 'format']
         | 
| 39 | 
            +
                    color_options = ['red', 'green', 'blue']
         | 
| 40 | 
            +
                    box = [10, 10, 100, 100]  # Example box for demonstration
         | 
| 41 | 
            +
                    multi_crop_types = ['ocr', 'format']
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                    results = []
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                    # Run the model without autocast (not necessary for CPU)
         | 
| 46 | 
            +
                    for ocr_type in ocr_types:
         | 
| 47 | 
            +
                        with torch.no_grad():
         | 
| 48 | 
            +
                            outputs = model.chat(
         | 
| 49 | 
            +
                                tokenizer, temp_file_path, ocr_type=ocr_type
         | 
| 50 | 
            +
                            )
         | 
| 51 | 
            +
                            if isinstance(outputs, list) and outputs[0].strip():
         | 
| 52 | 
            +
                                return outputs[0].strip()  # Return if successful
         | 
| 53 | 
            +
                            results.append(outputs[0].strip() if outputs else "No result")
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                    # Try FINE-GRAINED OCR with box options
         | 
| 56 | 
            +
                    for ocr_type in fine_grained_options:
         | 
| 57 | 
            +
                        with torch.no_grad():
         | 
| 58 | 
            +
                            outputs = model.chat(
         | 
| 59 | 
            +
                                tokenizer, temp_file_path, ocr_type=ocr_type, ocr_box=box
         | 
| 60 | 
            +
                            )
         | 
| 61 | 
            +
                            if isinstance(outputs, list) and outputs[0].strip():
         | 
| 62 | 
            +
                                return outputs[0].strip()  # Return if successful
         | 
| 63 | 
            +
                            results.append(outputs[0].strip() if outputs else "No result")
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                    # Try FINE-GRAINED OCR with color options
         | 
| 66 | 
            +
                    for ocr_type in fine_grained_options:
         | 
| 67 | 
            +
                        for color in color_options:
         | 
| 68 | 
            +
                            with torch.no_grad():
         | 
| 69 | 
            +
                                outputs = model.chat(
         | 
| 70 | 
            +
                                    tokenizer, temp_file_path, ocr_type=ocr_type, ocr_color=color
         | 
| 71 | 
            +
                                )
         | 
| 72 | 
            +
                                if isinstance(outputs, list) and outputs[0].strip():
         | 
| 73 | 
            +
                                    return outputs[0].strip()  # Return if successful
         | 
| 74 | 
            +
                                results.append(outputs[0].strip()
         | 
| 75 | 
            +
                                               if outputs else "No result")
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                    # Try MULTI-CROP OCR
         | 
| 78 | 
            +
                    for ocr_type in multi_crop_types:
         | 
| 79 | 
            +
                        with torch.no_grad():
         | 
| 80 | 
            +
                            outputs = model.chat_crop(
         | 
| 81 | 
            +
                                tokenizer, temp_file_path, ocr_type=ocr_type
         | 
| 82 | 
            +
                            )
         | 
| 83 | 
            +
                            if isinstance(outputs, list) and outputs[0].strip():
         | 
| 84 | 
            +
                                return outputs[0].strip()  # Return if successful
         | 
| 85 | 
            +
                            results.append(outputs[0].strip() if outputs else "No result")
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                    # If no text was extracted
         | 
| 88 | 
            +
                    if all(not text for text in results):
         | 
| 89 | 
            +
                        return "No text extracted."
         | 
| 90 | 
            +
                    else:
         | 
| 91 | 
            +
                        return "\n".join(results)
         | 
| 92 | 
            +
             | 
| 93 | 
            +
                except Exception as e:
         | 
| 94 | 
            +
                    return f"Error during text extraction: {str(e)}"
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                finally:
         | 
| 97 | 
            +
                    if os.path.exists(temp_file_path):
         | 
| 98 | 
            +
                        os.remove(temp_file_path)
         | 
