aistudio_TTS / main.py
Qi28's picture
Update main.py
5324b8b verified
# /home/aistudio/work
print('----远程执行-----')
import os
import subprocess
import sys
import configparser
import shutil
import openi
# openi 数据下载 数据集必须为公开数据集
openi_datasets = [
# {
# "dataset_name": "qilan2/SD-QL",# 数据集路径
# "file_name": "SD模型3.zip",# 数据集名
# "save_path": "/tmp/code/sd/stable-diffusion-webui/models/Stable-diffusion/",# 解压路径结尾带/
# "extract": True, # 是否解压文件 True False
# "use_unzip": True # unzip解压 True 7z解压 False
# },
# {
# "dataset_name": "qilan2/qilan_TTS",
# "file_name": "tts-mode.zip",
# "save_path": "/home/aistudio/work/",
# "extract": True,
# "use_unzip": True
# }
]
mode_url = [
#'文件名:URL'
# 'tts1.zip:https://open-data.obs.cn-south-222.ai.pcl.cn:443/attachment/c/e/ce3364d5-e727-4ff3-8438-be5b4f6067de/tts1.zip?response-content-disposition=attachment%3B+filename%3D%22tts1.zip%22&AWSAccessKeyId=ZSCXA9TLRN1USYWIF7A5&Expires=1733754134&Signature=wcy5vx%2FYYgRsCaz4HTK1rV7oGAE%3D'
]
# 安装环境
# os.system('apt install aria2 -y')
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)