|
47 | 47 | )
|
48 | 48 | from huggingface_hub.errors import HfHubHTTPError, ValidationError
|
49 | 49 | from huggingface_hub.inference._client import _open_as_binary
|
50 |
| -from huggingface_hub.inference._common import _stream_chat_completion_response, _stream_text_generation_response |
| 50 | +from huggingface_hub.inference._common import ( |
| 51 | + _as_url, |
| 52 | + _stream_chat_completion_response, |
| 53 | + _stream_text_generation_response, |
| 54 | +) |
51 | 55 | from huggingface_hub.inference._providers import get_provider_helper
|
52 | 56 | from huggingface_hub.inference._providers.hf_inference import _build_chat_completion_url
|
53 | 57 |
|
@@ -1163,3 +1167,28 @@ def test_chat_completion_url_resolution(
|
1163 | 1167 | assert request_params.url == expected_request_url
|
1164 | 1168 | assert request_params.json is not None
|
1165 | 1169 | assert request_params.json.get("model") == expected_payload_model
|
| 1170 | + |
| 1171 | + |
| 1172 | +@pytest.mark.parametrize( |
| 1173 | + "content_input, default_mime_type, expected, is_exact_match", |
| 1174 | + [ |
| 1175 | + ("https://my-url.com/cat.gif", "image/jpeg", "https://my-url.com/cat.gif", True), |
| 1176 | + ("assets/image.png", "image/jpeg", "data:image/png;base64,", False), |
| 1177 | + (Path("assets/image.png"), "image/jpeg", "data:image/png;base64,", False), |
| 1178 | + ("assets/image.foo", "image/jpeg", "data:image/jpeg;base64,", False), |
| 1179 | + (b"some image bytes", "image/jpeg", "data:image/jpeg;base64,c29tZSBpbWFnZSBieXRlcw==", True), |
| 1180 | + (io.BytesIO(b"some image bytes"), "image/jpeg", "data:image/jpeg;base64,c29tZSBpbWFnZSBieXRlcw==", True), |
| 1181 | + ], |
| 1182 | +) |
| 1183 | +def test_as_url(content_input, default_mime_type, expected, is_exact_match, tmp_path: Path): |
| 1184 | + if isinstance(content_input, (str, Path)) and not str(content_input).startswith("http"): |
| 1185 | + file_path = tmp_path / content_input |
| 1186 | + file_path.parent.mkdir(exist_ok=True, parents=True) |
| 1187 | + file_path.touch() |
| 1188 | + content_input = file_path |
| 1189 | + |
| 1190 | + result = _as_url(content_input, default_mime_type) |
| 1191 | + if is_exact_match: |
| 1192 | + assert result == expected |
| 1193 | + else: |
| 1194 | + assert result.startswith(expected) |
0 commit comments