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

Allow specifying data with the Inference API wrapper #271

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/huggingface_hub/inference_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,23 @@ def __repr__(self):

def __call__(
self,
inputs: Union[str, Dict, List[str], List[List[str]]],
inputs: Optional[Union[str, Dict, List[str], List[List[str]]]] = None,
params: Optional[Dict] = None,
data: Optional[bytes] = None,
):
payload = {
"inputs": inputs,
"options": self.options,
}

if inputs:
payload["inputs"] = inputs

if params:
payload["parameters"] = params

# TODO: Decide if we should raise an error instead of
# returning the json.
response = requests.post(
self.api_url, headers=self.headers, json=payload
self.api_url, headers=self.headers, json=payload, data=data
).json()
return response
Binary file added tests/samples/plane.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/samples/sample1.flac
Binary file not shown.
28 changes: 27 additions & 1 deletion tests/test_inference_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.


import os
import unittest

from huggingface_hub.inference_api import InferenceApi
Expand All @@ -21,6 +21,13 @@


class InferenceApiTest(unittest.TestCase):
def read(self, filename: str) -> bytes:
dirname = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(dirname, "samples", filename)
with open(filename, "rb") as f:
bpayload = f.read()
return bpayload

@with_production_testing
def test_simple_inference(self):
api = InferenceApi("bert-base-uncased")
Expand Down Expand Up @@ -55,6 +62,25 @@ def test_inference_with_dict_inputs(self):
self.assertTrue("score" in result)
self.assertTrue("answer" in result)

@with_production_testing
def test_inference_with_audio(self):
api = InferenceApi("facebook/wav2vec2-large-960h-lv60-self")
data = self.read("sample1.flac")
result = api(data=data)
self.assertIsInstance(result, dict)
self.assertTrue("text" in result)

@with_production_testing
def test_inference_with_image(self):
api = InferenceApi("google/vit-base-patch16-224")
data = self.read("plane.jpg")
result = api(data=data)
self.assertIsInstance(result, list)
for classification in result:
self.assertIsInstance(classification, dict)
self.assertTrue("score" in classification)
self.assertTrue("label" in classification)

@with_production_testing
def test_inference_overriding_task(self):
api = InferenceApi(
Expand Down