Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huggingface Spaces #8

Closed
AK391 opened this issue Feb 2, 2022 · 18 comments
Closed

Huggingface Spaces #8

AK391 opened this issue Feb 2, 2022 · 18 comments

Comments

@AK391
Copy link
Contributor

AK391 commented Feb 2, 2022

Hi, would you be interested in sharing a web demo on Huggingface Spaces for UniFormer?

It would make this model more accessible as it would allow people to try out the model directly from the browser. Some other recent machine learning model repos have set up Spaces for easy access:

github: https://github.com/salesforce/BLIP
Spaces: https://huggingface.co/spaces/akhaliq/BLIP

github: https://github.com/facebookresearch/omnivore
Spaces: https://huggingface.co/spaces/akhaliq/omnivore

Spaces is completely free, and I can help setup a Gradio Space. Here are some getting started instructions if you'd prefer to do it yourself: https://huggingface.co/blog/gradio-spaces

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 2, 2022

Thanks for your interesting suggestions! Since these days are the Chinese spring festival, I will finish a web demo when free.

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 3, 2022

@AK391 I have created a demo for image classification followed your code (https://huggingface.co/spaces/Andy1621/uniformer_image_demo). I will try to add video demo later ~~

@AK391
Copy link
Contributor Author

AK391 commented Feb 3, 2022

@Andy1621 Thanks, I opened a PR #9 to add a link/badge to the demo in the readme. Also for outputs types label can be used here https://gradio.app/docs/#o_label

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 4, 2022

@AK391 I have create a video demo and merge the PR! Thanks for your interesting suggestion!

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 4, 2022

@AK391 How can I set the font size of gradio.outputs.Label? It seems that the default font size is too large to show the prediction😂

@AK391
Copy link
Contributor Author

AK391 commented Feb 4, 2022

@Andy1621 thanks video demo look great, you can try using custom css https://gradio.app/docs/#interface for the font size

@AK391
Copy link
Contributor Author

AK391 commented Feb 4, 2022

@Andy1621 also I created a org for https://huggingface.co/Sense-X SenseTime X-Lab (similar to github https://github.com/Sense-X), you can move all your spaces+models+datasets here similar to how you do on github, just send a invite request and I can accept it

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 5, 2022

@AK391 Look great!
(1) Is there any good practice for uploading models? I find swin create one model for each model (https://huggingface.co/microsoft/swin-small-patch4-window7-224). And there are README.md, config.json, preprocessor_config.json, and pytorch_model.bin.
(2) Besides, how can I upload my model code? I find they can simply import their model like from transformers import SwinForImageClassification.
(2) How can I upload multi models fast? It seems that I should upload one model at one time.

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 5, 2022

@AK391 BTW, I have sent a request to the organization you created. Would you please accept it and give me the corresponding permission to verify the organization?

@AK391
Copy link
Contributor Author

AK391 commented Feb 5, 2022

@Andy1621 Thanks, approved request to adding to organization. Regarding adding models, there is a step-by-step guide for adding your model to the Hugging Face Hub. Once models are added they can be used in spaces in two ways, one like here https://huggingface.co/spaces/akhaliq/JoJoGAN by using

from huggingface_hub import hf_hub_download

and if the inference api is setup they can be added using

gr.Interface.load()

like here https://huggingface.co/spaces/akhaliq/xm_transformer_600m

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 6, 2022

@AK391 Thanks for your patience. I will try to add some models today.

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 9, 2022

@AK391 I have updated some popular models and demo in hugging. More models will be uploaded if needed.

@AK391
Copy link
Contributor Author

AK391 commented Feb 9, 2022

@Andy1621 Great thanks, also once a model is added to the hub it can be easily turned in a Gradio space using, for example

import gradio as gr
gr.Interface.Load("huggingface/Sense-X/uniformer_image").launch()

see

https://gradio.app/docs/#load

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 10, 2022

@AK391 Thanks! But how does it work? It seems that such load api does not create an Interface (including fn, inputs and outputs). And the code you use does not initialize a model like model=uniformer_small().

import gradio as gr
gr.Interface.Load("huggingface/Sense-X/uniformer_image").launch()
gradio.Interface(
  self, fn, inputs=None, outputs=None, examples=None, examples_per_page=10, 
  live=False, layout="unaligned", interpretation=None, num_shap=2.0, theme=None, 
  title=None, description=None, article=None, thumbnail=None, css=None, 
  allow_screenshot=True, allow_flagging=None, flagging_options=None, flagging_dir="flagged"
)

Gradio interfaces are created by constructing a Interface object with a locally-defined function,
or with Interface.load() with the path to a repo or by Interface.from_pipeline() with a Transformers Pipeline.

Should I change the code for repo? It seems confused to use such load api.


One more question, since I have not pull UniFormer to the transformers repo, I can not use the model api of transformers like swin:

from transformers import AutoFeatureExtractor, SwinForImageClassification
feature_extractor = AutoFeatureExtractor.from_pretrained("microsoft/swin-tiny-patch4-window7-224")
model = SwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224")

thus I suggest others used as in model card: create model => load model by hf_hub_download => model inference.
And I upload multi models in one Model, which is not like deit and swin that created by Hugging Face team. They simply upload one model for one Model (see https://huggingface.co/models?search=swin).

@AK391
Copy link
Contributor Author

AK391 commented Feb 10, 2022

@Andy1621
Copy link
Collaborator

Andy1621 commented Feb 10, 2022

@AK391 Thanks!

def get_huggingface_interface(model_name, api_key, alias):
    model_url = "https://huggingface.co/{}".format(model_name)
    api_url = "https://api-inference.huggingface.co/models/{}".format(model_name)
    print("Fetching model from: {}".format(model_url))


    if api_key is not None:
        headers = {"Authorization": f"Bearer {api_key}"}
    else:
        headers = {}


    # Checking if model exists, and if so, it gets the pipeline
    response = requests.request("GET", api_url, headers=headers)
    assert response.status_code == 200, "Invalid model name or src"
    p = response.json().get("pipeline_tag")

The followed dict is the response of https://huggingface.co/microsoft/swin-tiny-patch4-window7-224

{
  'id': 'microsoft/swin-tiny-patch4-window7-224', 
  'modelId': 'microsoft/swin-tiny-patch4-window7-224', 
  'private': False, 
  'pipeline_tag': 'image-classification', 
  'sha': '896e18f760e16594e63d8bbe01ada6142bb0ef8e', 
  'lastModified': '2022-01-28T13:12:55.000Z', 
  'tags': ['pytorch', 'swin', 'image-classification', 'dataset:imagenet-1k', 'arxiv:2103.14030', 'transformers', 'license:apache-2.0', 'vision'], 
  'siblings': [{'rfilename': '.gitattributes'}, {'rfilename': 'README.md'}, {'rfilename': 'config.json'}, {'rfilename': 'preprocessor_config.json'}, {'rfilename': 'pytorch_model.bin'}], 
  'downloads': 1165, 
  'library_name': 'transformers', 
  'widgetData': [{'src': 'https://huggingface.co/datasets/mishig/sample_images/resolve/main/tiger.jpg', 'example_title': 'Tiger'}, {'src': 'https://huggingface.co/datasets/mishig/sample_images/resolve/main/teapot.jpg', 'example_title': 'Teapot'}, {'src': 'https://huggingface.co/datasets/mishig/sample_images/resolve/main/palace.jpg', 'example_title': 'Palace'}], 
  'likes': 0, 
  'model-index': None, 
  'config': {'architectures': ['SwinForImageClassification'], 'model_type': 'swin'}, 
  'cardData': {'license': 'apache-2.0', 'tags': ['vision', 'image-classification'], 'datasets': ['imagenet-1k'], 
  'widget': [{'src': 'https://huggingface.co/datasets/mishig/sample_images/resolve/main/tiger.jpg', 'example_title': 'Tiger'}, {'src': 'https://huggingface.co/datasets/mishig/sample_images/resolve/main/teapot.jpg', 'example_title': 'Teapot'}, {'src': 'https://huggingface.co/datasets/mishig/sample_images/resolve/main/palace.jpg', 'example_title': 'Palace'}]}
}

As what I guessed, it seems that such api load() only works for those models in transformers repo (required special config and pipeline_tag). And such api does not support video_classification.
Now I simply upload multi models in one Model and used it as space to share models, which is not consistent with other Model. Their model codes are merged in transformers, so they can used the apis.

@AK391
Copy link
Contributor Author

AK391 commented Feb 10, 2022

@Andy1621 you can see the currently supported inference api pipelines gradio has here: https://github.com/gradio-app/gradio/blob/c9298b38021323918037a5a39914a787e8517f60/gradio/external.py#L49, video classification is currently not support through huggingface or gradio but you can try image classification, see current pipelines here for huggingface model hub https://github.com/huggingface/huggingface_hub/blob/df012f1489cb50f15e0908598d7e355f1f31b52f/js/src/lib/interfaces/Types.ts

@Andy1621
Copy link
Collaborator

As there is no more activity, I am closing the issue, don't hesitate to reopen it if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants