ngxson HF staff commited on
Commit
edb9ee2
·
verified ·
1 Parent(s): 0e2f342

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import signal
4
+ os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
5
+ import gradio as gr
6
+ import tempfile
7
+
8
+ from huggingface_hub import HfApi, ModelCard, whoami
9
+ from gradio_huggingfacehub_search import HuggingfaceHubSearch
10
+ from pathlib import Path
11
+ from textwrap import dedent
12
+
13
+
14
+ def process_model(ft_model_id: str, base_model_id: str, rank: str, private_repo, oauth_token: gr.OAuthToken | None):
15
+ if oauth_token is None or oauth_token.token is None:
16
+ raise gr.Error("You must be logged in")
17
+ model_name = ft_model_id.split('/')[-1]
18
+
19
+ if not os.path.exists("outputs"):
20
+ os.makedirs("outputs")
21
+
22
+ try:
23
+ api = HfApi(token=oauth_token.token)
24
+
25
+ with tempfile.TemporaryDirectory(dir="outputs") as outputdir:
26
+ result = subprocess.run([
27
+ "mergekit-extract-lora",
28
+ ft_model_id,
29
+ base_model_id,
30
+ outputdir,
31
+ f"--rank={rank}",
32
+ ], shell=False, capture_output=True)
33
+ print(result)
34
+ if result.returncode != 0:
35
+ raise Exception(f"Error converting to LoRA PEFT {q_method}: {result.stderr}")
36
+ print("Model converted to LoRA PEFT successfully!")
37
+ print(f"Converted model path: {outputdir}")
38
+
39
+ # Check output dir
40
+ if not os.listdir(outputdir):
41
+ raise Exception("Output directory is empty!")
42
+
43
+ # Create repo
44
+ username = whoami(oauth_token.token)["name"]
45
+ new_repo_url = api.create_repo(repo_id=f"{username}/LoRA-{model_name}", exist_ok=True, private=private_repo)
46
+ new_repo_id = new_repo_url.repo_id
47
+ print("Repo created successfully!", new_repo_url)
48
+
49
+ # Upload files
50
+ api.upload_file(
51
+ folder_path=outputdir,
52
+ path_in_repo="",
53
+ repo_id=new_repo_id,
54
+ )
55
+ print("Uploaded", outputdir)
56
+
57
+ return (
58
+ f'<h1>✅ DONE</h1><br/><br/>Find your repo here: <a href="{new_repo_url}" target="_blank" style="text-decoration:underline">{new_repo_id}</a>'
59
+ )
60
+ except Exception as e:
61
+ return (f"<h1>❌ ERROR</h1><br/><br/>{e}")
62
+
63
+
64
+ css="""/* Custom CSS to allow scrolling */
65
+ .gradio-container {overflow-y: auto;}
66
+ """
67
+ # Create Gradio interface
68
+ with gr.Blocks(css=css) as demo:
69
+ gr.Markdown("You must be logged in.")
70
+ gr.LoginButton(min_width=250)
71
+
72
+ ft_model_id = HuggingfaceHubSearch(
73
+ label="Fine tuned model repository",
74
+ placeholder="Search for repository on Huggingface",
75
+ search_type="model",
76
+ )
77
+
78
+ base_model_id = HuggingfaceHubSearch(
79
+ label="Base tuned model repository",
80
+ placeholder="Search for repository on Huggingface",
81
+ search_type="model",
82
+ )
83
+
84
+ rank = gr.Dropdown(
85
+ ["16", "32", "64", "128"],
86
+ label="LoRA rank",
87
+ info="Higher the rank, better the result, but heavier the adapter",
88
+ value="32",
89
+ filterable=False,
90
+ visible=True
91
+ )
92
+
93
+ private_repo = gr.Checkbox(
94
+ value=False,
95
+ label="Private Repo",
96
+ info="Create a private repo under your username."
97
+ )
98
+
99
+ iface = gr.Interface(
100
+ fn=process_model,
101
+ inputs=[
102
+ ft_model_id,
103
+ base_model_id,
104
+ rank,
105
+ private_repo,
106
+ ],
107
+ outputs=[
108
+ gr.Markdown(label="output"),
109
+ ],
110
+ title="Convert fine tuned model into LoRA with mergekit-extract-lora",
111
+ description="The space takes a fine tuned model, a base model, then make a PEFT-compatible LoRA adapter based on the difference between 2 models.",
112
+ api_name=False
113
+ )
114
+
115
+ # Launch the interface
116
+ demo.queue(default_concurrency_limit=1, max_size=5).launch(debug=True, show_api=False)