Fix validate url (#2957)

* Fix validate url

* Add unit test + fix impl

* Fix CHANGELOG

* Use head instead of get to check url

* Add head route for files

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
Freddy Boulton 2023-01-18 18:47:57 +01:00 committed by GitHub
parent 0091b366af
commit cab8d888dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 2 deletions

View File

@ -12,6 +12,7 @@ No changes to highlight.
* Fixes bug where png files were not being recognized when uploading images by [@abidlabs](https://github.com/abidlabs) in [PR 3002](https://github.com/gradio-app/gradio/pull/3002)
* Fixes bug where temporary uploaded files were not being added to temp sets by [@abidlabs](https://github.com/abidlabs) in [PR 3005](https://github.com/gradio-app/gradio/pull/3005)
* Fixes issue where markdown support in chatbot breaks older demos [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 3006](https://github.com/gradio-app/gradio/pull/3006)
* Fix bug where the Image component could not serialize image urls by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2957](https://github.com/gradio-app/gradio/pull/2957)
## Documentation Changes:
* SEO improvements to guides by[@aliabd](https://github.com/aliabd) in [PR 2915](https://github.com/gradio-app/gradio/pull/2915)

View File

@ -255,6 +255,7 @@ class App(FastAPI):
else:
return FileResponse(blocks.favicon_path)
@app.head("/file={path:path}", dependencies=[Depends(login_check)])
@app.get("/file={path:path}", dependencies=[Depends(login_check)])
async def file(path: str, request: fastapi.Request):
abs_path = str(Path(path).resolve())

View File

@ -80,8 +80,10 @@ class ImgSerializable(Serializable):
"""
if x is None or x == "":
return None
is_url = utils.validate_url(x)
path = x if is_url else Path(load_dir) / x
return processing_utils.encode_url_or_file_to_base64(
Path(load_dir) / x, encryption_key=encryption_key
path, encryption_key=encryption_key
)
def deserialize(

View File

@ -710,7 +710,10 @@ def append_unique_suffix(name: str, list_of_names: List[str]):
def validate_url(possible_url: str) -> bool:
headers = {"User-Agent": "gradio (https://gradio.app/; team@gradio.app)"}
try:
return requests.get(possible_url, headers=headers).ok
head_request = requests.head(possible_url, headers=headers)
if head_request.status_code == 405:
return requests.get(possible_url, headers=headers).ok
return head_request.ok
except Exception:
return False

View File

@ -668,6 +668,12 @@ class TestImage:
image_output = gr.Image(type="numpy")
assert image_output.postprocess(y_img).startswith("data:image/png;base64,")
@pytest.mark.flaky
def test_serialize_url(self):
img = "https://gradio.app/assets/img/header-image.jpg"
expected = processing_utils.encode_url_or_file_to_base64(img)
assert gr.Image().serialize(img) == expected
def test_in_interface_as_input(self):
"""
Interface, process, interpret