File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed
Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 88
99import httpx
1010import jinja2
11+ from jinja2 .sandbox import ImmutableSandboxedEnvironment
1112from loguru import logger
1213from PIL import Image
1314
@@ -136,7 +137,17 @@ def __getstate__(self) -> object:
136137
137138 @cached_property
138139 def request_template (self ) -> jinja2 .Template :
139- j2_env = jinja2 .Environment (loader = jinja2 .BaseLoader (), autoescape = True )
140+ # Thanks to HuggingFace Tokenizers for this implementation
141+ def tojson (x , ensure_ascii = False ):
142+ # We override the built-in tojson filter because Jinja's
143+ # default filter escapes HTML characters
144+ return json .dumps (x , ensure_ascii = ensure_ascii )
145+
146+ j2_env = ImmutableSandboxedEnvironment (trim_blocks = True , lstrip_blocks = True )
147+
148+ # Define custom filter functions
149+ j2_env .filters ["tojson" ] = tojson
150+
140151 return j2_env .from_string (self ._request_template_str )
141152
142153 @property
You can’t perform that action at this time.
0 commit comments