AkashKumarave commited on
Commit
581454b
·
verified ·
1 Parent(s): 84e5a17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -6,23 +6,42 @@ from diffusers import StableDiffusionXLPipeline
6
  from insightface.app import FaceAnalysis
7
  from accelerate import init_empty_weights, load_checkpoint_and_dispatch
8
  import os
9
- import zipfile
10
 
11
- # Force offline mode
12
  os.environ["HF_HUB_OFFLINE"] = "1"
13
 
14
  # Set device to CPU
15
  device = "cpu"
16
  dtype = torch.float32
17
 
18
- # Extract InsightFace model if needed
19
  insightface_model_dir = "/home/user/.insightface/models/buffalo_l"
20
- if not os.path.exists(insightface_model_dir):
21
- os.makedirs(insightface_model_dir, exist_ok=True)
22
- with zipfile.ZipFile("./buffalo_l.zip", "r") as zip_ref:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  zip_ref.extractall(insightface_model_dir)
 
24
 
25
- # Load face encoder with preloaded model
26
  face_app = FaceAnalysis(providers=["CPUExecutionProvider"], root="/home/user/.insightface/models")
27
  face_app.prepare(ctx_id=0, det_size=(480, 480))
28
 
 
6
  from insightface.app import FaceAnalysis
7
  from accelerate import init_empty_weights, load_checkpoint_and_dispatch
8
  import os
9
+ import urllib.request
10
 
11
+ # Force offline mode for Hugging Face Hub (but allow InsightFace download)
12
  os.environ["HF_HUB_OFFLINE"] = "1"
13
 
14
  # Set device to CPU
15
  device = "cpu"
16
  dtype = torch.float32
17
 
18
+ # Set up InsightFace model directory
19
  insightface_model_dir = "/home/user/.insightface/models/buffalo_l"
20
+ os.makedirs(insightface_model_dir, exist_ok=True)
21
+ buffalo_l_zip = "./buffalo_l.zip"
22
+
23
+ # Download buffalo_l.zip if not present
24
+ if not os.path.exists(buffalo_l_zip):
25
+ try:
26
+ print("Downloading buffalo_l.zip for InsightFace...")
27
+ urllib.request.urlretrieve(
28
+ "https://github.com/deepinsight/insightface/releases/download/v0.7/buffalo_l.zip",
29
+ buffalo_l_zip
30
+ )
31
+ print("Download completed.")
32
+ except Exception as e:
33
+ print(f"Failed to download buffalo_l.zip: {e}")
34
+ # Fallback to a lighter model or raise error
35
+ raise RuntimeError("Cannot download buffalo_l.zip. Please ensure network access or preload the file.")
36
+
37
+ # Extract buffalo_l.zip
38
+ if os.path.exists(buffalo_l_zip):
39
+ import zipfile
40
+ with zipfile.ZipFile(buffalo_l_zip, "r") as zip_ref:
41
  zip_ref.extractall(insightface_model_dir)
42
+ print("Extracted buffalo_l.zip.")
43
 
44
+ # Load face encoder
45
  face_app = FaceAnalysis(providers=["CPUExecutionProvider"], root="/home/user/.insightface/models")
46
  face_app.prepare(ctx_id=0, det_size=(480, 480))
47