Skip to content

Commit

Permalink
better print and pbar
Browse files Browse the repository at this point in the history
  • Loading branch information
NevermindNilas committed Jun 17, 2024
1 parent a4a498f commit ec3dfb5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
38 changes: 25 additions & 13 deletions src/downloadModels.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,16 @@ def modelsMap(
return "maxxvitv2_rmlp_base_rw_224.sw_in12k_b80_224px_20k_coloraug0.4_6ch_clamp_softmax_fp16_op17_onnxslim.onnx"
else:
return "maxxvitv2_rmlp_base_rw_224.sw_in12k_b80_224px_20k_coloraug0.4_6ch_clamp_softmax_op17_onnxslim.onnx"

case "pervfi_lite":
return "pervfi_lite.pth"

case "pervfi":
return "pervfi.pth"

case "raft":
return "raft-sintel.pth"

case _:
raise ValueError(f"Model {model} not found.")

Expand All @@ -351,23 +351,36 @@ def downloadAndLog(model: str, filename: str, download_url: str, folderPath: str

toLog = f"Downloading {model.upper()} model..."
logging.info(toLog)
print(toLog)

response = requests.get(download_url, stream=True)

try:
total_size_in_bytes = int(response.headers.get("content-length", 0))
if total_size_in_bytes == 0:
total_size_in_bytes = None
total_size_in_mb = total_size_in_bytes / (1024 * 1024) # Convert bytes to MB
except Exception as e:
total_size_in_bytes = None
total_size_in_mb = 0 # If there's an error, default to 0 MB
logging.error(e)

with alive_bar(total_size_in_bytes, title="Progress", bar="smooth") as bar:
with alive_bar(
int(total_size_in_mb + 1), # Hacky but it works
title=f"Downloading {model.capitalize()} model",
bar="smooth",
unit = "MB",
spinner=True,
enrich_print=False,
receipt=True,
monitor=True,
elapsed=True,
stats=False,
dual_line=True,
force_tty=True,
) as bar:
with open(os.path.join(folderPath, filename), "wb") as file:
for data in response.iter_content(chunk_size=1024):
for data in response.iter_content(
chunk_size=1024 * 1024
):
file.write(data)
bar(len(data))
bar(int(len(data) / (1024 * 1024)))

if filename.endswith(".zip"):
import zipfile
Expand All @@ -377,8 +390,7 @@ def downloadAndLog(model: str, filename: str, download_url: str, folderPath: str
os.remove(os.path.join(folderPath, filename))
filename = filename[:-4]

toLog = f"Downloaded {model.upper()} model to: {os.path.join(folderPath, filename)}"

toLog = f"Downloaded {model.capitalize()} model to: {os.path.join(folderPath, filename)}"
logging.info(toLog)
print(toLog)

Expand Down
32 changes: 20 additions & 12 deletions src/getFFMPEG.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ def getFFMPEG():

def downloadAndExtractFFMPEG(ffmpegPath):
logging.info("Getting FFMPEG")
print(
"Couldn't find FFMPEG, downloading it now. This will take a few seconds on the first run, but will be cached for future runs."
)

if platform.system() == "Windows":
FFMPEGURL = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip"
extractFunc = extractFFMPEGZip
Expand All @@ -52,15 +48,27 @@ def downloadAndExtractFFMPEG(ffmpegPath):

response = requests.get(FFMPEGURL, stream=True)
totalSizeInBytes = int(response.headers.get("content-length", 0))

with alive_bar(total=totalSizeInBytes, title="Downloading FFMPEG", bar="smooth") as bar, open(ffmpegArchivePath, "wb") as file:
for data in response.iter_content(chunk_size=1024):
totalSizeInMB = int(totalSizeInBytes / 1024 / 1024)

with alive_bar(
total=totalSizeInMB + 1,
title="Downloading FFmpeg",
bar="smooth",
unit="MB",
spinner=True,
enrich_print=True,
receipt=True,
monitor=True,
elapsed=True,
stats=False,
dual_line=True,
force_tty=True,
) as bar, open(ffmpegArchivePath, "wb") as file:
for data in response.iter_content(chunk_size=1024 * 1024):
file.write(data)
bar(len(data))
bar(int(len(data) / (1024 * 1024)))

extractFunc(ffmpegArchivePath, ffmpegDir)

print("\n")
return str(ffmpegPath)


Expand All @@ -81,10 +89,10 @@ def extractFFMPEGTar(ffmpegTarPath, ffmpegDir):
with tarfile.open(ffmpegTarPath, "r:xz") as tarRef:
tarRef.extractall(ffmpegDir)

for directory in ffmpegDir.glob('ffmpeg-*-static'):
for directory in ffmpegDir.glob("ffmpeg-*-static"):
(directory / "ffmpeg").rename(ffmpegDir / "ffmpeg")
break

ffmpegTarPath.unlink()
for directory in glob.glob(str(ffmpegDir / "ffmpeg-*-static")):
shutil.rmtree(directory)
shutil.rmtree(directory)

0 comments on commit ec3dfb5

Please sign in to comment.