mrcuddle commited on
Commit
e69d387
·
verified ·
1 Parent(s): 33e9d70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -85
app.py CHANGED
@@ -1,102 +1,93 @@
1
  import gradio as gr
2
- from huggingface_hub import HfApi
3
- import spaces
4
  import shutil
5
  import logging
6
  import subprocess
7
  from pathlib import Path
 
 
8
 
9
  @spaces.GPU
10
- def write_repo(base_model, model_to_merge):
11
- with open("repo.txt", "w") as repo:
12
- repo.write(base_model + "\n" + model_to_merge)
13
-
14
- def merge_and_upload(base_model, model_to_merge, scaling_factor, weight_drop_prob, repo_name, token):
15
- # Define a fixed output path
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
- # Set up logging
28
- logging.basicConfig(level=logging.INFO)
29
- log_output = ""
30
 
31
- # Run the command and capture the output
32
- result = subprocess.run(command, capture_output=True, text=True)
33
-
 
34
 
35
- # Log the output
36
- log_output += result.stdout + "\n"
37
- log_output += result.stderr + "\n"
38
- logging.info(result.stdout)
39
- logging.error(result.stderr)
 
40
 
41
- # Check if the merge was successful
42
- if result.returncode != 0:
43
- return None, f"Error in merging models: {result.stderr}", log_output
44
-
45
- # Update progress bar
46
- yield 0.5, "Merging completed. Uploading to Hugging Face Hub..."
47
-
48
- # Upload the result to Hugging Face Hub
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
- # Autofill the repo name if none is provided
55
- if not repo_name:
56
- repo_name = f"{user}/default-repo"
 
 
 
57
 
58
- # Create a new repo or update an existing one
59
- api.create_repo(repo_id=repo_name, token=token, exist_ok=True)
 
 
 
 
 
 
60
 
61
- # Upload the file
62
- api.upload_folder(
63
- folder_path=str(outpath),
64
- repo_id=repo_name,
65
- repo_type="model",
66
- token=token
67
- )
68
- repo_url = f"https://huggingface.co/{repo_name}"
69
- yield 1.0, "Upload completed."
70
- return repo_url, "Model merged and uploaded successfully!", log_output
71
- except Exception as e:
72
- return None, f"Error uploading to Hugging Face Hub: {str(e)}", log_output
73
 
74
- # Define the Gradio interface
75
- with gr.Blocks(theme="Ytheme/XRainbow", fill_width=True, delete_cache=(60, 3600)) as demo:
76
- gr.Markdown("# Model Merger and Uploader")
77
- gr.Markdown("Combine any two models using a Super Mario merge(DARE) as described in the linked whitepaper.")
78
- gr.Markdown("Works with:")
79
- gr.Markdown("* Stable Diffusion (1.5, XL/XL Turbo)")
80
- gr.Markdown("* LLMs (Mistral, Llama, etc)")
81
- gr.Markdown("* LoRas (must be same size)")
82
- gr.Markdown("* Any two homologous models")
 
 
 
 
 
 
 
 
 
83
 
84
- with gr.Column():
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
- demo.launch()
 
 
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()