Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import random
|
|
| 4 |
import string
|
| 5 |
import tempfile
|
| 6 |
import time
|
| 7 |
-
|
| 8 |
from typing import Iterable, List
|
| 9 |
|
| 10 |
import gradio as gr
|
|
@@ -13,7 +13,7 @@ import torch
|
|
| 13 |
import yaml
|
| 14 |
from gradio_logsview.logsview import Log, LogsView, LogsViewRunner
|
| 15 |
from mergekit.config import MergeConfiguration
|
| 16 |
-
import spaces
|
| 17 |
|
| 18 |
has_gpu = torch.cuda.is_available()
|
| 19 |
|
|
@@ -66,25 +66,25 @@ This Space is heavily inspired by LazyMergeKit by Maxime Labonne (see [Colab](ht
|
|
| 66 |
examples = [[str(f)] for f in pathlib.Path("examples").glob("*.yaml")]
|
| 67 |
COMMUNITY_HF_TOKEN = os.getenv("COMMUNITY_HF_TOKEN")
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
runner = LogsViewRunner()
|
| 71 |
|
| 72 |
if not yaml_config:
|
| 73 |
-
|
| 74 |
return
|
| 75 |
|
| 76 |
try:
|
| 77 |
merge_config = MergeConfiguration.model_validate(yaml.safe_load(yaml_config))
|
| 78 |
except Exception as e:
|
| 79 |
-
|
| 80 |
return
|
| 81 |
|
| 82 |
is_community_model = False
|
| 83 |
if not hf_token:
|
| 84 |
if "/" in repo_name and not repo_name.startswith("mergekit-community/"):
|
| 85 |
-
|
| 86 |
return
|
| 87 |
-
|
| 88 |
is_community_model = True
|
| 89 |
if not COMMUNITY_HF_TOKEN:
|
| 90 |
raise gr.Error("Cannot upload to community org: community token not set by Space owner.")
|
|
@@ -98,10 +98,10 @@ def merge(yaml_config, hf_token, repo_name, profile_name):
|
|
| 98 |
merged_path.mkdir(parents=True, exist_ok=True)
|
| 99 |
config_path = merged_path / "config.yaml"
|
| 100 |
config_path.write_text(yaml_config)
|
| 101 |
-
|
| 102 |
|
| 103 |
if not repo_name:
|
| 104 |
-
|
| 105 |
repo_name = f"{profile_name}/mergekit-{merge_config.merge_method}" if profile_name else f"mergekit-{merge_config.merge_method}"
|
| 106 |
repo_name += "-" + "".join(random.choices(string.ascii_lowercase, k=7))
|
| 107 |
repo_name = repo_name.replace("/", "-").strip("-")
|
|
@@ -110,26 +110,42 @@ def merge(yaml_config, hf_token, repo_name, profile_name):
|
|
| 110 |
repo_name = f"mergekit-community/{repo_name}"
|
| 111 |
|
| 112 |
try:
|
| 113 |
-
|
| 114 |
repo_url = api.create_repo(repo_name, exist_ok=True)
|
| 115 |
-
|
| 116 |
except Exception as e:
|
| 117 |
-
|
| 118 |
return
|
| 119 |
|
| 120 |
tmp_env = os.environ.copy()
|
| 121 |
tmp_env["HF_HOME"] = f"{tmpdirname}/.cache"
|
| 122 |
full_cli = cli + f" --lora-merge-cache {tmpdirname}/.lora_cache"
|
| 123 |
-
|
| 124 |
|
| 125 |
if runner.exit_code != 0:
|
| 126 |
-
|
| 127 |
api.delete_repo(repo_url.repo_id)
|
| 128 |
return
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
with gr.Blocks() as demo:
|
| 135 |
gr.Markdown(MARKDOWN_DESCRIPTION)
|
|
@@ -146,7 +162,7 @@ with gr.Blocks() as demo:
|
|
| 146 |
gr.Examples(examples, fn=lambda s: (s,), run_on_click=True, label="Examples", inputs=[filename], outputs=[config])
|
| 147 |
gr.Markdown(MARKDOWN_ARTICLE)
|
| 148 |
|
| 149 |
-
button.click(fn=merge, inputs=[config, token, repo_name, profile_name], outputs=[logs]
|
| 150 |
|
| 151 |
|
| 152 |
@spaces.GPU
|
|
|
|
| 4 |
import string
|
| 5 |
import tempfile
|
| 6 |
import time
|
| 7 |
+
import threading
|
| 8 |
from typing import Iterable, List
|
| 9 |
|
| 10 |
import gradio as gr
|
|
|
|
| 13 |
import yaml
|
| 14 |
from gradio_logsview.logsview import Log, LogsView, LogsViewRunner
|
| 15 |
from mergekit.config import MergeConfiguration
|
| 16 |
+
from huggingface_hub import spaces
|
| 17 |
|
| 18 |
has_gpu = torch.cuda.is_available()
|
| 19 |
|
|
|
|
| 66 |
examples = [[str(f)] for f in pathlib.Path("examples").glob("*.yaml")]
|
| 67 |
COMMUNITY_HF_TOKEN = os.getenv("COMMUNITY_HF_TOKEN")
|
| 68 |
|
| 69 |
+
def merge_process(yaml_config, hf_token, repo_name, profile_name, logs_queue):
|
| 70 |
+
runner = LogsViewRunner(logs_queue)
|
| 71 |
|
| 72 |
if not yaml_config:
|
| 73 |
+
runner.log("Empty yaml, pick an example below", level="ERROR")
|
| 74 |
return
|
| 75 |
|
| 76 |
try:
|
| 77 |
merge_config = MergeConfiguration.model_validate(yaml.safe_load(yaml_config))
|
| 78 |
except Exception as e:
|
| 79 |
+
runner.log(f"Invalid yaml {e}", level="ERROR")
|
| 80 |
return
|
| 81 |
|
| 82 |
is_community_model = False
|
| 83 |
if not hf_token:
|
| 84 |
if "/" in repo_name and not repo_name.startswith("mergekit-community/"):
|
| 85 |
+
runner.log(f"Cannot upload merge model to namespace {repo_name.split('/')[0]}: you must provide a valid token.", level="ERROR")
|
| 86 |
return
|
| 87 |
+
runner.log("No HF token provided. Your merged model will be uploaded to the https://huggingface.co/mergekit-community organization.")
|
| 88 |
is_community_model = True
|
| 89 |
if not COMMUNITY_HF_TOKEN:
|
| 90 |
raise gr.Error("Cannot upload to community org: community token not set by Space owner.")
|
|
|
|
| 98 |
merged_path.mkdir(parents=True, exist_ok=True)
|
| 99 |
config_path = merged_path / "config.yaml"
|
| 100 |
config_path.write_text(yaml_config)
|
| 101 |
+
runner.log(f"Merge configuration saved in {config_path}")
|
| 102 |
|
| 103 |
if not repo_name:
|
| 104 |
+
runner.log("No repo name provided. Generating a random one.")
|
| 105 |
repo_name = f"{profile_name}/mergekit-{merge_config.merge_method}" if profile_name else f"mergekit-{merge_config.merge_method}"
|
| 106 |
repo_name += "-" + "".join(random.choices(string.ascii_lowercase, k=7))
|
| 107 |
repo_name = repo_name.replace("/", "-").strip("-")
|
|
|
|
| 110 |
repo_name = f"mergekit-community/{repo_name}"
|
| 111 |
|
| 112 |
try:
|
| 113 |
+
runner.log(f"Creating repo {repo_name}")
|
| 114 |
repo_url = api.create_repo(repo_name, exist_ok=True)
|
| 115 |
+
runner.log(f"Repo created: {repo_url}")
|
| 116 |
except Exception as e:
|
| 117 |
+
runner.log(f"Error creating repo {e}", level="ERROR")
|
| 118 |
return
|
| 119 |
|
| 120 |
tmp_env = os.environ.copy()
|
| 121 |
tmp_env["HF_HOME"] = f"{tmpdirname}/.cache"
|
| 122 |
full_cli = cli + f" --lora-merge-cache {tmpdirname}/.lora_cache"
|
| 123 |
+
runner.run_command(full_cli.split(), cwd=merged_path, env=tmp_env) # Removed yield from
|
| 124 |
|
| 125 |
if runner.exit_code != 0:
|
| 126 |
+
runner.log("Merge failed. Deleting repo as no model is uploaded.", level="ERROR")
|
| 127 |
api.delete_repo(repo_url.repo_id)
|
| 128 |
return
|
| 129 |
|
| 130 |
+
runner.log("Model merged successfully. Uploading to HF.")
|
| 131 |
+
runner.run_python(api.upload_folder, repo_id=repo_url.repo_id, folder_path=merged_path / "merge") # Removed yield from
|
| 132 |
+
runner.log(f"Model successfully uploaded to HF: {repo_url.repo_id}")
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def merge(yaml_config, hf_token, repo_name, profile_name):
|
| 136 |
+
logs_queue = []
|
| 137 |
+
thread = threading.Thread(target=merge_process, args=(yaml_config, hf_token, repo_name, profile_name, logs_queue))
|
| 138 |
+
thread.start()
|
| 139 |
+
|
| 140 |
+
while thread.is_alive():
|
| 141 |
+
if logs_queue:
|
| 142 |
+
yield logs_queue[:]
|
| 143 |
+
logs_queue.clear()
|
| 144 |
+
time.sleep(0.1)
|
| 145 |
+
|
| 146 |
+
if logs_queue:
|
| 147 |
+
yield logs_queue
|
| 148 |
+
|
| 149 |
|
| 150 |
with gr.Blocks() as demo:
|
| 151 |
gr.Markdown(MARKDOWN_DESCRIPTION)
|
|
|
|
| 162 |
gr.Examples(examples, fn=lambda s: (s,), run_on_click=True, label="Examples", inputs=[filename], outputs=[config])
|
| 163 |
gr.Markdown(MARKDOWN_ARTICLE)
|
| 164 |
|
| 165 |
+
button.click(fn=merge, inputs=[config, token, repo_name, profile_name], outputs=[logs])
|
| 166 |
|
| 167 |
|
| 168 |
@spaces.GPU
|