Spaces:
Running
Running
import os | |
import torch | |
import requests | |
from modelscope import snapshot_download | |
MODEL_DIR = snapshot_download( | |
"ccmusic-database/music_genre", | |
cache_dir="./__pycache__", | |
) | |
def toCUDA(x): | |
if hasattr(x, "cuda"): | |
if torch.cuda.is_available(): | |
return x.cuda() | |
return x | |
def find_mp3_files(folder_path=f"{MODEL_DIR}/examples"): | |
wav_files = [] | |
for root, _, files in os.walk(folder_path): | |
for file in files: | |
if file.endswith(".mp3"): | |
file_path = os.path.join(root, file) | |
wav_files.append(file_path) | |
return wav_files | |
def get_modelist(model_dir=MODEL_DIR): | |
try: | |
entries = os.listdir(model_dir) | |
except OSError as e: | |
print(f"无法访问 {model_dir}: {e}") | |
return | |
# 遍历所有条目 | |
output = [] | |
for entry in entries: | |
# 获取完整路径 | |
full_path = os.path.join(model_dir, entry) | |
# 跳过'.git'文件夹 | |
if entry == ".git" or entry == "examples": | |
print(f"跳过 .git / examples 文件夹: {full_path}") | |
continue | |
# 检查条目是文件还是目录 | |
if os.path.isdir(full_path): | |
# 打印目录路径 | |
output.append(os.path.basename(full_path)) | |
return output | |
def download(url: str): | |
filename = url.split("/")[-1] | |
response = requests.get(url, stream=True) | |
if response.status_code == 200: | |
with open(filename, "wb") as f: | |
for chunk in response.iter_content(chunk_size=8192): | |
f.write(chunk) | |
print(f"文件已下载到 {os.getcwd()}/{filename}") | |
else: | |
print(f"下载失败,状态码:{response.status_code}") | |