NaveenKumar Namachivayam commited on
Commit
bd9ee71
·
1 Parent(s): 48ebfc3

fix: pin starlette/fastapi to avoid Jinja2 LRUCache bug, remove monkey-patch

Browse files

Pin starlette<0.40.0 and fastapi<0.115.0 to prevent the unhashable dict
Jinja2 cache key bug in gradio 4.44.0. Remove the LRUCache monkey-patch
since the dependency pins prevent the issue at its source.

Files changed (2) hide show
  1. app.py +0 -23
  2. requirements.txt +2 -0
app.py CHANGED
@@ -2,29 +2,6 @@
2
  import random
3
  import re
4
 
5
- # Patch Jinja2 LRUCache to handle unhashable keys (gradio 4.44.0 bug).
6
- # HF Spaces force-installs gradio==4.44.0 which passes a dict as a Jinja2
7
- # cache key. Convert unhashable keys to a stable tuple before cache ops.
8
- try:
9
- from jinja2.utils import LRUCache
10
-
11
- def _make_hashable(key):
12
- if isinstance(key, dict):
13
- return tuple(sorted((k, _make_hashable(v)) for k, v in key.items()))
14
- if isinstance(key, list):
15
- return tuple(_make_hashable(i) for i in key)
16
- return key
17
-
18
- _orig_getitem = LRUCache.__getitem__
19
- _orig_get = LRUCache.get
20
- _orig_setitem = LRUCache.__setitem__
21
-
22
- LRUCache.__getitem__ = lambda self, key: _orig_getitem(self, _make_hashable(key))
23
- LRUCache.get = lambda self, key, default=None: _orig_get(self, _make_hashable(key), default)
24
- LRUCache.__setitem__ = lambda self, key, value: _orig_setitem(self, _make_hashable(key), value)
25
- except Exception:
26
- pass
27
-
28
  import huggingface_hub
29
 
30
  if not hasattr(huggingface_hub, "HfFolder"):
 
2
  import random
3
  import re
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import huggingface_hub
6
 
7
  if not hasattr(huggingface_hub, "HfFolder"):
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
  torch>=2.0.0
2
  huggingface-hub>=0.30,<1.0.0
 
 
 
1
  torch>=2.0.0
2
  huggingface-hub>=0.30,<1.0.0
3
+ starlette<0.40.0
4
+ fastapi<0.115.0