sara.mesquita commited on
Commit
dfd5424
·
1 Parent(s): 2af65c2

fix: robust patch script handles any indentation level

Browse files
Files changed (1) hide show
  1. patch_gradio.py +43 -8
patch_gradio.py CHANGED
@@ -1,12 +1,47 @@
1
- """Patch gradio_client get_type() to handle bool schemas (additionalProperties: false)."""
 
 
 
 
 
2
  path = "/usr/local/lib/python3.11/site-packages/gradio_client/utils.py"
3
  with open(path) as f:
4
  src = f.read()
5
- patched = src.replace(
6
- "def get_type(schema):\n",
7
- 'def get_type(schema):\n if not isinstance(schema, dict): return "Any"\n',
 
 
 
 
 
 
 
 
 
8
  )
9
- assert patched != src, "Pattern not found — patch failed"
10
- with open(path, "w") as f:
11
- f.write(patched)
12
- print("gradio_client patched OK")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Patch gradio_client/utils.py para tratar schema=bool em get_type().
3
+ JSON Schema permite additionalProperties: false (bool), mas gradio_client assume dict.
4
+ """
5
+ import re
6
+
7
  path = "/usr/local/lib/python3.11/site-packages/gradio_client/utils.py"
8
  with open(path) as f:
9
  src = f.read()
10
+
11
+ # Encontra a definição de get_type (pode estar indentada) e injeta guard no início
12
+ # Antes: def get_type(schema):
13
+ # Depois: def get_type(schema):
14
+ # if not isinstance(schema, dict): return "Any"
15
+ patched = re.sub(
16
+ r'([ \t]*def get_type\(schema\):\n)',
17
+ r'\1\g<1> if not isinstance(schema, dict): return "Any"\n'.replace(
18
+ r'\g<1>', ''
19
+ ),
20
+ src,
21
+ count=1,
22
  )
23
+
24
+ # Fallback: substituição simples se regex falhar
25
+ if patched == src:
26
+ for variant in [
27
+ "def get_type(schema):\n",
28
+ " def get_type(schema):\n",
29
+ " def get_type(schema):\n",
30
+ ]:
31
+ if variant in src:
32
+ indent = variant[: len(variant) - len(variant.lstrip())]
33
+ guard = f"{indent} if not isinstance(schema, dict): return \"Any\"\n"
34
+ patched = src.replace(variant, variant + guard, 1)
35
+ print(f"Patched with variant: {repr(variant)}")
36
+ break
37
+
38
+ if patched == src:
39
+ print("WARNING: pattern not found, printing context around 'get_type':")
40
+ for i, line in enumerate(src.splitlines()):
41
+ if "get_type" in line:
42
+ print(f" line {i}: {repr(line)}")
43
+ # Não falha o build — app pode funcionar mesmo sem o patch
44
+ else:
45
+ with open(path, "w") as f:
46
+ f.write(patched)
47
+ print("gradio_client patched OK")