Spaces:
Running on Zero
Running on Zero
Commit ·
e1b1045
1
Parent(s): bf47208
Refactor OCR model initialization and prediction handling for improved error reporting and message formatting
Browse files5 unmodified lines67891011121314151642 unmodified lines5960616263646566676869707172737475767778798081828384858687888990919293945 unmodified linesfrom transformers import AutoProcessor, AutoModelForImageTextToText, pipelineimport spaces# --- Global Model and Processor (initialize as None for lazy loading) ---HF_PROCESSOR = NoneHF_MODEL = NoneHF_PIPE = NoneMODEL_LOAD_ERROR_MSG = None # To store any error message from loading# --- Helper Functions ---42 unmodified linesexcept Exception as e:return f"An unexpected error occurred during XML parsing: {e}"@spaces.GPU # Ensures GPU is available for model loading (on first call) and inferencedef predict(pil_image):"""Performs OCR prediction using the Hugging Face model, with lazy loading."""global HF_PROCESSOR, HF_MODEL, HF_PIPE, MODEL_LOAD_ERROR_MSGif HF_PIPE is None and MODEL_LOAD_ERROR_MSG is None:try:print("Attempting to load Hugging Face model and processor within @spaces.GPU context...")HF_PROCESSOR = AutoProcessor.from_pretrained("reducto/RolmOCR")HF_MODEL = AutoModelForImageTextToText.from_pretrained("reducto/RolmOCR",torch_dtype=torch.bfloat16,device_map="auto" # Should utilize ZeroGPU correctly here)HF_PIPE = pipeline("image-text-to-text", model=HF_MODEL, processor=HF_PROCESSOR)print("Hugging Face OCR model loaded successfully.")except Exception as e:MODEL_LOAD_ERROR_MSG = f"Error loading Hugging Face model: {str(e)}"print(MODEL_LOAD_ERROR_MSG)# HF_PIPE remains None, error message is storedif HF_PIPE is None:error_to_report = MODEL_LOAD_ERROR_MSG if MODEL_LOAD_ERROR_MSG else "OCR model could not be initialized."raise RuntimeError(error_to_report)# Proceed with inference if pipe is availablereturn HF_PIPE(pil_image,prompt="Return the plain text representation of this document as if you were reading it naturally.\n",)def run_hf_ocr(image_path):"""5 unmodified lines67891011121314151617181920212223242542 unmodified lines686970717273747576777879808182838485868788899091929394955 unmodified linesfrom transformers import AutoProcessor, AutoModelForImageTextToText, pipelineimport spaces# --- Global Model and Processor ---HF_PROCESSOR = NoneHF_MODEL = NoneHF_PIPE = NoneMODEL_LOAD_ERROR_MSG = NoneHF_PROCESSOR = AutoProcessor.from_pretrained("reducto/RolmOCR")HF_MODEL = AutoModelForImageTextToText.from_pretrained("reducto/RolmOCR",torch_dtype=torch.bfloat16,device_map="auto")HF_PIPE = pipeline("image-text-to-text", model=HF_MODEL, processor=HF_PROCESSOR)# --- Helper Functions ---42 unmodified linesexcept Exception as e:return f"An unexpected error occurred during XML parsing: {e}"@spaces.GPUdef predict(pil_image):"""Performs OCR prediction using the Hugging Face model."""global HF_PIPE, MODEL_LOAD_ERROR_MSGif HF_PIPE is None:error_to_report = MODEL_LOAD_ERROR_MSG if MODEL_LOAD_ERROR_MSG else "OCR model could not be initialized."raise RuntimeError(error_to_report)# Format the message in the expected structuremessages = [{"role": "user","content": [{"type": "image", "image": pil_image},{"type": "text", "text": "Return the plain text representation of this document as if you were reading it naturally.\n"}]}]# Use the pipeline with the properly formatted messagesreturn HF_PIPE(messages)def run_hf_ocr(image_path):"""
app.py+27-26