Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import HfApi, login
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
@spaces.GPU
|
| 7 |
+
def upload_file_to_hf(file_path):
|
| 8 |
+
"""
|
| 9 |
+
将ZIP文件上传到TalkTalkAI/RVC Hugging Face模型仓库。
|
| 10 |
+
|
| 11 |
+
参数:
|
| 12 |
+
file_path (str): 要上传的ZIP文件路径
|
| 13 |
+
|
| 14 |
+
返回:
|
| 15 |
+
str: 成功或错误消息
|
| 16 |
+
"""
|
| 17 |
+
try:
|
| 18 |
+
# 从环境变量获取API令牌
|
| 19 |
+
api_token = os.environ.get("hf_token")
|
| 20 |
+
if not api_token:
|
| 21 |
+
return "错误: 未设置hf_token环境变量"
|
| 22 |
+
|
| 23 |
+
# 设置仓库ID
|
| 24 |
+
repo_id = "TalkTalkAI/RVC"
|
| 25 |
+
|
| 26 |
+
# Log in to Hugging Face
|
| 27 |
+
login(token=api_token, add_to_git_credential=False)
|
| 28 |
+
|
| 29 |
+
# Initialize the Hugging Face API
|
| 30 |
+
api = HfApi()
|
| 31 |
+
|
| 32 |
+
# 获取文件名并确保是ZIP文件
|
| 33 |
+
filename = os.path.basename(file_path)
|
| 34 |
+
if not filename.lower().endswith('.zip'):
|
| 35 |
+
return "错误: 仅支持ZIP格式文件"
|
| 36 |
+
|
| 37 |
+
# 在文件名中添加时间戳
|
| 38 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 39 |
+
base_name = os.path.splitext(filename)[0]
|
| 40 |
+
path_in_repo = f"{base_name}_{timestamp}.zip"
|
| 41 |
+
|
| 42 |
+
# 创建提交消息
|
| 43 |
+
commit_message = f"上传模型 {path_in_repo}"
|
| 44 |
+
|
| 45 |
+
# Upload the file
|
| 46 |
+
api.upload_file(
|
| 47 |
+
path_or_fileobj=file_path,
|
| 48 |
+
path_in_repo=path_in_repo,
|
| 49 |
+
repo_id=repo_id,
|
| 50 |
+
commit_message=commit_message
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return f"成功上传 {path_in_repo} 到 {repo_id}"
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"上传文件时出错: {str(e)}"
|
| 57 |
+
|
| 58 |
+
# Create the Gradio interface
|
| 59 |
+
with gr.Blocks(title="RVC模型上传") as demo:
|
| 60 |
+
gr.Markdown("# RVC模型上传工具")
|
| 61 |
+
gr.Markdown("""
|
| 62 |
+
将ZIP文件上传到书梦 AI 歌手模型仓库。
|
| 63 |
+
|
| 64 |
+
上传文件:
|
| 65 |
+
从本地上传您的 AI 歌手 ZIP 文件,其中包含 .pth 和 .index 模型文件
|
| 66 |
+
|
| 67 |
+
返回结果:
|
| 68 |
+
您的 AI 歌手 ZIP 文件下载链接,可直接在[书梦](https://www.doingdream.com/)使用
|
| 69 |
+
""")
|
| 70 |
+
|
| 71 |
+
with gr.Group():
|
| 72 |
+
file = gr.File(label="选择要上传的模型ZIP文件", file_types=[".zip"])
|
| 73 |
+
|
| 74 |
+
upload_button = gr.Button("上传模型", variant="primary")
|
| 75 |
+
result = gr.Textbox(label="上传结果", interactive=False, lines=4)
|
| 76 |
+
|
| 77 |
+
# 定义上传函数(适用于Gradio)
|
| 78 |
+
def gradio_upload(file):
|
| 79 |
+
if not file:
|
| 80 |
+
return "错误: 未选择任何文件"
|
| 81 |
+
|
| 82 |
+
if not file.name.lower().endswith('.zip'):
|
| 83 |
+
return "错误: 仅支持ZIP格式文件"
|
| 84 |
+
|
| 85 |
+
result = upload_file_to_hf(file.name)
|
| 86 |
+
|
| 87 |
+
# 检查是否上传成功并包含URL
|
| 88 |
+
if "成功上传" in result:
|
| 89 |
+
# 使结果更易于复制粘贴
|
| 90 |
+
gr.Info("上传成功!模型链接已生成")
|
| 91 |
+
|
| 92 |
+
return result
|
| 93 |
+
|
| 94 |
+
# Connect the button to the function
|
| 95 |
+
upload_button.click(
|
| 96 |
+
fn=gradio_upload,
|
| 97 |
+
inputs=[file],
|
| 98 |
+
outputs=result
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Launch the app
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
demo.launch()
|