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

Inference API wrapper client #65

Merged
merged 21 commits into from
Jul 16, 2021
Merged

Inference API wrapper client #65

merged 21 commits into from
Jul 16, 2021

Conversation

osanseviero
Copy link
Member

@osanseviero osanseviero commented Jun 2, 2021

This is still WIP, but I would like to get some feedback early on. We use the validation from the inference API to validate the inputs are ok.

Ideally this would be in a single call, but the API can expect both inputs and params (see zero-shot), so I added set_inputs and set_params methods. Let me know what you think

from huggingface_hub.inference_api import InferenceApi

# Mask filling (only inputs, string)
api = InferenceApi("bert-base-uncased")
print(api("The goal of life is [MASK]."))

# Zero-shot (input string, with params)
api = InferenceApi("typeform/distilbert-base-uncased-mnli")
inputs="Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!"
params = {
    "candidate_labels": ["refund", "legal", "faq"]
}
print(api(inputs, params))

# Question answering (input is dict, with params)
api = InferenceApi("deepset/roberta-base-squad2")
inputs={
    "question":"What's my name?",
    "context":"My name is Clara and I live in Berkeley."
}
print(api(inputs, params))

# Feature extraction by overriding pipeline tag (input is string)
api = InferenceApi("osanseviero/full-sentence-distillroberta2", task="feature-extraction")
inputs="This is an example again"
print(api(inputs))

# Error, missing context
api = InferenceApi("deepset/roberta-base-squad2")
print(api({"question":"What's my name?"}))

# Error, task does not exist
api = InferenceApi("bert-base-uncased", task="invalid_task")

@julien-c @Narsil FYI

Copy link
Contributor

@Narsil Narsil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with adding wrapper over the inference but as already said elsewhere, I am a bit concerned with maintenance of this as it could really go out of sync, as the groundtruth for transformers is really the pipelines part of transformers.

src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/validation.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks neat, but do we actually want to perform validation client-side, vs. calling the API and displaying a clear validation error here?

src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
osanseviero and others added 2 commits June 4, 2021 15:41
Co-authored-by: Julien Chaumond <julien@huggingface.co>
@osanseviero
Copy link
Member Author

osanseviero commented Jun 4, 2021

My initial discussion with @LysandreJik led to the idea of client-side validation, but based on the comments, I think we should remove it.

I checked the returned values of the inference API when missing inputs and they are very explicit.

Benefits of client-side validation:

  • We can throw errors early on (totally ok if we don't have)
  • Users can easily find the inputs/params (fixable with good documentation and good error messages from server-side)

Cons of client-side validation

  • Hard to maintain
  • Incompatibility between transformers and some community pipelines (list of lists as inputs, for example).

Based on this and the discussion, I removed all client-side validation :)

@osanseviero osanseviero changed the title Add draft of inference API wrapper client Proposal for inference API wrapper client Jun 4, 2021
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, this looks cool!

Regarding client-side validation, I am quite bullish on IntelliSense being a big component of the wrapper; but if it explodes complexity/maintainability then we can definitely overlook it in this first version and revisit if it makes sense later on.

src/huggingface_hub/inference_api.py Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
@julien-c
Copy link
Member

julien-c commented Jun 9, 2021

also tagging @cbensimon with whom we've discussed this wrapper recently

@osanseviero osanseviero changed the title Proposal for inference API wrapper client Inference API wrapper client Jul 15, 2021
@osanseviero osanseviero marked this pull request as ready for review July 15, 2021 15:52
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
src/huggingface_hub/inference_api.py Outdated Show resolved Hide resolved
Comment on lines +140 to +141
# TODO: Decide if we should raise an error instead of
# returning the json.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned offline with @osanseviero, I think handling exceptions here is the most pythonic way to handle things - and that without such logic there's little value in having a Python wrapper for the inference API if it's a glorified cURL utility.

I would argue that the wrapper's ability to return appropriate errors such as raise ModelNotLoadedError or raise MissingKeyError, which inherit from the appropriate canonic errors (ValueError, OSError), is very important API-wise.

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to the next PR to address the comments - agree to merge this now so that Flax users can use it for their spaces

@osanseviero osanseviero merged commit 409f819 into main Jul 16, 2021
@osanseviero osanseviero deleted the inference_wrapper branch July 16, 2021 13:12
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

Successfully merging this pull request may close these issues.

None yet

5 participants