Instructions to use defford/GLM-OCR with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use defford/GLM-OCR with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # 'pip install "transformers<5.0.0' from transformers import pipeline pipe = pipeline("image-to-text", model="defford/GLM-OCR")# Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("defford/GLM-OCR") model = AutoModelForMultimodalLM.from_pretrained("defford/GLM-OCR", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update handler.py
Browse files- handler.py +20 -9
handler.py
CHANGED
|
@@ -6,7 +6,8 @@ import base64
|
|
| 6 |
|
| 7 |
class EndpointHandler():
|
| 8 |
def __init__(self, path=""):
|
| 9 |
-
#
|
|
|
|
| 10 |
self.processor = AutoProcessor.from_pretrained(path, trust_remote_code=True)
|
| 11 |
self.model = AutoModelForVision2Seq.from_pretrained(
|
| 12 |
path,
|
|
@@ -17,17 +18,27 @@ class EndpointHandler():
|
|
| 17 |
self.model.eval()
|
| 18 |
|
| 19 |
def __call__(self, data):
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
image_data = base64.b64decode(inputs)
|
| 23 |
-
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
with torch.no_grad():
|
| 30 |
-
generated_ids = self.model.generate(**
|
| 31 |
|
| 32 |
result = self.processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
|
|
| 33 |
return [{"generated_text": result}]
|
|
|
|
| 6 |
|
| 7 |
class EndpointHandler():
|
| 8 |
def __init__(self, path=""):
|
| 9 |
+
# We MUST use trust_remote_code=True because the architecture
|
| 10 |
+
# is defined in the files you downloaded, not in the library itself.
|
| 11 |
self.processor = AutoProcessor.from_pretrained(path, trust_remote_code=True)
|
| 12 |
self.model = AutoModelForVision2Seq.from_pretrained(
|
| 13 |
path,
|
|
|
|
| 18 |
self.model.eval()
|
| 19 |
|
| 20 |
def __call__(self, data):
|
| 21 |
+
# Handle the input format from Google Sheets
|
| 22 |
+
inputs_data = data.pop("inputs", data)
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# If the data comes in as a string (base64)
|
| 25 |
+
if isinstance(inputs_data, str):
|
| 26 |
+
image_bytes = base64.b64decode(inputs_data)
|
| 27 |
+
else:
|
| 28 |
+
# Handle direct bytes if necessary
|
| 29 |
+
image_bytes = inputs_data
|
| 30 |
+
|
| 31 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 32 |
+
|
| 33 |
+
# The prompt that tells the AI what to look for
|
| 34 |
+
prompt = "Extract all line items. Return a JSON array of objects with: date, vendor, description, qty, price, total."
|
| 35 |
+
|
| 36 |
+
# Process the image and text
|
| 37 |
+
model_inputs = self.processor(text=prompt, images=image, return_tensors="pt").to(self.model.device)
|
| 38 |
|
| 39 |
with torch.no_grad():
|
| 40 |
+
generated_ids = self.model.generate(**model_inputs, max_new_tokens=1024)
|
| 41 |
|
| 42 |
result = self.processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 43 |
+
|
| 44 |
return [{"generated_text": result}]
|