Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import logging
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import List, Optional
|
| 4 |
-
import os
|
| 5 |
-
os.system('pip install modelscope -U')
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
from huggingface_hub import HfApi
|
|
@@ -62,14 +60,14 @@ class HFToMSConverter:
|
|
| 62 |
src.rename(dst)
|
| 63 |
logger.info(f"移动文件成功: {src.name}")
|
| 64 |
elif operation_type == "push":
|
| 65 |
-
ms_repo_id, clone_dir
|
| 66 |
self.ms_api.upload_folder(
|
| 67 |
repo_id=f"{self.config['username']}/{ms_repo_id}",
|
| 68 |
folder_path=clone_dir,
|
| 69 |
commit_message='upload dataset folder',
|
| 70 |
repo_type='dataset'
|
| 71 |
)
|
| 72 |
-
logger.info(f"
|
| 73 |
return True
|
| 74 |
except Exception as e:
|
| 75 |
logger.error(f"{operation_type}操作失败: {e}")
|
|
@@ -83,24 +81,27 @@ class HFToMSConverter:
|
|
| 83 |
Path(clone_dir) / filename
|
| 84 |
)
|
| 85 |
|
| 86 |
-
def push_to_ms(self, ms_repo_id: str, clone_dir: str
|
| 87 |
"""推送到ModelScope"""
|
| 88 |
-
return self.handle_file_operation("push", ms_repo_id, clone_dir
|
| 89 |
|
| 90 |
-
def
|
| 91 |
-
"""
|
| 92 |
clone_dir = ms_repo.split("/")[-1]
|
| 93 |
|
| 94 |
try:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
-
logger.error(f"
|
| 104 |
return False
|
| 105 |
|
| 106 |
def create_ui() -> gr.Blocks:
|
|
@@ -136,9 +137,7 @@ def create_ui() -> gr.Blocks:
|
|
| 136 |
|
| 137 |
converter = HFToMSConverter(config)
|
| 138 |
files = converter.get_hf_files(hf_repo, repo_type)
|
| 139 |
-
|
| 140 |
-
for filename in files:
|
| 141 |
-
converter.process_file(hf_repo, ms_repo, filename)
|
| 142 |
|
| 143 |
submit.click(
|
| 144 |
handle_submit,
|
|
|
|
| 1 |
import logging
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import List, Optional
|
|
|
|
|
|
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import HfApi
|
|
|
|
| 60 |
src.rename(dst)
|
| 61 |
logger.info(f"移动文件成功: {src.name}")
|
| 62 |
elif operation_type == "push":
|
| 63 |
+
ms_repo_id, clone_dir = args
|
| 64 |
self.ms_api.upload_folder(
|
| 65 |
repo_id=f"{self.config['username']}/{ms_repo_id}",
|
| 66 |
folder_path=clone_dir,
|
| 67 |
commit_message='upload dataset folder',
|
| 68 |
repo_type='dataset'
|
| 69 |
)
|
| 70 |
+
logger.info(f"推送文件夹成功: {clone_dir}")
|
| 71 |
return True
|
| 72 |
except Exception as e:
|
| 73 |
logger.error(f"{operation_type}操作失败: {e}")
|
|
|
|
| 81 |
Path(clone_dir) / filename
|
| 82 |
)
|
| 83 |
|
| 84 |
+
def push_to_ms(self, ms_repo_id: str, clone_dir: str) -> bool:
|
| 85 |
"""推送到ModelScope"""
|
| 86 |
+
return self.handle_file_operation("push", ms_repo_id, clone_dir)
|
| 87 |
|
| 88 |
+
def process_files(self, hf_repo: str, ms_repo: str, files: List[str]) -> bool:
|
| 89 |
+
"""处理所有文件的完整流程"""
|
| 90 |
clone_dir = ms_repo.split("/")[-1]
|
| 91 |
|
| 92 |
try:
|
| 93 |
+
# 下载并移动所有文件
|
| 94 |
+
for filename in files:
|
| 95 |
+
if not all([
|
| 96 |
+
self.download_file(hf_repo, filename),
|
| 97 |
+
self.move_file(filename, clone_dir)
|
| 98 |
+
]):
|
| 99 |
+
return False
|
| 100 |
+
|
| 101 |
+
# 统一推送整个文件夹
|
| 102 |
+
return self.push_to_ms(ms_repo, clone_dir)
|
| 103 |
except Exception as e:
|
| 104 |
+
logger.error(f"处理文件失败: {e}")
|
| 105 |
return False
|
| 106 |
|
| 107 |
def create_ui() -> gr.Blocks:
|
|
|
|
| 137 |
|
| 138 |
converter = HFToMSConverter(config)
|
| 139 |
files = converter.get_hf_files(hf_repo, repo_type)
|
| 140 |
+
converter.process_files(hf_repo, ms_repo, files)
|
|
|
|
|
|
|
| 141 |
|
| 142 |
submit.click(
|
| 143 |
handle_submit,
|