diff --git a/crazy_functions/Latex_Function.py b/crazy_functions/Latex_Function.py index ba03cf126..d8773b04f 100644 --- a/crazy_functions/Latex_Function.py +++ b/crazy_functions/Latex_Function.py @@ -106,7 +106,31 @@ def is_float(s): return True except ValueError: return False + def download_file_withprocess(url, filename): + from tqdm import tqdm + # 发送请求获取文件 + proxies = get_conf('proxies') + headers = { + 'User-Agent': 'Lynx' + } + response = requests.get(url, headers=headers, proxies=proxies, stream=True) # 增加stream=True参数 + + total_size = int(response.headers.get('content-length', 0)) # 获取文件总大小 + block_size = 1024 # 每次下载的块大小 + + # 使用 tqdm 创建进度条 + with open(filename, 'wb') as file, tqdm( + desc=filename, + total=total_size, + unit='iB', + unit_scale=True, + unit_divisor=1024, + ) as bar: + for data in response.iter_content(block_size): + file.write(data) # 写入文件 + bar.update(len(data)) # 更新进度条 + if txt.startswith('https://arxiv.org/pdf/'): arxiv_id = txt.split('/')[-1] # 2402.14207v2.pdf txt = arxiv_id.split('v')[0] # 2402.14207 @@ -147,10 +171,11 @@ def is_float(s): yield from update_ui_lastest_msg("调用缓存", chatbot=chatbot, history=history) # 刷新界面 else: yield from update_ui_lastest_msg("开始下载", chatbot=chatbot, history=history) # 刷新界面 - proxies = get_conf('proxies') - r = requests.get(url_tar, proxies=proxies) - with open(dst, 'wb+') as f: - f.write(r.content) + # proxies = get_conf('proxies') + # r = requests.get(url_tar, proxies=proxies) + # with open(dst, 'wb+') as f: + # f.write(r.content) + download_file_withprocess(url=url_tar, filename=dst) # <-------------- extract file -------------> yield from update_ui_lastest_msg("下载完成", chatbot=chatbot, history=history) # 刷新界面 from toolbox import extract_archive @@ -545,4 +570,4 @@ def PDF翻译中文并重新编译PDF(txt, llm_kwargs, plugin_kwargs, chatbot, h promote_file_to_downloadzone(file=zip_res, chatbot=chatbot) # <-------------- we are done -------------> - return success \ No newline at end of file + return success