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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -5,35 +5,31 @@ from PIL import Image
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:
@@ -87,10 +83,10 @@ def build_interface():
87
  fn=recognize_handwritten_text,
88
  inputs=gr.Image(type="numpy", label="Upload Handwritten Image"),
89
  outputs="text",
90
- title="✍️ Handwritten Text Recognition",
91
  description="πŸ“· Upload a handwritten image. Uses PaddleOCR (detection) + TrOCR (recognition).",
92
  )
93
 
94
- if __name__ == "__main__":
95
  iface = build_interface()
96
- iface.launch(enable_queue=True)
 
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:
 
83
  fn=recognize_handwritten_text,
84
  inputs=gr.Image(type="numpy", label="Upload Handwritten Image"),
85
  outputs="text",
86
+ title="✍ Handwritten Text Recognition",
87
  description="πŸ“· Upload a handwritten image. Uses PaddleOCR (detection) + TrOCR (recognition).",
88
  )
89
 
90
+ if _name_ == "_main_":
91
  iface = build_interface()
92
+ iface.launch()