Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
MODEL_ID = "zai-org/GLM-OCR"
|
| 10 |
+
|
| 11 |
+
print("Loading GLM-OCR model...")
|
| 12 |
+
|
| 13 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 14 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 15 |
+
MODEL_ID,
|
| 16 |
+
torch_dtype=torch.float32
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
@app.get("/")
|
| 20 |
+
async def root():
|
| 21 |
+
return {"status": "GLM-OCR API is running"}
|
| 22 |
+
|
| 23 |
+
@app.post("/ocr")
|
| 24 |
+
async def extract_text(file: UploadFile = File(...)):
|
| 25 |
+
try:
|
| 26 |
+
contents = await file.read()
|
| 27 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 28 |
+
|
| 29 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 30 |
+
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model.generate(**inputs, max_new_tokens=1024)
|
| 33 |
+
|
| 34 |
+
text = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 35 |
+
|
| 36 |
+
return {
|
| 37 |
+
"success": True,
|
| 38 |
+
"text": text
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return {
|
| 43 |
+
"success": False,
|
| 44 |
+
"error": str(e)
|
| 45 |
+
}
|