Spaces:
Running
on
Zero
Running
on
Zero
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,121 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from transformers import AutoTokenizer, AutoModel
|
| 3 |
-
from huggingface_hub import HfApi, HfFolder, create_repo, upload_folder
|
| 4 |
-
import os
|
| 5 |
-
import gradio as gr
|
| 6 |
-
|
| 7 |
-
def load_and_reupload_model(model_name, new_repo_id, hf_token, max_shard_size="1.5GB"):
|
| 8 |
-
"""
|
| 9 |
-
Loads a model and tokenizer, saves the model in smaller shards,
|
| 10 |
-
and uploads them to a new private repository on the Hugging Face Hub.
|
| 11 |
-
|
| 12 |
-
Args:
|
| 13 |
-
model_name (str): The name of the model to load from the Hugging Face Hub.
|
| 14 |
-
new_repo_id (str): The ID for the new private repository (e.g., "your-username/your-repo-name").
|
| 15 |
-
hf_token (str): Your Hugging Face API token with write permissions.
|
| 16 |
-
max_shard_size (str, optional): The maximum size of each model shard. Defaults to "1GB".
|
| 17 |
-
|
| 18 |
-
Returns:
|
| 19 |
-
A string log of the process.
|
| 20 |
-
"""
|
| 21 |
-
log_output = []
|
| 22 |
-
try:
|
| 23 |
-
# --- Validate inputs ---
|
| 24 |
-
if not all([model_name, new_repo_id, hf_token]):
|
| 25 |
-
return "β Error: All fields (Model Name, New Repo ID, and HF Token) are required."
|
| 26 |
-
|
| 27 |
-
# --- 1. Log in to Hugging Face Hub ---
|
| 28 |
-
HfFolder.save_token(hf_token)
|
| 29 |
-
log_output.append("β
Successfully saved Hugging Face token.")
|
| 30 |
-
|
| 31 |
-
# --- 2. Create a new private repository ---
|
| 32 |
-
create_repo(new_repo_id, private=True, exist_ok=True, token=hf_token)
|
| 33 |
-
log_output.append(f"β
Successfully created or confirmed private repository: {new_repo_id}")
|
| 34 |
-
|
| 35 |
-
# --- 3. Load the tokenizer and model ---
|
| 36 |
-
log_output.append(f"π Loading tokenizer for {model_name}...")
|
| 37 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 38 |
-
log_output.append("β
Tokenizer loaded successfully.")
|
| 39 |
-
|
| 40 |
-
log_output.append(f"π Loading model {model_name}...")
|
| 41 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 42 |
-
model = AutoModel.from_pretrained(
|
| 43 |
-
model_name,
|
| 44 |
-
trust_remote_code=True,
|
| 45 |
-
use_safetensors=True,
|
| 46 |
-
).to(device)
|
| 47 |
-
|
| 48 |
-
if device.type == 'cuda':
|
| 49 |
-
model = model.to(torch.bfloat16)
|
| 50 |
-
|
| 51 |
-
model.eval()
|
| 52 |
-
log_output.append(f"β
Model loaded successfully to {device} and set to evaluation mode.")
|
| 53 |
-
|
| 54 |
-
# --- 4. Save the model and tokenizer locally with sharding ---
|
| 55 |
-
local_save_dir = new_repo_id.split('/')[-1]
|
| 56 |
-
os.makedirs(local_save_dir, exist_ok=True)
|
| 57 |
-
|
| 58 |
-
log_output.append(f"π Saving model with max_shard_size='{max_shard_size}' to '{local_save_dir}'...")
|
| 59 |
-
model.save_pretrained(local_save_dir, max_shard_size=max_shard_size)
|
| 60 |
-
tokenizer.save_pretrained(local_save_dir)
|
| 61 |
-
log_output.append("β
Model and tokenizer saved locally.")
|
| 62 |
-
|
| 63 |
-
# --- 5. Upload the sharded model and tokenizer to the new repo ---
|
| 64 |
-
log_output.append(f"π Uploading files to {new_repo_id}...")
|
| 65 |
-
api = HfApi()
|
| 66 |
-
api.upload_folder(
|
| 67 |
-
folder_path=local_save_dir,
|
| 68 |
-
repo_id=new_repo_id,
|
| 69 |
-
repo_type="model",
|
| 70 |
-
token=hf_token
|
| 71 |
-
)
|
| 72 |
-
log_output.append(f"π Successfully uploaded model and tokenizer to private repo: {new_repo_id}")
|
| 73 |
-
|
| 74 |
-
except Exception as e:
|
| 75 |
-
log_output.append(f"β An error occurred: {e}")
|
| 76 |
-
|
| 77 |
-
return "\n".join(log_output)
|
| 78 |
-
|
| 79 |
-
# --- Gradio Interface ---
|
| 80 |
-
with gr.Blocks(theme="soft") as demo:
|
| 81 |
-
gr.Markdown(
|
| 82 |
-
"""
|
| 83 |
-
# π Hugging Face Model Sharder & Re-Uploader
|
| 84 |
-
This application loads a model from the Hugging Face Hub, saves it locally into smaller shards (e.g., 1GB each), and then uploads it to a new private repository under your account.
|
| 85 |
-
"""
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
with gr.Row():
|
| 89 |
-
with gr.Column(scale=2):
|
| 90 |
-
model_name_input = gr.Textbox(
|
| 91 |
-
label="Original Model Name",
|
| 92 |
-
value="strangervisionhf/deepseek-ocr-latest-transformers",
|
| 93 |
-
placeholder="e.g., 'strangervisionhf/deepseek-ocr-latest-transformers'"
|
| 94 |
-
)
|
| 95 |
-
new_repo_id_input = gr.Textbox(
|
| 96 |
-
label="New Private Repository ID",
|
| 97 |
-
placeholder="e.g., 'your-username/private-deepseek-ocr-sharded'"
|
| 98 |
-
)
|
| 99 |
-
hf_token_input = gr.Textbox(
|
| 100 |
-
label="Hugging Face Write Token",
|
| 101 |
-
type="password",
|
| 102 |
-
placeholder="Enter your Hugging Face token with write access"
|
| 103 |
-
)
|
| 104 |
-
run_button = gr.Button("Shard and Upload Model", variant="primary")
|
| 105 |
-
|
| 106 |
-
with gr.Column(scale=3):
|
| 107 |
-
output_log = gr.Textbox(
|
| 108 |
-
label="Process Log",
|
| 109 |
-
lines=15,
|
| 110 |
-
interactive=False,
|
| 111 |
-
autoscroll=True
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
run_button.click(
|
| 115 |
-
fn=load_and_reupload_model,
|
| 116 |
-
inputs=[model_name_input, new_repo_id_input, hf_token_input],
|
| 117 |
-
outputs=output_log
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
if __name__ == "__main__":
|
| 121 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|