imperiusrex commited on
Commit
2db835f
Β·
verified Β·
1 Parent(s): 0ae04b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -5,31 +5,35 @@ from PIL import Image
5
  import numpy as np
6
  import cv2
7
  from paddleocr import TextDetection
8
- from spaces import GPU # βœ… Required for ZeroGPU
9
 
10
  MODEL_HUB_ID = "imperiusrex/Handwritten_model"
11
 
12
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
-
14
- print("πŸ”„ Loading models...")
15
 
 
16
  processor = TrOCRProcessor.from_pretrained(MODEL_HUB_ID)
17
- model = VisionEncoderDecoderModel.from_pretrained(MODEL_HUB_ID)
18
- model.to(device)
19
  model.eval()
20
 
21
  ocr_det_model = TextDetection(model_name="PP-OCRv5_server_det")
 
22
 
23
- print("βœ… Models loaded successfully.")
24
-
25
- @GPU # βœ… This tells Hugging Face this function needs the GPU (H200)
26
  def recognize_handwritten_text(image_input):
27
  if image_input is None:
28
  return "Please upload an image."
29
 
 
 
 
 
 
30
  image_pil = Image.fromarray(image_input).convert("RGB")
31
 
32
- detection_results = ocr_det_model.predict(image_input, batch_size=1)
 
33
 
34
  detected_polys = []
35
  for res in detection_results:
@@ -89,4 +93,4 @@ def build_interface():
89
 
90
  if __name__ == "__main__":
91
  iface = build_interface()
92
- iface.launch()
 
5
  import numpy as np
6
  import cv2
7
  from paddleocr import TextDetection
8
+ from spaces import GPU # βœ… For ZeroGPU
9
 
10
  MODEL_HUB_ID = "imperiusrex/Handwritten_model"
11
 
12
+ # --- Load models on CPU first ---
13
+ device_cpu = torch.device("cpu")
 
14
 
15
+ print("πŸ”„ Loading models on CPU...")
16
  processor = TrOCRProcessor.from_pretrained(MODEL_HUB_ID)
17
+ model = VisionEncoderDecoderModel.from_pretrained(MODEL_HUB_ID).to(device_cpu)
 
18
  model.eval()
19
 
20
  ocr_det_model = TextDetection(model_name="PP-OCRv5_server_det")
21
+ print("βœ… Models loaded (CPU mode). GPU will be used only during inference.")
22
 
23
+ @GPU # βœ… Runs on GPU when called
 
 
24
  def recognize_handwritten_text(image_input):
25
  if image_input is None:
26
  return "Please upload an image."
27
 
28
+ # Move model to GPU here (only when needed)
29
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
30
+ model.to(device)
31
+ torch.zeros(1).to(device) # CUDA warmup
32
+
33
  image_pil = Image.fromarray(image_input).convert("RGB")
34
 
35
+ # Run detection on original image
36
+ detection_results = ocr_det_model.predict(np.array(image_pil), batch_size=1)
37
 
38
  detected_polys = []
39
  for res in detection_results:
 
93
 
94
  if __name__ == "__main__":
95
  iface = build_interface()
96
+ iface.launch(enable_queue=True)