Maarij-Aqeel commited on
Commit
d189374
·
1 Parent(s): 2a40912

Monkey-patch starlette TemplateResponse for gradio 4.44.0 compat

Browse files

The HF Spaces container installs starlette 1.0+ which removed auto-detection
for the deprecated TemplateResponse(name_str, context_dict) positional call.
gradio 4.44.0 still uses that old style; the context dict ends up assigned to
the 'name' parameter and jinja2's template cache crashes with 'unhashable type:
dict'. Patch Jinja2Templates.TemplateResponse before importing gradio so
old-style calls are translated to the new (request, name, context) signature.

Files changed (1) hide show
  1. app.py +18 -0
app.py CHANGED
@@ -1,4 +1,22 @@
1
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import torch
3
  import torchaudio
4
  import gradio as gr
 
1
  import os
2
+
3
+ # Starlette >= 1.0 removed the deprecated positional TemplateResponse(name, context)
4
+ # call style. gradio 4.44.0 still calls templates.TemplateResponse(template_str,
5
+ # context_dict), which then hits the new-only signature TemplateResponse(request,
6
+ # name, ...) and passes the context dict as `name`, exploding inside jinja2's cache
7
+ # with "unhashable type: 'dict'". Translate old-style calls before gradio imports.
8
+ import starlette.templating as _st_templating
9
+ _orig_template_response = _st_templating.Jinja2Templates.TemplateResponse
10
+ def _compat_template_response(self, *args, **kwargs):
11
+ if args and isinstance(args[0], str):
12
+ name = args[0]
13
+ context = args[1] if len(args) > 1 else kwargs.pop("context", None) or {}
14
+ request = context.get("request") if isinstance(context, dict) else None
15
+ if request is not None:
16
+ return _orig_template_response(self, request, name, context, *args[2:], **kwargs)
17
+ return _orig_template_response(self, *args, **kwargs)
18
+ _st_templating.Jinja2Templates.TemplateResponse = _compat_template_response
19
+
20
  import torch
21
  import torchaudio
22
  import gradio as gr