Babajaan commited on
Commit
6d737f5
·
verified ·
1 Parent(s): d3aff1c

Fix: robust HfFolder shim via sys.modules dict injection for gradio 5.0.0

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -12,10 +12,13 @@ import traceback
12
  from pathlib import Path
13
 
14
  # ── Compatibility shim: gradio 5.0.0 + huggingface_hub >= 0.27.0 ──
15
- # HfFolder was removed in huggingface_hub 0.27 but gradio 5.0.0 still imports it
 
16
  try:
 
17
  import huggingface_hub
18
- if not hasattr(huggingface_hub, "HfFolder"):
 
19
  class _HfFolderCompat:
20
  @staticmethod
21
  def get_token():
@@ -26,9 +29,15 @@ try:
26
  @staticmethod
27
  def delete_token():
28
  pass
29
- huggingface_hub.HfFolder = _HfFolderCompat()
30
- except Exception:
31
- pass
 
 
 
 
 
 
32
 
33
  # Ensure our modules are importable
34
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
 
12
  from pathlib import Path
13
 
14
  # ── Compatibility shim: gradio 5.0.0 + huggingface_hub >= 0.27.0 ──
15
+ # HfFolder was removed in huggingface_hub 0.27+ but gradio 5.0.0 still imports it.
16
+ # We inject a minimal stub directly into huggingface_hub's module dict BEFORE gradio loads.
17
  try:
18
+ import sys
19
  import huggingface_hub
20
+ hf_mod = sys.modules.get("huggingface_hub") or huggingface_hub
21
+ if "HfFolder" not in hf_mod.__dict__:
22
  class _HfFolderCompat:
23
  @staticmethod
24
  def get_token():
 
29
  @staticmethod
30
  def delete_token():
31
  pass
32
+ # Inject into the real module dict so `from huggingface_hub import HfFolder` works
33
+ hf_mod.__dict__["HfFolder"] = _HfFolderCompat()
34
+ if hasattr(hf_mod, "__all__") and "HfFolder" not in hf_mod.__all__:
35
+ hf_mod.__all__.append("HfFolder")
36
+ print("[ViralImages] HfFolder compatibility shim applied.")
37
+ else:
38
+ print("[ViralImages] HfFolder already present, no shim needed.")
39
+ except Exception as e:
40
+ print(f"[ViralImages] HfFolder shim failed (non-fatal): {e}")
41
 
42
  # Ensure our modules are importable
43
  sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))