|
|
|
print('----远程执行-----') |
|
import os |
|
import subprocess |
|
import sys |
|
import configparser |
|
import shutil |
|
import openi |
|
|
|
|
|
openi_datasets = [ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
] |
|
mode_url = [ |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
def download_mode(file_urls, download_dir='/home/aistudio/work'): |
|
os.makedirs(download_dir, exist_ok=True) |
|
for file_url in file_urls: |
|
try: |
|
file_name, url = file_url.split(':', 1) |
|
download_wget(file_name, download_dir, url) |
|
except ValueError: |
|
print(f'无效的文件 URL 格式: {file_url}') |
|
|
|
def download_wget(file_name, download_dir, url): |
|
download_path = os.path.join(download_dir, file_name) |
|
|
|
|
|
if os.path.exists(download_path): |
|
print(f'文件 {file_name} 已存在, URL: {url}') |
|
return |
|
|
|
|
|
wget_args = ( |
|
f'wget -c --directory-prefix={download_dir} ' |
|
'--tries=5 --timeout=60 --waitretry=30 ' |
|
'--read-timeout=60 --continue ' |
|
'{url} -O {file_name}' |
|
) |
|
try: |
|
os.system(wget_args.format(url=url, file_name=file_name)) |
|
print(f'已成功下载: {file_name}') |
|
except Exception as e: |
|
print(f'下载 {url} 失败: {e}') |
|
|
|
def download_openi(datasets): |
|
for dataset in datasets: |
|
dataset_name = dataset["dataset_name"] |
|
file_name = dataset["file_name"] |
|
save_path = os.path.join(".", dataset["save_path"]) |
|
extract = dataset.get("extract", True) |
|
use_unzip = dataset.get("use_unzip", True) |
|
|
|
if not os.path.exists(save_path): |
|
os.makedirs(save_path) |
|
os.chdir(save_path) |
|
|
|
config = configparser.ConfigParser() |
|
config.read('time.ini') |
|
print('执行指令',f'openi dataset download {dataset_name} {file_name} --cluster NPU --save_path {save_path}') |
|
if config.has_option('downloaded_files', file_name): |
|
print(f"配置文件中已存在 {file_name},跳过下载。") |
|
continue |
|
|
|
|
|
openi.download_file( |
|
repo_id=dataset_name, |
|
file=file_name, |
|
cluster="npu", |
|
save_path=save_path, |
|
force=True, |
|
) |
|
|
|
|
|
if not os.path.exists(file_name): |
|
print(f"下载 {file_name} 失败,跳过后续步骤。") |
|
continue |
|
|
|
print(f"数据集 {file_name} 已下载到 {save_path} 目录。") |
|
|
|
|
|
if not config.has_section('downloaded_files'): |
|
config.add_section('downloaded_files') |
|
config.set('downloaded_files', file_name, 'True') |
|
with open('time.ini', 'w') as configfile: |
|
config.write(configfile) |
|
|
|
|
|
if extract: |
|
extract_path = os.path.join(save_path, os.path.splitext(file_name)[0]) |
|
if not os.path.exists(extract_path): |
|
os.makedirs(extract_path) |
|
if use_unzip: |
|
os.system(f'unzip -j {file_name} -d {extract_path}') |
|
else: |
|
os.system(f'7z x {file_name} -o{extract_path} -aoa') |
|
|
|
print(f"数据集 {file_name} 已解压到 {save_path} 目录。") |
|
|
|
|
|
os.remove(file_name) |
|
print(f"已删除压缩包文件 {file_name}。") |
|
|
|
|
|
|
|
download_openi(openi_datasets) |
|
download_mode(mode_url) |
|
|