tmdwo commited on
Commit
c89d1c8
ยท
verified ยท
1 Parent(s): 6cc746a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -14
app.py CHANGED
@@ -16,23 +16,41 @@ from transformers import (
16
  )
17
 
18
  # ============ GRADIO CLIENT COMPAT PATCH ============
19
- # HF Spaces (and some Gradio versions) may ship with gradio_client that crashes when
20
- # it encounters boolean JSON Schema nodes (e.g., additionalProperties: false) while
21
- # generating /info API schema. Patch defensively to prevent startup failures.
22
  try:
23
  import gradio_client.utils as _grc_utils
24
 
25
  if not getattr(_grc_utils, "_BOOL_SCHEMA_PATCHED", False):
26
- _orig_get_type = _grc_utils.get_type
 
27
 
28
  def _safe_get_type(schema): # noqa: ANN001
29
  if isinstance(schema, bool) or not isinstance(schema, dict):
30
- # boolean schema: True => any, False => no additional props
31
- # For our purposes (API type rendering), treat as Any to avoid crashes.
32
  return "any"
33
  return _orig_get_type(schema)
34
 
35
- _grc_utils.get_type = _safe_get_type
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  _grc_utils._BOOL_SCHEMA_PATCHED = True
37
  except Exception as _e:
38
  # If gradio_client is unavailable or API changes, ignore and proceed.
@@ -44,15 +62,22 @@ def _launch_compat(demo, **kwargs):
44
  Gradio ๋ฒ„์ „๋ณ„๋กœ Blocks.launch() ์‹œ๊ทธ๋‹ˆ์ฒ˜๊ฐ€ ๋‹ฌ๋ผ์„œ(์˜ˆ: ssr ์ง€์› ์—ฌ๋ถ€),
45
  ํ˜„์žฌ ์„ค์น˜๋œ Gradio๊ฐ€ ์ง€์›ํ•˜๋Š” ํ‚ค๋งŒ ๊ณจ๋ผ launch()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
46
  """
 
47
  try:
48
- sig = inspect.signature(demo.launch)
49
- supported = set(sig.parameters.keys())
50
- filtered = {k: v for k, v in kwargs.items() if k in supported}
51
- return demo.launch(**filtered)
52
- except Exception:
53
- # signature introspection ์‹คํŒจ ์‹œ, ๋ณด์ˆ˜์ ์œผ๋กœ ssr๋งŒ ์ œ๊ฑฐ ํ›„ ์žฌ์‹œ๋„
54
- kwargs.pop("ssr", None)
55
  return demo.launch(**kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # ============ THEME SETUP ============
58
  # Theme disabled to avoid API schema issues on HF Spaces
 
16
  )
17
 
18
  # ============ GRADIO CLIENT COMPAT PATCH ============
19
+ # Some gradio_client versions crash when encountering boolean JSON Schema nodes
20
+ # (e.g., additionalProperties: false/true) while generating /info API schema.
21
+ # Patch defensively to prevent startup failures on HF Spaces.
22
  try:
23
  import gradio_client.utils as _grc_utils
24
 
25
  if not getattr(_grc_utils, "_BOOL_SCHEMA_PATCHED", False):
26
+ # 1) get_type() sometimes assumes schema is a dict
27
+ _orig_get_type = getattr(_grc_utils, "get_type", None)
28
 
29
  def _safe_get_type(schema): # noqa: ANN001
30
  if isinstance(schema, bool) or not isinstance(schema, dict):
31
+ return "any"
32
+ if _orig_get_type is None:
33
  return "any"
34
  return _orig_get_type(schema)
35
 
36
+ if _orig_get_type is not None:
37
+ _grc_utils.get_type = _safe_get_type
38
+
39
+ # 2) _json_schema_to_python_type() may raise on schema == True/False
40
+ _orig_js2pt = getattr(_grc_utils, "_json_schema_to_python_type", None)
41
+
42
+ def _safe_json_schema_to_python_type(schema, defs=None): # noqa: ANN001
43
+ if isinstance(schema, bool):
44
+ # boolean JSON Schema: True means "any", False means "no value".
45
+ # For API-info rendering, treating both as Any avoids crashes.
46
+ return "Any"
47
+ if _orig_js2pt is None:
48
+ return "Any"
49
+ return _orig_js2pt(schema, defs)
50
+
51
+ if _orig_js2pt is not None:
52
+ _grc_utils._json_schema_to_python_type = _safe_json_schema_to_python_type
53
+
54
  _grc_utils._BOOL_SCHEMA_PATCHED = True
55
  except Exception as _e:
56
  # If gradio_client is unavailable or API changes, ignore and proceed.
 
62
  Gradio ๋ฒ„์ „๋ณ„๋กœ Blocks.launch() ์‹œ๊ทธ๋‹ˆ์ฒ˜๊ฐ€ ๋‹ฌ๋ผ์„œ(์˜ˆ: ssr ์ง€์› ์—ฌ๋ถ€),
63
  ํ˜„์žฌ ์„ค์น˜๋œ Gradio๊ฐ€ ์ง€์›ํ•˜๋Š” ํ‚ค๋งŒ ๊ณจ๋ผ launch()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
64
  """
65
+ # ๋จผ์ € ์›ํ•˜๋Š” ์˜ต์…˜์œผ๋กœ ๊ทธ๋Œ€๋กœ ํ˜ธ์ถœํ•˜๊ณ , "unexpected keyword"๊ฐ€ ๋‚˜๋ฉด ํ•ด๋‹น ํ‚ค๋งŒ ์ œ๊ฑฐ ํ›„ ์žฌ์‹œ๋„
66
  try:
 
 
 
 
 
 
 
67
  return demo.launch(**kwargs)
68
+ except TypeError as e:
69
+ msg = str(e)
70
+ if "unexpected keyword argument 'ssr'" in msg or 'unexpected keyword argument "ssr"' in msg:
71
+ kwargs.pop("ssr", None)
72
+ return demo.launch(**kwargs)
73
+ # ๊ทธ ์™ธ ์ผ€์ด์Šค๋Š” ์‹œ๊ทธ๋‹ˆ์ฒ˜ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ๋ฒˆ ๋” ๋ณด์ˆ˜์ ์œผ๋กœ ํ•„ํ„ฐ๋ง
74
+ try:
75
+ sig = inspect.signature(demo.launch)
76
+ supported = set(sig.parameters.keys())
77
+ filtered = {k: v for k, v in kwargs.items() if k in supported}
78
+ return demo.launch(**filtered)
79
+ except Exception:
80
+ raise
81
 
82
  # ============ THEME SETUP ============
83
  # Theme disabled to avoid API schema issues on HF Spaces