Spaces:
Running
Running
Add support of private repo
Browse files- app.py +44 -18
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import gradio as gr
|
| 4 |
-
from huggingface_hub import HfApi, whoami, ModelCard
|
| 5 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
| 6 |
from textwrap import dedent
|
| 7 |
from pathlib import Path
|
|
@@ -9,9 +9,6 @@ from pathlib import Path
|
|
| 9 |
from tempfile import TemporaryDirectory
|
| 10 |
|
| 11 |
from huggingface_hub.file_download import repo_folder_name
|
| 12 |
-
from optimum.exporters.tasks import TasksManager
|
| 13 |
-
from optimum.intel.utils.constant import _TASK_ALIASES
|
| 14 |
-
from optimum.intel.openvino.utils import _HEAD_TO_AUTOMODELS
|
| 15 |
from optimum.exporters import TasksManager
|
| 16 |
|
| 17 |
from optimum.intel.utils.modeling_utils import _find_files_matching_pattern
|
|
@@ -28,9 +25,23 @@ from optimum.intel import (
|
|
| 28 |
OVStableDiffusionPipeline,
|
| 29 |
OVStableDiffusionXLPipeline,
|
| 30 |
OVLatentConsistencyModelPipeline,
|
| 31 |
-
OVModelForPix2Struct,
|
| 32 |
OVWeightQuantizationConfig,
|
| 33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def quantize_model(
|
| 36 |
model_id: str,
|
|
@@ -42,23 +53,33 @@ def quantize_model(
|
|
| 42 |
oauth_token: gr.OAuthToken,
|
| 43 |
):
|
| 44 |
if oauth_token.token is None:
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
model_name = model_id.split("/")[-1]
|
| 48 |
username = whoami(oauth_token.token)["name"]
|
| 49 |
new_repo_id = f"{username}/{model_name}-openvino-{dtype}"
|
| 50 |
-
|
| 51 |
-
library_name = TasksManager.infer_library_from_model(model_id)
|
| 52 |
-
# task = TasksManager.infer_task_from_model(model_id, token=oauth_token.token)
|
| 53 |
-
# library_name = TasksManager.infer_library_from_model(model_id, token=oauth_token.token)
|
| 54 |
|
| 55 |
-
if
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
if task == "text2text-generation":
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
auto_model_class = _HEAD_TO_AUTOMODELS[task]
|
| 64 |
ov_files = _find_files_matching_pattern(
|
|
@@ -85,7 +106,7 @@ def quantize_model(
|
|
| 85 |
|
| 86 |
api = HfApi(token=oauth_token.token)
|
| 87 |
if api.repo_exists(new_repo_id) and not overwritte:
|
| 88 |
-
|
| 89 |
|
| 90 |
with TemporaryDirectory() as d:
|
| 91 |
folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
|
|
@@ -129,7 +150,7 @@ def quantize_model(
|
|
| 129 |
repo_id=new_repo_id,
|
| 130 |
)
|
| 131 |
except Exception as e:
|
| 132 |
-
|
| 133 |
|
| 134 |
try:
|
| 135 |
card = ModelCard.load(model_id, token=oauth_token.token)
|
|
@@ -169,6 +190,8 @@ def quantize_model(
|
|
| 169 |
repo_id=new_repo_id,
|
| 170 |
)
|
| 171 |
return f"This model was successfully quantized, find it under your repo {new_repo_url}'"
|
|
|
|
|
|
|
| 172 |
finally:
|
| 173 |
shutil.rmtree(folder, ignore_errors=True)
|
| 174 |
|
|
@@ -177,6 +200,8 @@ DESCRIPTION = """
|
|
| 177 |
This Space uses [Optimum Intel](https://huggingface.co/docs/optimum/main/en/intel/openvino/optimization) to automatically apply NNCF weight only quantization on a model hosted on the [Hub](https://huggingface.co/models) and convert it to the [OpenVINO format](https://docs.openvino.ai/2024/documentation/openvino-ir-format.html) if not already.
|
| 178 |
|
| 179 |
The resulting model will then be pushed under your HF user namespace. For now we only support conversion for models that are hosted on public repositories.
|
|
|
|
|
|
|
| 180 |
"""
|
| 181 |
|
| 182 |
model_id = HuggingfaceHubSearch(
|
|
@@ -202,6 +227,7 @@ quant_method = gr.Dropdown(
|
|
| 202 |
"""
|
| 203 |
calibration_dataset = gr.Dropdown(
|
| 204 |
[
|
|
|
|
| 205 |
"wikitext2",
|
| 206 |
"c4",
|
| 207 |
"c4-new",
|
|
@@ -209,7 +235,7 @@ calibration_dataset = gr.Dropdown(
|
|
| 209 |
"laion/220k-GPT4Vision-captions-from-LIVIS",
|
| 210 |
"laion/filtered-wit",
|
| 211 |
],
|
| 212 |
-
value="
|
| 213 |
label="Calibration dataset",
|
| 214 |
filterable=False,
|
| 215 |
visible=True,
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import gradio as gr
|
| 4 |
+
from huggingface_hub import HfApi, whoami, ModelCard, model_info
|
| 5 |
from gradio_huggingfacehub_search import HuggingfaceHubSearch
|
| 6 |
from textwrap import dedent
|
| 7 |
from pathlib import Path
|
|
|
|
| 9 |
from tempfile import TemporaryDirectory
|
| 10 |
|
| 11 |
from huggingface_hub.file_download import repo_folder_name
|
|
|
|
|
|
|
|
|
|
| 12 |
from optimum.exporters import TasksManager
|
| 13 |
|
| 14 |
from optimum.intel.utils.modeling_utils import _find_files_matching_pattern
|
|
|
|
| 25 |
OVStableDiffusionPipeline,
|
| 26 |
OVStableDiffusionXLPipeline,
|
| 27 |
OVLatentConsistencyModelPipeline,
|
|
|
|
| 28 |
OVWeightQuantizationConfig,
|
| 29 |
)
|
| 30 |
+
from diffusers import ConfigMixin
|
| 31 |
+
|
| 32 |
+
_HEAD_TO_AUTOMODELS = {
|
| 33 |
+
"feature-extraction": OVModelForFeatureExtraction,
|
| 34 |
+
"fill-mask": OVModelForMaskedLM,
|
| 35 |
+
"text-generation": OVModelForCausalLM,
|
| 36 |
+
"text-classification": OVModelForSequenceClassification,
|
| 37 |
+
"token-classification": OVModelForTokenClassification,
|
| 38 |
+
"question-answering": OVModelForQuestionAnswering,
|
| 39 |
+
"image-classification": OVModelForImageClassification,
|
| 40 |
+
"audio-classification": OVModelForAudioClassification,
|
| 41 |
+
"stable-diffusion": OVStableDiffusionPipeline,
|
| 42 |
+
"stable-diffusion-xl": OVStableDiffusionXLPipeline,
|
| 43 |
+
"latent-consistency": OVLatentConsistencyModelPipeline,
|
| 44 |
+
}
|
| 45 |
|
| 46 |
def quantize_model(
|
| 47 |
model_id: str,
|
|
|
|
| 53 |
oauth_token: gr.OAuthToken,
|
| 54 |
):
|
| 55 |
if oauth_token.token is None:
|
| 56 |
+
return "You must be logged in to use this space"
|
| 57 |
+
|
| 58 |
+
if not model_id:
|
| 59 |
+
return f"### Invalid input 🐞 Please specify a model name, got {model_id}"
|
| 60 |
|
| 61 |
model_name = model_id.split("/")[-1]
|
| 62 |
username = whoami(oauth_token.token)["name"]
|
| 63 |
new_repo_id = f"{username}/{model_name}-openvino-{dtype}"
|
| 64 |
+
library_name = TasksManager.infer_library_from_model(model_id, token=oauth_token.token)
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
if library_name == "diffusers":
|
| 67 |
+
ConfigMixin.config_name = "model_index.json"
|
| 68 |
+
class_name = ConfigMixin.load_config(model_id, token=oauth_token.token)["_class_name"].lower()
|
| 69 |
+
if "xl" in class_name:
|
| 70 |
+
task = "stable-diffusion-xl"
|
| 71 |
+
elif "consistency" in class_name:
|
| 72 |
+
task = "latent-consistency"
|
| 73 |
+
else:
|
| 74 |
+
task = "stable-diffusion"
|
| 75 |
+
else:
|
| 76 |
+
task = TasksManager.infer_task_from_model(model_id, token=oauth_token.token)
|
| 77 |
|
| 78 |
if task == "text2text-generation":
|
| 79 |
+
return "Export of Seq2Seq models is currently disabled."
|
| 80 |
+
|
| 81 |
+
if task not in _HEAD_TO_AUTOMODELS:
|
| 82 |
+
return f"The task '{task}' is not supported, only {_HEAD_TO_AUTOMODELS.keys()} tasks are supported"
|
| 83 |
|
| 84 |
auto_model_class = _HEAD_TO_AUTOMODELS[task]
|
| 85 |
ov_files = _find_files_matching_pattern(
|
|
|
|
| 106 |
|
| 107 |
api = HfApi(token=oauth_token.token)
|
| 108 |
if api.repo_exists(new_repo_id) and not overwritte:
|
| 109 |
+
return f"Model {new_repo_id} already exist, please set overwritte=True to push on an existing repo"
|
| 110 |
|
| 111 |
with TemporaryDirectory() as d:
|
| 112 |
folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))
|
|
|
|
| 150 |
repo_id=new_repo_id,
|
| 151 |
)
|
| 152 |
except Exception as e:
|
| 153 |
+
return f"Error uploading file {file_path}: {e}"
|
| 154 |
|
| 155 |
try:
|
| 156 |
card = ModelCard.load(model_id, token=oauth_token.token)
|
|
|
|
| 190 |
repo_id=new_repo_id,
|
| 191 |
)
|
| 192 |
return f"This model was successfully quantized, find it under your repo {new_repo_url}'"
|
| 193 |
+
except Exception as e:
|
| 194 |
+
return f"### Error: {e}"
|
| 195 |
finally:
|
| 196 |
shutil.rmtree(folder, ignore_errors=True)
|
| 197 |
|
|
|
|
| 200 |
This Space uses [Optimum Intel](https://huggingface.co/docs/optimum/main/en/intel/openvino/optimization) to automatically apply NNCF weight only quantization on a model hosted on the [Hub](https://huggingface.co/models) and convert it to the [OpenVINO format](https://docs.openvino.ai/2024/documentation/openvino-ir-format.html) if not already.
|
| 201 |
|
| 202 |
The resulting model will then be pushed under your HF user namespace. For now we only support conversion for models that are hosted on public repositories.
|
| 203 |
+
|
| 204 |
+
The list of the supported architectures can be found in the [documentation](https://huggingface.co/docs/optimum/main/en/intel/openvino/models)
|
| 205 |
"""
|
| 206 |
|
| 207 |
model_id = HuggingfaceHubSearch(
|
|
|
|
| 227 |
"""
|
| 228 |
calibration_dataset = gr.Dropdown(
|
| 229 |
[
|
| 230 |
+
"None",
|
| 231 |
"wikitext2",
|
| 232 |
"c4",
|
| 233 |
"c4-new",
|
|
|
|
| 235 |
"laion/220k-GPT4Vision-captions-from-LIVIS",
|
| 236 |
"laion/filtered-wit",
|
| 237 |
],
|
| 238 |
+
value="None",
|
| 239 |
label="Calibration dataset",
|
| 240 |
filterable=False,
|
| 241 |
visible=True,
|
requirements.txt
CHANGED
|
@@ -3,7 +3,6 @@ gradio[oauth]>=4.37.2
|
|
| 3 |
gradio_huggingfacehub_search==0.0.6
|
| 4 |
transformers==4.42.4
|
| 5 |
diffusers==0.29.1
|
| 6 |
-
optimum==1.21.2
|
| 7 |
-
optimum-intel==1.18.1
|
| 8 |
openvino
|
| 9 |
nncf
|
|
|
|
|
|
| 3 |
gradio_huggingfacehub_search==0.0.6
|
| 4 |
transformers==4.42.4
|
| 5 |
diffusers==0.29.1
|
|
|
|
|
|
|
| 6 |
openvino
|
| 7 |
nncf
|
| 8 |
+
git+https://github.com/huggingface/optimum-intel.git
|