Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,102 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import spaces
|
4 |
import shutil
|
5 |
import logging
|
6 |
import subprocess
|
7 |
from pathlib import Path
|
|
|
|
|
8 |
|
9 |
@spaces.GPU
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
outpath = Path('/tmp/output')
|
17 |
-
write_repo(base_model, model_to_merge)
|
18 |
-
|
19 |
-
# Construct the command to run hf_merge.py
|
20 |
-
command = [
|
21 |
-
"python3", "hf_merge.py",
|
22 |
-
"-p", str(weight_drop_prob),
|
23 |
-
"-lambda", str(scaling_factor),
|
24 |
-
"repo.txt", str(outpath)
|
25 |
-
]
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
#
|
42 |
-
if
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
api = HfApi(token=token)
|
50 |
-
try:
|
51 |
-
# Get the username of the user who is logged in
|
52 |
-
user = api.whoami(token=token)["name"]
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
)
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
return None, f"Error uploading to Hugging Face Hub: {str(e)}", log_output
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
with gr.Row():
|
86 |
-
token = gr.Textbox(label="Your HF write token", placeholder="hf_...", value="", max_lines=1)
|
87 |
-
with gr.Row():
|
88 |
-
base_model = gr.Textbox(label="Base Model", placeholder=".safetensors")
|
89 |
-
with gr.Row():
|
90 |
-
model_to_merge = gr.Textbox(label="Merge Model", placeholder=".bin/.safetensors")
|
91 |
-
with gr.Row():
|
92 |
-
repo_name = gr.Textbox(label="New Model", placeholder="SDXL-", info="If empty, auto-complete", value="", max_lines=1)
|
93 |
-
with gr.Row():
|
94 |
-
scaling_factor = gr.Slider(minimum=0, maximum=10, value=3.0, label="Scaling Factor")
|
95 |
-
with gr.Row():
|
96 |
-
weight_drop_prob = gr.Slider(minimum=0, maximum=1, value=0.3, label="Weight Drop Probability")
|
97 |
-
gr.Button("Merge").click(
|
98 |
-
merge_and_upload,
|
99 |
-
inputs=[base_model, model_to_merge, scaling_factor, weight_drop_prob, repo_name, token]
|
100 |
-
)
|
101 |
|
102 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
|
|
3 |
import shutil
|
4 |
import logging
|
5 |
import subprocess
|
6 |
from pathlib import Path
|
7 |
+
from merge_script import ModelMerger, get_max_vocab_size, download_json_files
|
8 |
+
import spaces
|
9 |
|
10 |
@spaces.GPU
|
11 |
+
def merge_models(base_model, model_to_merge, scaling_factor, weight_drop_prob, repo_name, token, commit_message):
|
12 |
+
# Define staging and output paths
|
13 |
+
staging_path = "/tmp/staging"
|
14 |
+
output_path = "/tmp/output"
|
15 |
+
os.makedirs(staging_path, exist_ok=True)
|
16 |
+
os.makedirs(output_path, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Initialize ModelMerger and prepare base model
|
19 |
+
model_merger = ModelMerger(staging_path, repo_name, token)
|
20 |
+
model_merger.prepare_base_model(base_model, os.path.join(staging_path, "base_model"))
|
21 |
|
22 |
+
# Merge models and handle progress updates
|
23 |
+
for repo_name in [base_model, model_to_merge]:
|
24 |
+
model_merger.merge_repo(repo_name, os.path.join(staging_path, "staging_model"), weight_drop_prob, scaling_factor)
|
25 |
+
yield 0.25, f"Merged {repo_name}"
|
26 |
|
27 |
+
# Finalize merge and handle vocab size
|
28 |
+
model_merger.finalize_merge(output_path)
|
29 |
+
yield 0.5, "Finalizing merge and handling vocab size..."
|
30 |
+
max_vocab_size, repo_with_max_vocab = get_max_vocab_size([base_model, model_to_merge])
|
31 |
+
if max_vocab_size > 0:
|
32 |
+
download_json_files(repo_with_max_vocab, ['config.json', 'special_tokens_map.json', 'tokenizer.json', 'tokenizer_config.json'], output_path)
|
33 |
|
34 |
+
# Upload merged model to Hugging Face Hub
|
35 |
+
if repo_name:
|
36 |
+
model_merger.upload_model(output_path, repo_name, commit_message)
|
37 |
+
yield 0.75, "Uploading merged model to Hugging Face Hub..."
|
38 |
+
repo_url = f"https://huggingface.co/{repo_name}"
|
39 |
+
yield 1.0, f"Model merged and uploaded successfully! {repo_url}"
|
40 |
+
else:
|
41 |
+
yield 1.0, "Model merged successfully! No upload performed."
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
def get_model_type_info(model_name):
|
44 |
+
model_types = {
|
45 |
+
"base_model": "Base model should be in .safetensors format.",
|
46 |
+
"model_to_merge": "Model to merge can be in .safetensors or .bin format."
|
47 |
+
}
|
48 |
+
return model_types.get(model_name, "No specific info available.")
|
49 |
|
50 |
+
def validate_model_format(model_name, model_path):
|
51 |
+
if model_name == "base_model":
|
52 |
+
if not model_path.endswith(".safetensors"):
|
53 |
+
return False, "Base model must be in .safetensors format."
|
54 |
+
elif model_name == "model_to_merge":
|
55 |
+
if not model_path.endswith((".safetensors", ".bin")):
|
56 |
+
return False, "Model to merge must be in .safetensors or .bin format."
|
57 |
+
return True, None
|
58 |
|
59 |
+
def merge_and_upload_interface():
|
60 |
+
with gr.Blocks(theme="Ytheme/XRainbow", fill_width=True) as demo:
|
61 |
+
gr.Markdown("# Model Merger and Uploader")
|
62 |
+
gr.Markdown("Combine and upload models with real-time progress updates.")
|
63 |
+
gr.Markdown("**Model Compatibility:**")
|
64 |
+
gr.Markdown("Combine any two models using a Super Mario merge.")
|
65 |
+
gr.Markdown("Works with:")
|
66 |
+
gr.Markdown("* Stable Diffusion (1.5, XL/XL Turbo)")
|
67 |
+
gr.Markdown("* LLMs (Mistral, Llama, etc)")
|
68 |
+
gr.Markdown("* LoRas (must be same size)")
|
69 |
+
gr.Markdown("* Any two homologous models")
|
|
|
70 |
|
71 |
+
with gr.Column():
|
72 |
+
token = gr.Textbox(label="HuggingFace Token")
|
73 |
+
base_model = gr.Textbox(label="Base Model")
|
74 |
+
base_model_info = gr.HTML(get_model_type_info("base_model"))
|
75 |
+
model_to_merge = gr.Textbox(label="Model to Merge")
|
76 |
+
model_to_merge_info = gr.HTML(get_model_type_info("model_to_merge"))
|
77 |
+
repo_name = gr.Textbox(label="New Model Name")
|
78 |
+
scaling_factor = gr.Slider(minimum=0, maximum=10, label="Scaling Factor")
|
79 |
+
weight_drop_prob = gr.Slider(minimum=0, maximum=1, label="Weight Drop Probability")
|
80 |
+
gr.Button("Merge and Upload").click(
|
81 |
+
merge_models,
|
82 |
+
inputs=[base_model, model_to_merge, scaling_factor, weight_drop_prob, repo_name, token, commit_message],
|
83 |
+
outputs=[
|
84 |
+
gr.Textbox(label="Progress/Output"),
|
85 |
+
gr.Textbox(label="Log")
|
86 |
+
],
|
87 |
+
pre_process=validate_model_format
|
88 |
+
)
|
89 |
|
90 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
if __name__ == "__main__":
|
93 |
+
merge_and_upload_interface()
|