Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,13 @@ import os
|
|
6 |
import requests
|
7 |
import torch
|
8 |
import huggingface_hub
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
|
17 |
# Load the model file
|
@@ -19,25 +20,28 @@ model_path = "yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
|
|
19 |
if not os.path.exists(model_path):
|
20 |
# Download the model file if it doesn't exist
|
21 |
model_url = "https://huggingface.co/DILHTWD/documentlayoutsegmentation_YOLOv8_ondoclaynet/resolve/main/yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
# Load the document segmentation model
|
27 |
docseg_model = YOLO(model_path)
|
28 |
|
29 |
-
if zero_gpu_is_available:
|
30 |
-
docseg_model.to(accelerator.device) # Put the model on the accelerator's device.
|
31 |
|
|
|
32 |
|
33 |
def process_image(image):
|
34 |
try:
|
35 |
# Convert image to the format YOLO model expects
|
36 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
image = torch.from_numpy(image).to(accelerator.device)
|
41 |
|
42 |
results = docseg_model.predict(image)
|
43 |
result = results[0] # Get the first (and usually only) result
|
|
|
6 |
import requests
|
7 |
import torch
|
8 |
import huggingface_hub
|
9 |
+
from accelerate import Accelerator
|
10 |
+
from huggingface_hub import notebook_login # Added this for HF login
|
11 |
+
from huggingface_hub.utils import HfHubHTTPError # Added this to catch HF login errors
|
12 |
+
# Initialize Hugging Face Hub login
|
13 |
+
notebook_login()
|
14 |
+
# Initialize Accelerator
|
15 |
+
accelerator = Accelerator()
|
16 |
|
17 |
|
18 |
# Load the model file
|
|
|
20 |
if not os.path.exists(model_path):
|
21 |
# Download the model file if it doesn't exist
|
22 |
model_url = "https://huggingface.co/DILHTWD/documentlayoutsegmentation_YOLOv8_ondoclaynet/resolve/main/yolov8x-doclaynet-epoch64-imgsz640-initiallr1e-4-finallr1e-5.pt"
|
23 |
+
try:
|
24 |
+
response = requests.get(model_url)
|
25 |
+
with open(model_path, "wb") as f:
|
26 |
+
f.write(response.content)
|
27 |
+
except HfHubHTTPError as e:
|
28 |
+
if e.response.status_code == 401:
|
29 |
+
print("Authentication error. Please login to Hugging Face Hub.")
|
30 |
+
else:
|
31 |
+
raise e
|
32 |
# Load the document segmentation model
|
33 |
docseg_model = YOLO(model_path)
|
34 |
|
|
|
|
|
35 |
|
36 |
+
docseg_model = accelerator.prepare(docseg_model)
|
37 |
|
38 |
def process_image(image):
|
39 |
try:
|
40 |
# Convert image to the format YOLO model expects
|
41 |
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
42 |
|
43 |
+
# Move image to accelerator
|
44 |
+
image = torch.from_numpy(image).to(accelerator.device)
|
|
|
45 |
|
46 |
results = docseg_model.predict(image)
|
47 |
result = results[0] # Get the first (and usually only) result
|