| import urllib.request |
| from pathlib import Path |
|
|
| import streamlit as st |
|
|
|
|
| |
| def download_file(url, download_to: Path, expected_size=None): |
| |
| |
| 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) |
|
|
| |
| 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) |
|
|
| |
| weights_warning.warning( |
| "Downloading %s... (%6.2f/%6.2f MB)" |
| % (url, counter / MEGABYTES, length / MEGABYTES) |
| ) |
| progress_bar.progress(min(counter / length, 1.0)) |
| |
| finally: |
| if weights_warning is not None: |
| weights_warning.empty() |
| if progress_bar is not None: |
| progress_bar.empty() |
|
|