rehan953 commited on
Commit
481e07c
Β·
verified Β·
1 Parent(s): 18a0aa6

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -45,19 +45,24 @@ with open(formatter_path, "w") as f:
45
 
46
  print("βœ… result_formatter.py fixed")
47
 
48
- # ── STEP 3: Load model at startup ────────────────────────────
49
- from transformers import AutoProcessor, GlmOcrForConditionalGeneration
50
-
51
- print("Loading model at startup...")
52
- processor = AutoProcessor.from_pretrained("zai-org/GLM-OCR")
53
- model = GlmOcrForConditionalGeneration.from_pretrained(
54
- "zai-org/GLM-OCR",
55
- torch_dtype=torch.bfloat16,
56
- device_map="auto",
57
- )
58
- print(f"βœ… Model ready on {next(model.parameters()).device}")
59
-
60
- ABANDON = set(config["pipeline"]["result_formatter"]["abandon"])
 
 
 
 
 
61
 
62
  # ── STEP 4: OCR one image ─────────────────────────────────────
63
  def ocr_image(img_path):
@@ -102,6 +107,8 @@ def run_ocr(uploaded_file):
102
  return "Please upload a file.", "No regions detected."
103
 
104
  try:
 
 
105
  path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
106
 
107
  if path.lower().endswith(".pdf"):
 
45
 
46
  print("βœ… result_formatter.py fixed")
47
 
48
+ # ── STEP 3: Lazy model load ───────────────────────────────────
49
+ processor = None
50
+ model = None
51
+ ABANDON = set(config["pipeline"]["result_formatter"]["abandon"])
52
+
53
+ def load_model():
54
+ global processor, model
55
+ if model is not None:
56
+ return
57
+ from transformers import AutoProcessor, GlmOcrForConditionalGeneration
58
+ print("Loading model...")
59
+ processor = AutoProcessor.from_pretrained("zai-org/GLM-OCR")
60
+ model = GlmOcrForConditionalGeneration.from_pretrained(
61
+ "zai-org/GLM-OCR",
62
+ torch_dtype=torch.bfloat16,
63
+ device_map="auto",
64
+ )
65
+ print(f"βœ… Model ready on {next(model.parameters()).device}")
66
 
67
  # ── STEP 4: OCR one image ─────────────────────────────────────
68
  def ocr_image(img_path):
 
107
  return "Please upload a file.", "No regions detected."
108
 
109
  try:
110
+ load_model()
111
+
112
  path = uploaded_file.name if hasattr(uploaded_file, "name") else str(uploaded_file)
113
 
114
  if path.lower().endswith(".pdf"):