Spaces:
Sleeping
Sleeping
import urllib.request | |
from pathlib import Path | |
import streamlit as st | |
# This code is based on https://github.com/streamlit/demo-self-driving/blob/230245391f2dda0cb464008195a470751c01770b/streamlit_app.py#L48 # noqa: E501 | |
def download_file(url, download_to: Path, expected_size=None): | |
# Don't download the file twice. | |
# (If possible, verify the download using the file length.) | |
if download_to.exists(): | |
if expected_size: | |
if download_to.stat().st_size == expected_size: | |
return | |
else: | |
st.info(f"{url} is already downloaded.") | |
if not st.button("Download again?"): | |
return | |
download_to.parent.mkdir(parents=True, exist_ok=True) | |
# These are handles to two visual elements to animate. | |
weights_warning, progress_bar = None, None | |
try: | |
weights_warning = st.warning("Downloading %s..." % url) | |
progress_bar = st.progress(0) | |
with open(download_to, "wb") as output_file: | |
with urllib.request.urlopen(url) as response: | |
length = int(response.info()["Content-Length"]) | |
counter = 0.0 | |
MEGABYTES = 2.0 ** 20.0 | |
while True: | |
data = response.read(8192) | |
if not data: | |
break | |
counter += len(data) | |
output_file.write(data) | |
# We perform animation by overwriting the elements. | |
weights_warning.warning( | |
"Downloading %s... (%6.2f/%6.2f MB)" | |
% (url, counter / MEGABYTES, length / MEGABYTES) | |
) | |
progress_bar.progress(min(counter / length, 1.0)) | |
# Finally, we remove these visual elements by calling .empty(). | |
finally: | |
if weights_warning is not None: | |
weights_warning.empty() | |
if progress_bar is not None: | |
progress_bar.empty() | |