VcRlAgent commited on
Commit
c9b8d57
·
1 Parent(s): 47edd1f

InstantID For retaining facial identity

Browse files
Files changed (1) hide show
  1. app.py +52 -2
app.py CHANGED
@@ -20,10 +20,59 @@ logging.getLogger("onnxruntime").setLevel(logging.ERROR)
20
  os.environ["CUDA_VISIBLE_DEVICES"] = ""
21
 
22
  # --- Ensure InstantID is available ---
23
- if not os.path.exists("instantid"):
24
  print("🔄 Cloning InstantID repository...")
25
- subprocess.run(["git", "clone", "https://github.com/InstantID/InstantID.git"], check=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
 
27
  if os.path.exists("InstantID") and not os.path.exists("instantid"):
28
  os.rename("InstantID", "instantid")
29
 
@@ -39,6 +88,7 @@ try:
39
  except Exception as e:
40
  print("⚠️ Failed to import InstantIDPipeline:", e)
41
  InstantIDPipeline = None # graceful fallback
 
42
 
43
  import torchvision
44
  print("Printing Torch and TorchVision versions:")
 
20
  os.environ["CUDA_VISIBLE_DEVICES"] = ""
21
 
22
  # --- Ensure InstantID is available ---
23
+ if not Path("instantid").exists():
24
  print("🔄 Cloning InstantID repository...")
25
+ subprocess.run(["git", "clone", "--depth", "1", "https://github.com/InstantID/InstantID.git", "instantid"],check=True)
26
+
27
+ repo_root = Path("instantid").resolve()
28
+
29
+ # 🧭 Search for a pipeline file that matches *instantid*.py under the repo
30
+ candidates = list(repo_root.rglob("pipeline*instantid*.py"))
31
+ if not candidates:
32
+ # Fallback common names across commits
33
+ fallback_names = [
34
+ "pipelines/pipeline_instantid.py",
35
+ "pipelines/pipeline_stable_diffusion_instantid.py",
36
+ "pipelines/pipeline_stable_diffusion_xl_instantid.py",
37
+ ]
38
+ for name in fallback_names:
39
+ p = repo_root / name
40
+ if p.exists():
41
+ candidates = [p]
42
+ break
43
+
44
+ if not candidates:
45
+ raise FileNotFoundError(
46
+ "Could not locate an InstantID pipeline file under ./instantid. "
47
+ "Repo layout may have changed. Please check the repo structure."
48
+ )
49
+
50
+ pipeline_file = candidates[0]
51
+ print(f"✅ Using InstantID pipeline file: {pipeline_file.relative_to(repo_root)}")
52
+
53
+ # 🪄 Import the pipeline module by file path (no package needed)
54
+ spec = importlib.util.spec_from_file_location("instantid_pipeline", str(pipeline_file))
55
+ instantid_mod = importlib.util.module_from_spec(spec)
56
+ spec.loader.exec_module(instantid_mod) # type: ignore
57
+
58
+ # 🔎 Pick a pipeline class that looks like an InstantID Pipeline
59
+ InstantIDPipeline = None
60
+ for attr in dir(instantid_mod):
61
+ if "InstantID" in attr and "Pipeline" in attr:
62
+ InstantIDPipeline = getattr(instantid_mod, attr)
63
+ break
64
+
65
+ if InstantIDPipeline is None:
66
+ # Helpful diagnostics
67
+ print("Available names in module:", [a for a in dir(instantid_mod) if "Pipeline" in a])
68
+ raise ImportError(
69
+ "Could not find an InstantID pipeline class. "
70
+ "Looked for a class name containing both 'InstantID' and 'Pipeline'."
71
+ )
72
+
73
+ print(f"✅ Imported pipeline class: {InstantIDPipeline.__name__}")
74
 
75
+ '''
76
  if os.path.exists("InstantID") and not os.path.exists("instantid"):
77
  os.rename("InstantID", "instantid")
78
 
 
88
  except Exception as e:
89
  print("⚠️ Failed to import InstantIDPipeline:", e)
90
  InstantIDPipeline = None # graceful fallback
91
+ '''
92
 
93
  import torchvision
94
  print("Printing Torch and TorchVision versions:")