Spaces:
Sleeping
Sleeping
First draft
Browse files- app.py +130 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
from pathlib import Path
|
4 |
+
from typing import Iterable, List
|
5 |
+
|
6 |
+
import gradio as gr
|
7 |
+
import kagglehub
|
8 |
+
from gradio_logsview.logsview import Log, LogsView, LogsViewRunner
|
9 |
+
from huggingface_hub import HfApi
|
10 |
+
|
11 |
+
KAGGLE_JSON = os.environ.get("KAGGLE_JSON")
|
12 |
+
KAGGLE_JSON_PATH = Path("~/.kaggle/kaggle.json").expanduser().resolve()
|
13 |
+
if KAGGLE_JSON_PATH.exists():
|
14 |
+
print(f"Found existing kaggle.json file at {KAGGLE_JSON_PATH}")
|
15 |
+
elif KAGGLE_JSON is not None:
|
16 |
+
print(
|
17 |
+
"KAGGLE_JSON is set as secret. Will be able to be authenticated when downloading files from Kaggle."
|
18 |
+
)
|
19 |
+
KAGGLE_JSON_PATH.mkdir(parents=True, exist_ok=True)
|
20 |
+
KAGGLE_JSON_PATH.write_text(KAGGLE_JSON)
|
21 |
+
else:
|
22 |
+
print(
|
23 |
+
f"No kaggle.json file found at {KAGGLE_JSON_PATH}. You will not be able to download private/gated files from Kaggle."
|
24 |
+
)
|
25 |
+
|
26 |
+
|
27 |
+
MARKDOWN_DESCRIPTION = """
|
28 |
+
# Keggla-importer GUI
|
29 |
+
|
30 |
+
The fastest way to import a model from KaggleHub to the Hugging Face Hub 🔥
|
31 |
+
|
32 |
+
Specify a Kaggle handle and a Hugging Face Write Token to import a model from KaggleHub to the Hugging Face Hub.
|
33 |
+
|
34 |
+
To find the Kaggle handle from a web UI, click on the "download dropdown" and copy the handle from the code snippet.
|
35 |
+
Example: `"keras/gemma/keras/gemma_instruct_2b_en"`.
|
36 |
+
"""
|
37 |
+
|
38 |
+
if KAGGLE_JSON_PATH.exists():
|
39 |
+
MARKDOWN_DESCRIPTION += """
|
40 |
+
|
41 |
+
**Note**: a `kaggle.json` file exists in the home directory. This means the Space will be able to download **SOME** private/gated files from Kaggle.
|
42 |
+
To access other models, please duplicate this Space to a private Space and set the `KAGGLE_JSON` environment variable with the content of the `kaggle.json`
|
43 |
+
you've downloaded from your Kaggle user account.
|
44 |
+
|
45 |
+
"""
|
46 |
+
|
47 |
+
|
48 |
+
def import_model(kaggle_model: str, repo_name: str, token: str) -> Iterable[List[Log]]:
|
49 |
+
if not kaggle_model:
|
50 |
+
return "Kaggle model is required."
|
51 |
+
if not repo_name:
|
52 |
+
repo_name = kaggle_model.split("/")[-1]
|
53 |
+
if not token:
|
54 |
+
return "HF Write Token is required."
|
55 |
+
api = HfApi(token=token)
|
56 |
+
|
57 |
+
runner = LogsViewRunner()
|
58 |
+
|
59 |
+
yield runner.log(f"Creating HF repo {repo_name}")
|
60 |
+
repo_url = api.create_repo(repo_name, exist_ok=True)
|
61 |
+
yield runner.log(f"Created HF repo: {repo_url}")
|
62 |
+
repo_id = repo_url.repo_id
|
63 |
+
|
64 |
+
model_id = api.model_info(repo_id)
|
65 |
+
if len(model_id.siblings) > 1:
|
66 |
+
yield runner.log(
|
67 |
+
f"Model repo {repo_id} is not empty. Please delete it or set a different repo name.",
|
68 |
+
level="ERROR",
|
69 |
+
)
|
70 |
+
return
|
71 |
+
|
72 |
+
yield runner.log(f"Downloading model {kaggle_model} from Kaggle.")
|
73 |
+
yield from runner.run_python(kagglehub.model_download, handle=kaggle_model)
|
74 |
+
if runner.exit_code != 0:
|
75 |
+
yield runner.log("Failed to download model from Kaggle.", level="ERROR")
|
76 |
+
api.delete_repo(repo_id=repo_id)
|
77 |
+
return
|
78 |
+
|
79 |
+
cache_path = kagglehub.model_download(kaggle_model) # should be instant
|
80 |
+
yield runner.log(f"Model successfully downloaded from Kaggle to {cache_path}.")
|
81 |
+
|
82 |
+
yield runner.log(f"Uploading model to HF repo {repo_id}.")
|
83 |
+
yield from runner.run_python(
|
84 |
+
api.upload_folder, repo_id=repo_id, folder_path=cache_path
|
85 |
+
)
|
86 |
+
if runner.exit_code != 0:
|
87 |
+
yield runner.log("Failed to upload model to HF repo.", level="ERROR")
|
88 |
+
api.delete_repo(repo_id=repo_id)
|
89 |
+
return
|
90 |
+
|
91 |
+
yield runner.log(f"Model successfully uploaded to HF: {repo_url}.")
|
92 |
+
yield runner.log(f"Deleting local cache from {cache_path}.")
|
93 |
+
shutil.rmtree(cache_path)
|
94 |
+
|
95 |
+
yield runner.log("Done!")
|
96 |
+
|
97 |
+
|
98 |
+
with gr.Blocks() as demo:
|
99 |
+
gr.Markdown(MARKDOWN_DESCRIPTION)
|
100 |
+
|
101 |
+
with gr.Row():
|
102 |
+
with gr.Column():
|
103 |
+
kaggle_model = gr.Textbox(
|
104 |
+
lines=1,
|
105 |
+
label="Kaggle Model*",
|
106 |
+
placeholder="keras/codegemma/keras/code_gemma_7b_en",
|
107 |
+
)
|
108 |
+
repo_name = gr.Textbox(
|
109 |
+
lines=1,
|
110 |
+
label="Repo name",
|
111 |
+
placeholder="Optional. Will infer from Kaggle Model if empty.",
|
112 |
+
)
|
113 |
+
with gr.Column():
|
114 |
+
token = gr.Textbox(
|
115 |
+
lines=1,
|
116 |
+
label="HF Write Token*",
|
117 |
+
info="https://hf.co/settings/token",
|
118 |
+
type="password",
|
119 |
+
placeholder="hf_***",
|
120 |
+
)
|
121 |
+
|
122 |
+
button = gr.Button("Import", variant="primary")
|
123 |
+
logs = LogsView(label="Terminal output")
|
124 |
+
|
125 |
+
button.click(
|
126 |
+
fn=import_model, inputs=[kaggle_model, repo_name, token], outputs=[logs]
|
127 |
+
)
|
128 |
+
|
129 |
+
|
130 |
+
demo.queue(default_concurrency_limit=1).launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
kaggle
|
2 |
+
huggingface_hub
|
3 |
+
# see https://huggingface.co/spaces/Wauplin/gradio_logsview
|
4 |
+
gradio_logsview@https://huggingface.co/spaces/Wauplin/gradio_logsview/resolve/main/gradio_logsview-0.0.5-py3-none-any.whl
|