Skip to content

Commit

Permalink
fix encoding condition
Browse files Browse the repository at this point in the history
  • Loading branch information
hanouticelina committed Oct 15, 2024
1 parent b4bd2b5 commit 9ac79df
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/huggingface_hub/inference/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ def _prepare_payload(

json: Dict[str, Any] = {}
# If inputs is a bytes-like object, encode it to base64
if isinstance(inputs, (bytes, Path)) or (isinstance(inputs, str) and inputs.startswith(("http://", "https://"))):
json["inputs"] = _b64_encode(inputs)
# If inputs is a string or a dict, send it as is
elif isinstance(inputs, (dict, str)):
if expect_binary:
json["inputs"] = _b64_encode(inputs) # type: ignore
# Otherwise (string, dict, list) send it as is
else:
json["inputs"] = inputs
# Add parameters to the json payload if any
if has_parameters:
Expand Down
25 changes: 23 additions & 2 deletions tests/test_inference_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,17 @@ def test_resolve_chat_completion_url(
None,
"http://example.com",
),
# Case 10: inputs is a URL string with parameters
# Case 10: inputs is a URL string without parameters but expect_binary is False
(
"http://example.com",
None,
False,
{
"inputs": "http://example.com",
},
None,
),
# Case 11: inputs is a URL string with parameters
(
"http://example.com",
{"param1": "value1"},
Expand All @@ -1184,7 +1194,18 @@ def test_resolve_chat_completion_url(
},
None,
),
# Case 11: parameters contain None values
# Case 12: inputs is a URL string with parameters but expect_binary is False
(
"http://example.com",
{"param1": "value1"},
False,
{
"inputs": "http://example.com",
"parameters": {"param1": "value1"},
},
None,
),
# Case 13: parameters contain None values
(
"simple text",
{"param1": None, "param2": "value2"},
Expand Down

0 comments on commit 9ac79df

Please sign in to comment.