hysts HF staff commited on
Commit
df9df13
1 Parent(s): ff2f092
Files changed (1) hide show
  1. app.py +30 -0
app.py CHANGED
@@ -9,6 +9,7 @@ import sys
9
  import gradio as gr
10
  import numpy as np
11
  import torch
 
12
 
13
  sys.path.insert(0, "face_detection")
14
  sys.path.insert(0, "face_parsing")
@@ -21,6 +22,35 @@ from ibug.face_parsing.utils import label_colormap
21
  DESCRIPTION = "# [hhj1897/face_parsing](https://github.com/hhj1897/face_parsing)"
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def load_model(model_name: str, device: torch.device) -> FaceParser:
25
  encoder, decoder, num_classes = model_name.split("-")
26
  num_classes = int(num_classes) # type: ignore
 
9
  import gradio as gr
10
  import numpy as np
11
  import torch
12
+ from huggingface_hub import hf_hub_download
13
 
14
  sys.path.insert(0, "face_detection")
15
  sys.path.insert(0, "face_parsing")
 
22
  DESCRIPTION = "# [hhj1897/face_parsing](https://github.com/hhj1897/face_parsing)"
23
 
24
 
25
+ def is_lfs_pointer_file(path: pathlib.Path) -> bool:
26
+ try:
27
+ with open(path, "r") as f:
28
+ # Git LFS pointer files usually start with version line
29
+ version_line = f.readline()
30
+ if version_line.startswith("version https://git-lfs.github.com/spec/"):
31
+ # Check for the presence of oid and size lines
32
+ oid_line = f.readline()
33
+ size_line = f.readline()
34
+ if oid_line.startswith("oid sha256:") and size_line.startswith("size "):
35
+ return True
36
+ except Exception as e:
37
+ print(f"Error reading file {path}: {e}")
38
+ return False
39
+
40
+
41
+ lfs_model_paths = sorted(pathlib.Path("face_parsing").rglob("*.torch"))
42
+ for lfs_model_path in lfs_model_paths:
43
+ if is_lfs_pointer_file(lfs_model_path):
44
+ os.remove(lfs_model_path)
45
+ out_path = hf_hub_download(
46
+ "public-data/ibug-face-parsing",
47
+ filename=lfs_model_path.name,
48
+ repo_type="model",
49
+ subfolder=lfs_model_path.parts[-3],
50
+ )
51
+ os.symlink(out_path, lfs_model_path)
52
+
53
+
54
  def load_model(model_name: str, device: torch.device) -> FaceParser:
55
  encoder, decoder, num_classes = model_name.split("-")
56
  num_classes = int(num_classes) # type: ignore