zerovic commited on
Commit
38fcfdd
·
verified ·
1 Parent(s): ec9864f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -14
app.py CHANGED
@@ -1,22 +1,52 @@
1
  import sys
 
2
 
3
- # --- CRITICAL PATCH: FIX SCHEMA PARSER TYPEERROR ON PYTHON 3.13 ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  try:
5
  import gradio_client.utils as client_utils
6
- orig_get_type = client_utils.get_type
7
 
8
- def safe_get_type(schema):
9
- # If the schema parser encounters a raw boolean instead of a dictionary,
10
- # return a default fallback string to avoid the 'bool is not iterable' crash.
11
- if isinstance(schema, bool):
12
- return "boolean"
13
- return orig_get_type(schema)
14
-
15
- client_utils.get_type = safe_get_type
16
- print("Successfully patched gradio_client schema serialization engine.")
17
- except Exception as patch_err:
18
- print(f"Failed to apply serialization patch: {patch_err}")
19
- # -----------------------------------------------------------------
20
 
21
  import os
22
  import cv2
 
1
  import sys
2
+ import types
3
 
4
+ # =================================================================
5
+ # CRITICAL PYTHON 3.13 / GRADIO SDK CONFLICT MONKEY-PATCHES
6
+ # Must execute before ANY other library import to intercept Gradio
7
+ # =================================================================
8
+
9
+ # 1. Force-inject HfFolder into sys.modules to satisfy older Gradio imports
10
+ try:
11
+ # Create a dynamic mock class for HfFolder matching its historical signatures
12
+ class MockHfFolder:
13
+ @classmethod
14
+ def get_token(cls): return None
15
+ @classmethod
16
+ def save_token(cls, token): pass
17
+ @classmethod
18
+ def delete_token(cls): pass
19
+
20
+ # If huggingface_hub isn't fully loaded yet, build a stub module layout for it
21
+ if 'huggingface_hub' not in sys.modules:
22
+ hf_hub_mock = types.ModuleType('huggingface_hub')
23
+ hf_hub_mock.HfFolder = MockHfFolder
24
+ hf_hub_mock.whoami = lambda *args, **kwargs: {"name": "anonymous", "auth": {"type": "none"}}
25
+ sys.modules['huggingface_hub'] = hf_hub_mock
26
+ else:
27
+ # If it is loaded, inject the missing class descriptor directly into the object surface
28
+ import huggingface_hub
29
+ if not hasattr(huggingface_hub, 'HfFolder'):
30
+ huggingface_hub.HfFolder = MockHfFolder
31
+ sys.modules['huggingface_hub'].HfFolder = MockHfFolder
32
+ except Exception as patch_err_1:
33
+ print(f"Pre-import HfFolder patch skipped or failed: {patch_err_1}")
34
+
35
+ # 2. Patch the Gradio client schema serialization parser to avoid the 'bool' type loop error
36
  try:
37
  import gradio_client.utils as client_utils
38
+ orig_get_type = getattr(client_utils, 'get_type', None)
39
 
40
+ if orig_get_type:
41
+ def safe_get_type(schema):
42
+ if isinstance(schema, bool):
43
+ return "boolean"
44
+ return orig_get_type(schema)
45
+ client_utils.get_type = safe_get_type
46
+ print("Successfully patched gradio_client schema serialization engine.")
47
+ except Exception as patch_err_2:
48
+ print(f"Schema serialization engine patch deferred: {patch_err_2}")
49
+ # =================================================================
 
 
50
 
51
  import os
52
  import cv2