File size: 1,843 Bytes
c445497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch


def merge(base_model, trained_adapter, token):
    base = AutoModelForCausalLM.from_pretrained(
        base_model, torch_dtype=torch.float16, low_cpu_mem_usage=True, token=token
    )
    model = PeftModel.from_pretrained(base, trained_adapter, token=token)
    try:
        tokenizer = AutoTokenizer.from_pretrained(base_model, token=token)
    except RecursionError:
        tokenizer = AutoTokenizer.from_pretrained(
            base_model, unk_token="<unk>", token=token
        )

    model = model.merge_and_unload()

    print("Saving target model")
    model.push_to_hub(trained_adapter, token=token)
    tokenizer.push_to_hub(trained_adapter, token=token)
    return gr.Markdown.update(
        value="Model successfully merged and pushed! Please shutdown/pause this space"
    )


with gr.Blocks() as demo:
    gr.Markdown("## AutoTrain Merge Adapter")
    gr.Markdown("Please duplicate this space and attach a GPU in order to use it.")
    token = gr.Textbox(
        label="Hugging Face Write Token",
        value="",
        lines=1,
        max_lines=1,
        interactive=True,
        type="password",
    )
    base_model = gr.Textbox(
        label="Base Model (e.g. meta-llama/Llama-2-7b-chat-hf)",
        value="",
        lines=1,
        max_lines=1,
        interactive=True,
    )
    trained_adapter = gr.Textbox(
        label="Trained Adapter Model (e.g. username/autotrain-my-llama)",
        value="",
        lines=1,
        max_lines=1,
        interactive=True,
    )
    submit = gr.Button(value="Merge & Push")
    op = gr.Markdown(interactive=False)
    submit.click(merge, inputs=[base_model, trained_adapter, token], outputs=[op])


if __name__ == "__main__":
    demo.launch()