AdityaAjithKumar commited on
Commit
681e2b1
Β·
1 Parent(s): ebf1c12

new model

Browse files
Files changed (3) hide show
  1. Dockerfile +4 -2
  2. app.py +132 -37
  3. requirements.txt +2 -2
Dockerfile CHANGED
@@ -3,11 +3,13 @@ WORKDIR /app
3
  COPY . .
4
 
5
  RUN apt-get update && apt-get install -y --no-install-recommends \
6
- libgl1 libglib2.0-0 libgomp1 && \
7
  rm -rf /var/lib/apt/lists/*
8
 
 
 
9
  RUN pip install --no-cache-dir \
10
- fastapi uvicorn python-multipart pillow easyocr numpy
11
 
12
  EXPOSE 7860
13
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
3
  COPY . .
4
 
5
  RUN apt-get update && apt-get install -y --no-install-recommends \
6
+ libgl1 libglib2.0-0 && \
7
  rm -rf /var/lib/apt/lists/*
8
 
9
+ RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu
10
+
11
  RUN pip install --no-cache-dir \
12
+ fastapi uvicorn python-multipart pillow transformers
13
 
14
  EXPOSE 7860
15
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -1,82 +1,177 @@
1
  import logging
2
  import io
3
  import time
 
4
  from fastapi import FastAPI, File, Form, UploadFile, HTTPException
5
  from fastapi.middleware.gzip import GZipMiddleware
6
  from PIL import Image
7
- import easyocr
8
- import numpy as np
9
 
10
  logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
11
  logger = logging.getLogger(__name__)
12
 
13
- app = FastAPI(title="EasyOCR API", version="1.0.0")
14
  app.add_middleware(GZipMiddleware, minimum_size=1000)
15
 
16
- # ── Load once at startup ──────────────────────────────────────────
17
- logger.info("=== LOADING OCR ENGINE ===")
18
- reader = easyocr.Reader(["en"], gpu=False, verbose=False)
19
- logger.info("βœ… EasyOCR ready!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
- # ── Routes ───────────────────────────────────────────────────────
23
  @app.get("/")
24
  async def root():
25
- return {"message": "EasyOCR API ready! POST /ocr"}
26
 
27
 
28
  @app.get("/health")
29
  async def health():
30
- return {"status": "healthy", "engine": "EasyOCR", "gpu": False}
31
 
32
 
33
- @app.post("/ocr")
34
- async def ocr(
 
 
 
 
 
35
  image: UploadFile = File(...),
36
- min_confidence: float = Form(0.3), # filter out low-confidence detections
 
37
  ):
38
- # ── Validate ──────────────────────────────────────────────────
 
 
 
 
 
 
39
  if image.content_type not in {"image/jpeg", "image/png", "image/webp", "image/bmp", "image/tiff"}:
40
- raise HTTPException(status_code=415, detail=f"Unsupported file type: {image.content_type}")
41
 
42
  raw = await image.read()
43
- if len(raw) > 10 * 1024 * 1024: # 10MB limit
44
  raise HTTPException(status_code=413, detail="File too large. Max 10MB.")
45
 
46
- logger.info(f"πŸ“Έ OCR request | file: {image.filename} | size: {len(raw)/1024:.1f}KB")
47
-
48
- # ── Process ──────────��────────────────────────────────────────
 
 
 
 
 
 
 
 
 
49
  t0 = time.perf_counter()
 
 
50
  try:
51
  img = Image.open(io.BytesIO(raw)).convert("RGB")
52
- img_np = np.array(img)
53
-
54
- results = reader.readtext(img_np, detail=1, paragraph=False)
55
-
56
- # Filter by confidence and extract text in reading order (top→bottom, left→right)
57
- lines = [
58
- {"text": text, "confidence": round(conf, 3)}
59
- for (_, text, conf) in results
60
- if conf >= min_confidence
61
- ]
62
 
63
- full_text = " ".join(item["text"] for item in lines)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  except Exception as e:
66
- logger.error(f"❌ OCR error: {e}")
67
- raise HTTPException(status_code=500, detail="OCR processing failed.")
68
 
69
  elapsed = round((time.perf_counter() - t0) * 1000)
70
- logger.info(f"βœ… Done in {elapsed}ms | words: {len(lines)}")
71
 
72
  return {
73
- "extracted_text": full_text,
74
- "lines": lines, # individual detections with confidence scores
75
  "inference_ms": elapsed,
76
- "engine": "EasyOCR",
77
  }
78
 
79
 
80
  if __name__ == "__main__":
81
  import uvicorn
82
- uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
 
1
  import logging
2
  import io
3
  import time
4
+ import json
5
  from fastapi import FastAPI, File, Form, UploadFile, HTTPException
6
  from fastapi.middleware.gzip import GZipMiddleware
7
  from PIL import Image
8
+ import torch
9
+ from transformers import AutoProcessor, AutoModelForImageTextToText
10
 
11
  logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
12
  logger = logging.getLogger(__name__)
13
 
14
+ app = FastAPI(title="GLM-OCR Structured Extraction API", version="1.0.0")
15
  app.add_middleware(GZipMiddleware, minimum_size=1000)
16
 
17
+ # ── Built-in schemas ──────────────────────────────────────────────
18
+ SCHEMAS = {
19
+ "pan_card": {
20
+ "name": "string",
21
+ "father_name": "string",
22
+ "date_of_birth": "string",
23
+ "pan_number": "string"
24
+ },
25
+ "aadhaar_card": {
26
+ "name": "string",
27
+ "date_of_birth": "string",
28
+ "gender": "string",
29
+ "aadhaar_number": "string",
30
+ "address": "string"
31
+ },
32
+ "invoice": {
33
+ "invoice_number": "string",
34
+ "date": "string",
35
+ "vendor_name": "string",
36
+ "total_amount": "string",
37
+ "line_items": "array"
38
+ },
39
+ "business_card": {
40
+ "name": "string",
41
+ "title": "string",
42
+ "company": "string",
43
+ "email": "string",
44
+ "phone": "string",
45
+ "website": "string"
46
+ },
47
+ "receipt": {
48
+ "store_name": "string",
49
+ "date": "string",
50
+ "items": "array",
51
+ "subtotal": "string",
52
+ "tax": "string",
53
+ "total": "string"
54
+ }
55
+ }
56
+
57
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
58
+ MODEL_ID = "zai-org/GLM-OCR"
59
+
60
+ logger.info(f"=== LOADING GLM-OCR on {DEVICE} ===")
61
+ processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
62
+ model = AutoModelForImageTextToText.from_pretrained(
63
+ MODEL_ID,
64
+ torch_dtype=torch.bfloat16 if DEVICE == "cuda" else torch.float32,
65
+ trust_remote_code=True,
66
+ ).to(DEVICE)
67
+ model.eval()
68
+ logger.info("βœ… GLM-OCR ready!")
69
 
70
 
71
+ # ── Routes ────────────────────────────────────────────────────────
72
  @app.get("/")
73
  async def root():
74
+ return {"message": "GLM-OCR API ready!", "endpoints": ["GET /schemas", "POST /ocr/extract"]}
75
 
76
 
77
  @app.get("/health")
78
  async def health():
79
+ return {"status": "healthy", "model": MODEL_ID, "device": DEVICE}
80
 
81
 
82
+ @app.get("/schemas")
83
+ async def list_schemas():
84
+ return {"available": list(SCHEMAS.keys()), "schemas": SCHEMAS}
85
+
86
+
87
+ @app.post("/ocr/extract")
88
+ async def extract(
89
  image: UploadFile = File(...),
90
+ schema_name: str = Form(default="", description="Built-in: pan_card, aadhaar_card, invoice, business_card, receipt"),
91
+ custom_schema: str = Form(default="", description='Custom JSON e.g. {"company":"string","gst":"string"}'),
92
  ):
93
+ # ── Validate inputs ───────────────────────────────────────────
94
+ if not schema_name and not custom_schema:
95
+ raise HTTPException(status_code=422, detail='Provide schema_name or custom_schema.')
96
+
97
+ if schema_name and schema_name not in SCHEMAS:
98
+ raise HTTPException(status_code=422, detail=f"Unknown schema. Available: {list(SCHEMAS.keys())}")
99
+
100
  if image.content_type not in {"image/jpeg", "image/png", "image/webp", "image/bmp", "image/tiff"}:
101
+ raise HTTPException(status_code=415, detail=f"Unsupported type: {image.content_type}")
102
 
103
  raw = await image.read()
104
+ if len(raw) > 10 * 1024 * 1024:
105
  raise HTTPException(status_code=413, detail="File too large. Max 10MB.")
106
 
107
+ # ── Resolve schema ────────────────────────────────────────────
108
+ if schema_name:
109
+ schema = SCHEMAS[schema_name]
110
+ schema_label = schema_name
111
+ else:
112
+ try:
113
+ schema = json.loads(custom_schema)
114
+ schema_label = "custom"
115
+ except json.JSONDecodeError:
116
+ raise HTTPException(status_code=422, detail="custom_schema must be valid JSON.")
117
+
118
+ logger.info(f"πŸ“Έ Extract | file: {image.filename} | schema: {schema_label}")
119
  t0 = time.perf_counter()
120
+
121
+ # ── Run GLM-OCR with JSON schema prompt ───────────────────────
122
  try:
123
  img = Image.open(io.BytesIO(raw)).convert("RGB")
 
 
 
 
 
 
 
 
 
 
124
 
125
+ # GLM-OCR natively understands JSON schema extraction prompts
126
+ prompt = (
127
+ f"Extract information from this document and return ONLY a valid JSON object "
128
+ f"matching this schema: {json.dumps(schema)}. "
129
+ f"Use null for missing fields. No explanation, no markdown."
130
+ )
131
+
132
+ messages = [{"role": "user", "content": [
133
+ {"type": "image", "image": img},
134
+ {"type": "text", "text": prompt}
135
+ ]}]
136
+
137
+ text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
138
+ inputs = processor(text=text, images=[img], return_tensors="pt").to(DEVICE)
139
+
140
+ with torch.inference_mode():
141
+ outputs = model.generate(
142
+ **inputs,
143
+ max_new_tokens=1024,
144
+ do_sample=False,
145
+ )
146
+
147
+ input_len = inputs["input_ids"].shape[1]
148
+ raw_output = processor.decode(outputs[0][input_len:], skip_special_tokens=True).strip()
149
+
150
+ # ── Parse JSON from response ──────────────────────────────
151
+ try:
152
+ # Strip markdown fences if present
153
+ clean = raw_output.replace("```json", "").replace("```", "").strip()
154
+ structured = json.loads(clean)
155
+ except json.JSONDecodeError:
156
+ # Fallback: extract JSON block
157
+ start, end = clean.find("{"), clean.rfind("}") + 1
158
+ structured = json.loads(clean[start:end]) if start != -1 else {"raw": raw_output}
159
 
160
  except Exception as e:
161
+ logger.error(f"❌ Error: {e}")
162
+ raise HTTPException(status_code=500, detail="Extraction failed.")
163
 
164
  elapsed = round((time.perf_counter() - t0) * 1000)
165
+ logger.info(f"βœ… Done in {elapsed}ms")
166
 
167
  return {
168
+ "data": structured,
169
+ "schema_used": schema_label,
170
  "inference_ms": elapsed,
171
+ "model": MODEL_ID,
172
  }
173
 
174
 
175
  if __name__ == "__main__":
176
  import uvicorn
177
+ uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)
requirements.txt CHANGED
@@ -2,5 +2,5 @@ fastapi
2
  uvicorn
3
  python-multipart
4
  pillow
5
- easyocr
6
- numpy
 
2
  uvicorn
3
  python-multipart
4
  pillow
5
+ transformers
6
+ torch --index-url https://download.pytorch.org/whl/cpu