Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix Suggestion for _download_file_resumable in model_download.py #16

Open
SMACY2017 opened this issue Jan 28, 2024 · 3 comments
Open

Comments

@SMACY2017
Copy link

Hello,

我在使用过程中遇到了一个小问题,并想分享我找到的解决方案,希望能对其他使用者也有所帮助。

问题描述

在使用 _download_file_resumable 函数下载文件时,如果服务器的响应中没有 content-length 头部,脚本会抛出 TypeError。这是因为脚本试图将 None 转换为整数。

出现错误的代码

total_length = int(r.headers.get('content-length'))

建议的解决方案

我建议在尝试转换之前检查 content-length 是否存在。如果不存在,可以将 total_length 设置为 0 或 None。以下是修改后的代码段:

total_length = r.headers.get('content-length')

if total_length is not None:
    total_length = int(total_length)
else:
    # 如果没有content-length头,可以选择跳过进度条或使用其他方法估计文件大小
    total_length = 0  # 或者设置为None,根据您的需求调整

这个修改可以防止在缺少 content-length 头部时脚本崩溃,并允许下载继续进行。

希望这个建议对您有所帮助。再次感谢您的工作!

@little51
Copy link
Contributor

非常感谢,我按您的思路改了一下,做了一下判断。r.status_code应该是返回404,这时候total_length设成 0也不能正常下载,所以我直接给返False了

@SMACY2017
Copy link
Author

非常感谢您的及时回复!我希望在这里完整叙述一下出错的过程和您的修改后的效果。

初始问题描述

在运行最开始的版本时遇到了错误:

(llm_env) [用户名@服务器 ChatGLM-Finetuning]$ python model_download.py --repo_id THUDM/chatglm3-6b --mirror
2 of 25(README.md)100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████|1M/1M [00:00<00:00
Traceback (most recent call last):
File "PATH/model_download.py", line 221, in <module>
download_model_from_mirror(
File "PATH/model_download.py", line 199, in download_model_from_mirror
if _download_model_from_mirror(_repo_id, _repo_type, _token, _e):
File "PATH/model_download.py", line 193, in _download_model_from_mirror
if not _download_file_resumable(url, file_name, i, len(files)):
File "PATH/model_download.py", line 135, in _download_file_resumable
total_length = int(r.headers.get('content-length'))
...

按照我所说的修改后的结果

可以正常下载

(llm_env) [用户名@服务器 ChatGLM-Finetuning]$ python model_download.py --repo_id THUDM/chatglm3-6b --mirror
3 of 25(pytorch_model-00001-of-00007.bin)  1%|▌                                                                                                   |10M/1743M [00:14<413 of 25(pytorch_model-00001-of-00007.bin)  1%|▋                                                                                                   |11M/1743M [00:16<453 of 25(pytorch_model-00001-of-00007.bin)  1%|▋                                                                                                   |12M/1743M [00:17<413 of 25(pytorch_model-00001-of-00007.bin)  1%|▋                                                                                                   |13M/1743M [00:19<453 of 25(pytorch_model-00001-of-00007.bin)  1%|▊                                                                                                   |14M/1743M [00:20<413 of 25(pytorch_model-00001-of-00007.bin)  1%|▊                                                                                                   |15M/1743M [00:21<373 of 25(pytorch_model-00001-of-00007.bin)  1%|▉                                                                                                   |16M/1743M [00:23<433 of 25(pytorch_model-00001-of-00007.bin)  1%|▉                                                                                                   |17M/1743M [00:24<393 of 25(pytorch_model-00001-of-00007.bin)  1%|█                                                                                                   |18M/1743M [00:26<453 of 25(pytorch_model-00001-of-00007.bin)  1%|█
...

尝试您的最新修改

我刚刚尝试了一下您提出的直接返回 False 的方法,不能正常下载,结果如下:

(llm_env) [用户名@服务器 ChatGLM-Finetuning]$ python model_download.py --repo_id THUDM/chatglm3-6b --mirror
2024-01-28 20:03:16 THUDM/chatglm3-6b download :下载资源发生了错误,请使用正确的token

希望以上信息能有所帮助。再次感谢您的及时回复!

@little51
Copy link
Contributor

看一下r.status_code是多少?如果是200或206,r.headers.get('content-length')应该是有值的才对

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants