julien-c HF staff commited on
Commit
cde73c1
1 Parent(s): 049ec21

simplify + only handle yaml generation

Browse files
Files changed (2) hide show
  1. .vscode/settings.json +2 -1
  2. app.py +77 -133
.vscode/settings.json CHANGED
@@ -1,3 +1,4 @@
1
  {
2
- "editor.formatOnSave": true
 
3
  }
 
1
  {
2
+ "editor.formatOnSave": true,
3
+ "editor.defaultFormatter": "ms-python.black-formatter"
4
  }
app.py CHANGED
@@ -1,11 +1,23 @@
1
  import gradio as gr
2
- import os
3
- from huggingface_hub import HfApi
4
- import subprocess
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
 
7
  def create_config_yaml(
8
- model_name,
9
  model1,
10
  model1_layers,
11
  model2,
@@ -14,140 +26,72 @@ def create_config_yaml(
14
  base_model,
15
  parameters,
16
  dtype,
17
- ):
18
- yaml_config = (
19
- f" slices:\n"
20
- " - sources:\n"
21
- f" - model: {model1}\n"
22
- f" layer_range: {model1_layers}\n"
23
- f" - model: {model2}\n"
24
- f" layer_range: {model2_layers}\n"
25
- f" merge_method: {merge_method}\n"
26
- f" base_model: {base_model}\n"
27
- f" parameters:\n"
28
- f" {parameters}\n"
29
- f" dtype: {dtype}\n"
30
- )
31
- print("Writing YAML config to 'config.yaml'...")
32
-
33
- try:
34
- with open("config.yaml", "w", encoding="utf-8") as f:
35
- f.write(yaml_config)
36
- print("File 'config.yaml' written successfully.")
37
- except Exception as e:
38
- print(f"Error writing file: {e}")
39
- return yaml_config
40
-
41
-
42
- def execute_merge_command():
43
- # Define the command and arguments
44
- command = "mergekit-yaml"
45
- args = ["config.yaml", "./output-model-directory"]
46
-
47
- # Execute the command
48
- result = subprocess.run([command] + args, capture_output=True, text=True)
49
-
50
- # Check if the command was executed successfully
51
- if result.returncode == 0:
52
- print("Command executed successfully")
53
- return f"Output:\n{result.stdout}"
54
- else:
55
- print("Error in executing command")
56
- return f"Error:\n{result.stderr}"
57
-
58
-
59
- # Function to push to HF Hub (for the third tab)
60
- def push_to_hf_hub(model_name, yaml_config):
61
- # Username and API token setup
62
- username = "arcee-ai"
63
- api_token = os.getenv("HF_TOKEN")
64
- if api_token is None:
65
- return "Hugging Face API token not set. Please set the HF_TOKEN environment variable."
66
-
67
- # Initialize HfApi with token
68
- api = HfApi(token=api_token)
69
-
70
- repo_id = f"{username}/{model_name}"
71
-
72
- try:
73
- # Create a new repository on Hugging Face
74
- api.create_repo(repo_id=repo_id, repo_type="model")
75
-
76
- # For demonstration, let's just create a yaml file inside a folder
77
- # os.makedirs("merge", exist_ok=True)
78
- with open("config.yaml", "w") as file:
79
- file.write(yaml_config)
80
-
81
- # Upload the contents of the 'merge' folder to the repository
82
- api.upload_folder(repo_id=repo_id, folder_path="merge")
83
-
84
- return f"Successfully pushed to HF Hub: {repo_id}"
85
- except Exception as e:
86
- return str(e)
87
 
88
 
89
  # make sure to add the themes as well
90
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo")) as app:
91
- gr.Markdown("# Mergekit GUI") # Title for your Gradio app
92
- with gr.Tab("Config YAML"):
93
- # Inputs for the YAML config
94
- with gr.Row():
95
- model_name_input = gr.Textbox(label="Model Name")
96
- model1_input = gr.Textbox(label="Model 1")
97
- model1_layers_input = gr.Textbox(
98
- label="Model 1 Layer Range", placeholder="[start, end]"
99
- )
100
- model2_input = gr.Textbox(label="Model 2")
101
- model2_layers_input = gr.Textbox(
102
- label="Model 2 Layer Range", placeholder="[start, end]"
103
- )
104
- merge_method_input = gr.Dropdown(
105
- label="Merge Method", choices=["slerp", "linear"]
106
  )
107
- base_model_input = gr.Textbox(label="Base Model")
108
- parameters_input = gr.Textbox(
109
- label="Parameters", placeholder="Formatted as a list of dicts"
110
  )
111
- dtype_input = gr.Textbox(label="Data Type", value="bfloat16")
112
-
113
- create_button = gr.Button("Create Config YAML")
114
-
115
- create_button.click(
116
- fn=create_config_yaml,
117
- inputs=[
118
- model_name_input,
119
- model1_input,
120
- model1_layers_input,
121
- model2_input,
122
- model2_layers_input,
123
- merge_method_input,
124
- base_model_input,
125
- parameters_input,
126
- dtype_input,
127
- ],
128
- outputs=[],
129
- )
130
-
131
- with gr.Tab("Merge"):
132
- # Placeholder for Merge tab contents
133
- # Not yet tested
134
-
135
- merge_output = gr.Textbox(label="Merge Output", interactive=False)
136
- merge_button = gr.Button("Execute Merge Command")
137
-
138
- merge_button.click(fn=execute_merge_command, inputs=[], outputs=merge_output)
139
-
140
- with gr.Tab("Push to HF Hub"):
141
- push_model_name_input = gr.Textbox(label="Model Name", interactive=False)
142
- push_yaml_config_input = gr.Textbox(label="YAML Config", interactive=False)
143
- push_output = gr.Textbox(label="Push Output", interactive=False)
144
- push_button = gr.Button("Push to HF Hub")
145
-
146
- push_button.click(
147
- fn=push_to_hf_hub,
148
- inputs=[push_model_name_input, push_yaml_config_input],
149
- outputs=push_output,
150
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
 
153
- app.launch()
 
1
  import gradio as gr
2
+ import yaml
3
+
4
+ MARKDOWN_DESCRIPTION = """
5
+ # mergekit config.yaml generator
6
+
7
+ GUI to template a YAML configuration file for mergekit, which you can then copy/paste into [mergekit-gui](https://huggingface.co/spaces/arcee-ai/mergekit-gui) 🔥
8
+ """
9
+
10
+ DEFAULT_PARAMETERS = """
11
+ t:
12
+ - filter: self_attn
13
+ value: [0, 0.5, 0.3, 0.7, 1]
14
+ - filter: mlp
15
+ value: [1, 0.5, 0.7, 0.3, 0]
16
+ - value: 0.5
17
+ """
18
 
19
 
20
  def create_config_yaml(
 
21
  model1,
22
  model1_layers,
23
  model2,
 
26
  base_model,
27
  parameters,
28
  dtype,
29
+ ) -> str:
30
+ dict_config = {
31
+ "slices": [
32
+ {
33
+ "sources": [
34
+ {"model": model1, "layer_range": yaml.safe_load(model1_layers)},
35
+ {"model": model2, "layer_range": yaml.safe_load(model2_layers)},
36
+ ]
37
+ }
38
+ ],
39
+ "merge_method": merge_method,
40
+ "base_model": base_model,
41
+ }
42
+
43
+ if parameters:
44
+ dict_config["parameters"] = yaml.safe_load(parameters)
45
+ if dtype:
46
+ dict_config["dtype"] = dtype
47
+
48
+ return yaml.dump(dict_config, sort_keys=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
 
51
  # make sure to add the themes as well
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown(MARKDOWN_DESCRIPTION)
54
+ with gr.Row():
55
+ # model_name_input = gr.Textbox(label="Model Name", value="my-merge")
56
+ model1_input = gr.Textbox(label="Model 1", value="BioMistral/BioMistral-7B")
57
+ model1_layers_input = gr.Textbox(
58
+ label="Model 1 Layer Range", placeholder="[start, end]", value="[0, 32]"
 
 
 
 
 
 
 
 
 
59
  )
60
+ model2_input = gr.Textbox(
61
+ label="Model 2", value="CorticalStack/pastiche-crown-clown-7b-dare-dpo"
 
62
  )
63
+ model2_layers_input = gr.Textbox(
64
+ label="Model 2 Layer Range", placeholder="[start, end]", value="[0, 32]"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  )
66
+ merge_method_input = gr.Dropdown(
67
+ label="Merge Method", choices=["slerp", "linear"], value="slerp"
68
+ )
69
+ base_model_input = gr.Textbox(label="Base Model", value="BioMistral/BioMistral-7B")
70
+ parameters_input = gr.Code(
71
+ language="yaml",
72
+ label="Merge Parameters",
73
+ value=DEFAULT_PARAMETERS,
74
+ )
75
+ dtype_input = gr.Textbox(label="Dtype", value="bfloat16")
76
+
77
+ create_button = gr.Button("Create config.yaml", variant="primary")
78
+
79
+ output_zone = gr.Code(language="yaml", lines=10)
80
+
81
+ create_button.click(
82
+ fn=create_config_yaml,
83
+ inputs=[
84
+ model1_input,
85
+ model1_layers_input,
86
+ model2_input,
87
+ model2_layers_input,
88
+ merge_method_input,
89
+ base_model_input,
90
+ parameters_input,
91
+ dtype_input,
92
+ ],
93
+ outputs=[output_zone],
94
+ )
95
 
96
 
97
+ demo.launch()