Spaces:
Sleeping
Sleeping
File size: 6,571 Bytes
0533780 | 1 2 3 4 5 6 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | """
main.py β FastAPI server for zai-org/GLM-OCR
Endpoints:
GET / β Serves the frontend HTML
GET /health β Liveness probe + model info
POST /ocr β Run OCR on uploaded image
GET /metrics β Session-level stats
"""
import logging
import time
from contextlib import asynccontextmanager
from pathlib import Path
import uvicorn
from fastapi import FastAPI, File, Form, HTTPException, UploadFile, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel
from typing import Annotated
from ocr_engine import engine, OcrResult, OcrMode
# ββ Logging βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)-8s | %(name)s β %(message)s",
datefmt="%H:%M:%S",
)
logger = logging.getLogger(__name__)
# ββ Session metrics βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class SessionMetrics:
def __init__(self):
self.total_requests = 0
self.total_words = 0
self.total_chars = 0
self.total_ms = 0.0
self.errors = 0
self.started_at = time.time()
def record(self, result: OcrResult):
self.total_requests += 1
self.total_words += result.word_count
self.total_chars += result.char_count
self.total_ms += result.latency_ms
def to_dict(self) -> dict:
avg = self.total_ms / self.total_requests if self.total_requests else 0
return {
"total_requests": self.total_requests,
"total_words_extracted": self.total_words,
"total_chars_extracted": self.total_chars,
"avg_latency_ms": round(avg, 1),
"error_count": self.errors,
"uptime_seconds": round(time.time() - self.started_at, 1),
}
metrics = SessionMetrics()
# ββ Lifespan βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("π Starting up β loading GLM-OCR model β¦")
engine.load()
logger.info("β
Model ready.")
yield
logger.info("π Shutting down β¦")
engine.unload()
# ββ App ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app = FastAPI(
title="GLM-OCR API",
description="Self-hosted OCR backend powered by zai-org/GLM-OCR",
version="1.0.0",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["GET", "POST"],
allow_headers=["*"],
)
# ββ Schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class OcrResponse(BaseModel):
success: bool
text: str
word_count: int
char_count: int
latency_ms: float
mode: str
model_id: str
device: str
# ββ Routes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/", include_in_schema=False)
async def serve_frontend():
frontend = Path(__file__).parent / "frontend" / "index.html"
if not frontend.exists():
return JSONResponse({"message": "Frontend not found."}, 404)
return FileResponse(str(frontend))
@app.get("/health")
async def health():
return {
"status": "ok" if engine.loaded else "loading",
"model": engine.info,
}
@app.post("/ocr", response_model=OcrResponse)
async def run_ocr(
file: Annotated[UploadFile, File(description="Image file (PNG, JPG, WEBP, BMP, TIFF)")],
mode: Annotated[OcrMode, Form(description="'recognize' for plain text Β· 'parse' for structured markdown")] = "recognize",
):
"""
Run GLM-OCR on an uploaded image.
**mode options:**
- `recognize` β extracts raw text, preserves layout (default)
- `parse` β returns structured markdown (headers, tables, lists)
"""
allowed = {"image/png", "image/jpeg", "image/webp", "image/gif", "image/bmp", "image/tiff"}
if file.content_type and file.content_type not in allowed:
raise HTTPException(status_code=415, detail=f"Unsupported file type: {file.content_type}")
image_bytes = await file.read()
if not image_bytes:
raise HTTPException(status_code=400, detail="Empty file.")
if len(image_bytes) > 20 * 1024 * 1024:
raise HTTPException(status_code=413, detail="File too large. Max 20 MB.")
logger.info(f"OCR | file={file.filename} size={len(image_bytes)/1024:.1f}KB mode={mode}")
try:
result = engine.run(image_bytes, mode=mode)
except ValueError as e:
metrics.errors += 1
raise HTTPException(status_code=422, detail=str(e))
except Exception as e:
metrics.errors += 1
logger.exception("Inference error")
raise HTTPException(status_code=500, detail=f"Inference failed: {e}")
metrics.record(result)
logger.info(f"Done | {result.word_count} words | {result.latency_ms:.0f}ms")
return OcrResponse(
success = True,
text = result.text,
word_count = result.word_count,
char_count = result.char_count,
latency_ms = result.latency_ms,
mode = result.mode,
model_id = result.model_id,
device = result.device,
)
@app.get("/metrics")
async def get_metrics():
return metrics.to_dict()
@app.exception_handler(Exception)
async def global_handler(request: Request, exc: Exception):
logger.exception(f"Unhandled: {request.url}")
return JSONResponse(status_code=500, content={"detail": "Internal server error"})
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=False) |