Spaces:
Running
Running
Upload externalmod.py
Browse files- externalmod.py +78 -24
externalmod.py
CHANGED
@@ -9,7 +9,7 @@ import re
|
|
9 |
import tempfile
|
10 |
import warnings
|
11 |
from pathlib import Path
|
12 |
-
from typing import TYPE_CHECKING, Callable
|
13 |
|
14 |
import httpx
|
15 |
import huggingface_hub
|
@@ -33,11 +33,15 @@ if TYPE_CHECKING:
|
|
33 |
from gradio.interface import Interface
|
34 |
|
35 |
|
|
|
|
|
|
|
|
|
36 |
@document()
|
37 |
def load(
|
38 |
name: str,
|
39 |
src: str | None = None,
|
40 |
-
hf_token: str | None = None,
|
41 |
alias: str | None = None,
|
42 |
**kwargs,
|
43 |
) -> Blocks:
|
@@ -48,7 +52,7 @@ def load(
|
|
48 |
Parameters:
|
49 |
name: the name of the model (e.g. "gpt2" or "facebook/bart-base") or space (e.g. "flax-community/spanish-gpt2"), can include the `src` as prefix (e.g. "models/facebook/bart-base")
|
50 |
src: the source of the model: `models` or `spaces` (or leave empty if source is provided as a prefix in `name`)
|
51 |
-
hf_token: optional access token for loading private Hugging Face Hub models or spaces. Find your token here: https://huggingface.co/settings/tokens. Warning: only provide
|
52 |
alias: optional string used as the name of the loaded model instead of the default name (only applies if loading a Space running Gradio 2.x)
|
53 |
Returns:
|
54 |
a Gradio Blocks object for the given model
|
@@ -65,7 +69,7 @@ def load(
|
|
65 |
def load_blocks_from_repo(
|
66 |
name: str,
|
67 |
src: str | None = None,
|
68 |
-
hf_token: str | None = None,
|
69 |
alias: str | None = None,
|
70 |
**kwargs,
|
71 |
) -> Blocks:
|
@@ -89,7 +93,7 @@ def load_blocks_from_repo(
|
|
89 |
if src.lower() not in factory_methods:
|
90 |
raise ValueError(f"parameter: src must be one of {factory_methods.keys()}")
|
91 |
|
92 |
-
if hf_token is not None:
|
93 |
if Context.hf_token is not None and Context.hf_token != hf_token:
|
94 |
warnings.warn(
|
95 |
"""You are loading a model/Space with a different access token than the one you used to load a previous model/Space. This is not recommended, as it may cause unexpected behavior."""
|
@@ -100,12 +104,16 @@ def load_blocks_from_repo(
|
|
100 |
return blocks
|
101 |
|
102 |
|
103 |
-
def from_model(
|
|
|
|
|
104 |
model_url = f"https://huggingface.co/{model_name}"
|
105 |
api_url = f"https://api-inference.huggingface.co/models/{model_name}"
|
106 |
print(f"Fetching model from: {model_url}")
|
107 |
|
108 |
-
headers =
|
|
|
|
|
109 |
response = httpx.request("GET", api_url, headers=headers)
|
110 |
if response.status_code != 200:
|
111 |
raise ModelNotFoundError(
|
@@ -115,7 +123,7 @@ def from_model(model_name: str, hf_token: str | None, alias: str | None, **kwarg
|
|
115 |
|
116 |
headers["X-Wait-For-Model"] = "true"
|
117 |
client = huggingface_hub.InferenceClient(
|
118 |
-
model=model_name, headers=headers, token=hf_token
|
119 |
)
|
120 |
|
121 |
# For tasks that are not yet supported by the InferenceClient
|
@@ -365,10 +373,14 @@ def from_model(model_name: str, hf_token: str | None, alias: str | None, **kwarg
|
|
365 |
else:
|
366 |
raise ValueError(f"Unsupported pipeline type: {p}")
|
367 |
|
368 |
-
def query_huggingface_inference_endpoints(*data):
|
369 |
if preprocess is not None:
|
370 |
data = preprocess(*data)
|
371 |
-
|
|
|
|
|
|
|
|
|
372 |
if postprocess is not None:
|
373 |
data = postprocess(data) # type: ignore
|
374 |
return data
|
@@ -380,7 +392,7 @@ def from_model(model_name: str, hf_token: str | None, alias: str | None, **kwarg
|
|
380 |
"inputs": inputs,
|
381 |
"outputs": outputs,
|
382 |
"title": model_name,
|
383 |
-
|
384 |
}
|
385 |
|
386 |
kwargs = dict(interface_info, **kwargs)
|
@@ -391,19 +403,12 @@ def from_model(model_name: str, hf_token: str | None, alias: str | None, **kwarg
|
|
391 |
def from_spaces(
|
392 |
space_name: str, hf_token: str | None, alias: str | None, **kwargs
|
393 |
) -> Blocks:
|
394 |
-
client = Client(
|
395 |
-
space_name,
|
396 |
-
hf_token=hf_token,
|
397 |
-
download_files=False,
|
398 |
-
_skip_components=False,
|
399 |
-
)
|
400 |
-
|
401 |
space_url = f"https://huggingface.co/spaces/{space_name}"
|
402 |
|
403 |
print(f"Fetching Space from: {space_url}")
|
404 |
|
405 |
headers = {}
|
406 |
-
if hf_token
|
407 |
headers["Authorization"] = f"Bearer {hf_token}"
|
408 |
|
409 |
iframe_url = (
|
@@ -440,8 +445,7 @@ def from_spaces(
|
|
440 |
"Blocks or Interface locally. You may find this Guide helpful: "
|
441 |
"https://gradio.app/using_blocks_like_functions/"
|
442 |
)
|
443 |
-
|
444 |
-
return from_spaces_blocks(space=space_name, hf_token=hf_token)
|
445 |
|
446 |
|
447 |
def from_spaces_blocks(space: str, hf_token: str | None) -> Blocks:
|
@@ -486,7 +490,7 @@ def from_spaces_interface(
|
|
486 |
config = external_utils.streamline_spaces_interface(config)
|
487 |
api_url = f"{iframe_url}/api/predict/"
|
488 |
headers = {"Content-Type": "application/json"}
|
489 |
-
if hf_token
|
490 |
headers["Authorization"] = f"Bearer {hf_token}"
|
491 |
|
492 |
# The function should call the API with preprocessed data
|
@@ -526,6 +530,56 @@ def gr_Interface_load(
|
|
526 |
src: str | None = None,
|
527 |
hf_token: str | None = None,
|
528 |
alias: str | None = None,
|
529 |
-
**kwargs,
|
530 |
) -> Blocks:
|
531 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
import tempfile
|
10 |
import warnings
|
11 |
from pathlib import Path
|
12 |
+
from typing import TYPE_CHECKING, Callable, Literal
|
13 |
|
14 |
import httpx
|
15 |
import huggingface_hub
|
|
|
33 |
from gradio.interface import Interface
|
34 |
|
35 |
|
36 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
|
37 |
+
server_timeout = 600
|
38 |
+
|
39 |
+
|
40 |
@document()
|
41 |
def load(
|
42 |
name: str,
|
43 |
src: str | None = None,
|
44 |
+
hf_token: str | Literal[False] | None = None,
|
45 |
alias: str | None = None,
|
46 |
**kwargs,
|
47 |
) -> Blocks:
|
|
|
52 |
Parameters:
|
53 |
name: the name of the model (e.g. "gpt2" or "facebook/bart-base") or space (e.g. "flax-community/spanish-gpt2"), can include the `src` as prefix (e.g. "models/facebook/bart-base")
|
54 |
src: the source of the model: `models` or `spaces` (or leave empty if source is provided as a prefix in `name`)
|
55 |
+
hf_token: optional access token for loading private Hugging Face Hub models or spaces. Will default to the locally saved token if not provided. Pass `token=False` if you don't want to send your token to the server. Find your token here: https://huggingface.co/settings/tokens. Warning: only provide a token if you are loading a trusted private Space as it can be read by the Space you are loading.
|
56 |
alias: optional string used as the name of the loaded model instead of the default name (only applies if loading a Space running Gradio 2.x)
|
57 |
Returns:
|
58 |
a Gradio Blocks object for the given model
|
|
|
69 |
def load_blocks_from_repo(
|
70 |
name: str,
|
71 |
src: str | None = None,
|
72 |
+
hf_token: str | Literal[False] | None = None,
|
73 |
alias: str | None = None,
|
74 |
**kwargs,
|
75 |
) -> Blocks:
|
|
|
93 |
if src.lower() not in factory_methods:
|
94 |
raise ValueError(f"parameter: src must be one of {factory_methods.keys()}")
|
95 |
|
96 |
+
if hf_token is not None and hf_token is not False:
|
97 |
if Context.hf_token is not None and Context.hf_token != hf_token:
|
98 |
warnings.warn(
|
99 |
"""You are loading a model/Space with a different access token than the one you used to load a previous model/Space. This is not recommended, as it may cause unexpected behavior."""
|
|
|
104 |
return blocks
|
105 |
|
106 |
|
107 |
+
def from_model(
|
108 |
+
model_name: str, hf_token: str | Literal[False] | None, alias: str | None, **kwargs
|
109 |
+
):
|
110 |
model_url = f"https://huggingface.co/{model_name}"
|
111 |
api_url = f"https://api-inference.huggingface.co/models/{model_name}"
|
112 |
print(f"Fetching model from: {model_url}")
|
113 |
|
114 |
+
headers = (
|
115 |
+
{} if hf_token in [False, None] else {"Authorization": f"Bearer {hf_token}"}
|
116 |
+
)
|
117 |
response = httpx.request("GET", api_url, headers=headers)
|
118 |
if response.status_code != 200:
|
119 |
raise ModelNotFoundError(
|
|
|
123 |
|
124 |
headers["X-Wait-For-Model"] = "true"
|
125 |
client = huggingface_hub.InferenceClient(
|
126 |
+
model=model_name, headers=headers, token=hf_token, timeout=server_timeout,
|
127 |
)
|
128 |
|
129 |
# For tasks that are not yet supported by the InferenceClient
|
|
|
373 |
else:
|
374 |
raise ValueError(f"Unsupported pipeline type: {p}")
|
375 |
|
376 |
+
def query_huggingface_inference_endpoints(*data, **kwargs):
|
377 |
if preprocess is not None:
|
378 |
data = preprocess(*data)
|
379 |
+
try:
|
380 |
+
data = fn(*data, **kwargs) # type: ignore
|
381 |
+
except huggingface_hub.utils.HfHubHTTPError as e:
|
382 |
+
if "429" in str(e):
|
383 |
+
raise TooManyRequestsError() from e
|
384 |
if postprocess is not None:
|
385 |
data = postprocess(data) # type: ignore
|
386 |
return data
|
|
|
392 |
"inputs": inputs,
|
393 |
"outputs": outputs,
|
394 |
"title": model_name,
|
395 |
+
#"examples": examples,
|
396 |
}
|
397 |
|
398 |
kwargs = dict(interface_info, **kwargs)
|
|
|
403 |
def from_spaces(
|
404 |
space_name: str, hf_token: str | None, alias: str | None, **kwargs
|
405 |
) -> Blocks:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
406 |
space_url = f"https://huggingface.co/spaces/{space_name}"
|
407 |
|
408 |
print(f"Fetching Space from: {space_url}")
|
409 |
|
410 |
headers = {}
|
411 |
+
if hf_token not in [False, None]:
|
412 |
headers["Authorization"] = f"Bearer {hf_token}"
|
413 |
|
414 |
iframe_url = (
|
|
|
445 |
"Blocks or Interface locally. You may find this Guide helpful: "
|
446 |
"https://gradio.app/using_blocks_like_functions/"
|
447 |
)
|
448 |
+
return from_spaces_blocks(space=space_name, hf_token=hf_token)
|
|
|
449 |
|
450 |
|
451 |
def from_spaces_blocks(space: str, hf_token: str | None) -> Blocks:
|
|
|
490 |
config = external_utils.streamline_spaces_interface(config)
|
491 |
api_url = f"{iframe_url}/api/predict/"
|
492 |
headers = {"Content-Type": "application/json"}
|
493 |
+
if hf_token not in [False, None]:
|
494 |
headers["Authorization"] = f"Bearer {hf_token}"
|
495 |
|
496 |
# The function should call the API with preprocessed data
|
|
|
530 |
src: str | None = None,
|
531 |
hf_token: str | None = None,
|
532 |
alias: str | None = None,
|
533 |
+
**kwargs, # ignore
|
534 |
) -> Blocks:
|
535 |
+
try:
|
536 |
+
return load_blocks_from_repo(name, src, hf_token, alias)
|
537 |
+
except Exception as e:
|
538 |
+
print(e)
|
539 |
+
return gradio.Interface(lambda: None, ['text'], ['image'])
|
540 |
+
|
541 |
+
|
542 |
+
def list_uniq(l):
|
543 |
+
return sorted(set(l), key=l.index)
|
544 |
+
|
545 |
+
|
546 |
+
def get_status(model_name: str):
|
547 |
+
from huggingface_hub import AsyncInferenceClient
|
548 |
+
client = AsyncInferenceClient(token=HF_TOKEN, timeout=10)
|
549 |
+
return client.get_model_status(model_name)
|
550 |
+
|
551 |
+
|
552 |
+
def is_loadable(model_name: str, force_gpu: bool = False):
|
553 |
+
try:
|
554 |
+
status = get_status(model_name)
|
555 |
+
except Exception as e:
|
556 |
+
print(e)
|
557 |
+
print(f"Couldn't load {model_name}.")
|
558 |
+
return False
|
559 |
+
gpu_state = isinstance(status.compute_type, dict) and "gpu" in status.compute_type.keys()
|
560 |
+
if status is None or status.state not in ["Loadable", "Loaded"] or (force_gpu and not gpu_state):
|
561 |
+
print(f"Couldn't load {model_name}. Model state:'{status.state}', GPU:{gpu_state}")
|
562 |
+
return status is not None and status.state in ["Loadable", "Loaded"] and (not force_gpu or gpu_state)
|
563 |
+
|
564 |
+
|
565 |
+
def find_model_list(author: str="", tags: list[str]=[], not_tag="", sort: str="last_modified", limit: int=30, force_gpu=False, check_status=False):
|
566 |
+
from huggingface_hub import HfApi
|
567 |
+
api = HfApi(token=HF_TOKEN)
|
568 |
+
default_tags = ["diffusers"]
|
569 |
+
if not sort: sort = "last_modified"
|
570 |
+
limit = limit * 20 if check_status and force_gpu else limit * 5
|
571 |
+
models = []
|
572 |
+
try:
|
573 |
+
model_infos = api.list_models(author=author, #task="text-to-image",
|
574 |
+
tags=list_uniq(default_tags + tags), cardData=True, sort=sort, limit=limit)
|
575 |
+
except Exception as e:
|
576 |
+
print(f"Error: Failed to list models.")
|
577 |
+
print(e)
|
578 |
+
return models
|
579 |
+
for model in model_infos:
|
580 |
+
if not model.private and not model.gated or HF_TOKEN is not None:
|
581 |
+
loadable = is_loadable(model.id, force_gpu) if check_status else True
|
582 |
+
if not_tag and not_tag in model.tags or not loadable: continue
|
583 |
+
models.append(model.id)
|
584 |
+
if len(models) == limit: break
|
585 |
+
return models
|