Package: api-inference-community
This repositories enable third-party libraries integrated withhuggingface_hubto create
their own docker so that the widgets on the hub can work as thetransformersone do.The hardware to run the API will be provided by Hugging Face for now.Thedocker_images/commonfolder is intended to be a starter point for all new libs that
want to be integrated.Adding a new container from a new lib.Copy thedocker_images/commonfolder into your library's namedocker_images/example.Edit:docker_images/example/requirements.txtdocker_images/example/app/main.pydocker_images/example/app/pipelines/{task_name}.pyto implement the desired functionality. All required code is marked withIMPLEMENT_THISmarkup.Remove:Any pipeline files indocker_images/example/app/pipelines/that are not used.Any tests associated with deleted pipelines indocker_images/example/tests.Any imports of the pipelines you deleted fromdocker_images/example/app/pipelines/__init__.pyFeel free to customize anything required by your lib everywhere you want. The only real requirements, are to honor the HTTP endpoints, in the same fashion as thecommonfolder for all your supported tasks.Editexample/tests/test_api.pyto add TESTABLE_MODELS.Pass the test suitepytest -sv --rootdir docker_images/example/ docker_images/example/Submit your PR and enjoy !Going the full wayDoing the first 7 steps is good enough to get started, however in the process
you can anticipate some problems corrections early on. Maintainers will help you
along the way if you don't feel confident to follow those steps yourselfTest your creation within a docker./manage.pydockerMY_MODELshould work and responds on port 8000.curl -X POST -d "test" http://localhost:8000for instance if
the pipeline deals with simple text.If it doesn't work out of the box and/or docker is slow for some reason you
can test locally (using your local python environment) with :./manage.py start MY_MODELTest your docker uses cache properly.When doing subsequent docker launch with the same model_id, the docker should start up very fast and not redownload the whole model file. If you see the model/repo being downloaded over and over, it means the cache is not being used correctly.
You can edit thedocker_images/{framework}/Dockerfileand add an environment variable (by default it assumesHUGGINGFACE_HUB_CACHE), or your code directly to put
the model files in the/datafolder.Add a docker test.Edit thetests/test_dockers.pyfile to add a new test with your new framework
in it (def test_{framework}(self):for instance). As a basic you should have 1 line per task in this test function with a real working model on the hub. Those tests are relatively slow but will check automatically that correct errors are replied by your API and that the cache works properly. To run those tests your can simply do:RUN_DOCKER_TESTS=1pytest-svtests/test_dockers.py::DockerImageTests::test_{framework}Modifying files withinapi-inference-community/{routes,validation,..}.py.If you ever come across a bug withinapi-inference-community/package or want to update it
the development process is slightly more involved.First, make sure you need to change this package, each framework is very autonomous
so if your code can get away by being standalone go that way first as it's much simpler.If you can make the change only inapi-inference-communitywithout depending on it
that's also a great option. Make sure to add the proper tests to your PR.Finally, the best way to go is to develop locally usingmanage.pycommand:Do the necessary modifications withinapi-inference-communityfirst.Install it locally in your environment withpip install -e .Install your package dependencies locally.Run your webserver locally:./manage.py start --framework example --task audio-source-separation --model-id MY_MODELWhen everything is working, you will need to split your PR in two, 1 for theapi-inference-communitypart.
The second one will be for your package specific modifications and will only land once theapi-inference-communitytag has landed.This workflow is still work in progress, don't hesitate to ask questions to maintainers.Another similar command./manage.py docker --framework example --task audio-source-separation --model-id MY_MODELWill launch the server, but this time in a protected, controlled docker environment making sure the behavior
will be exactly the one in the API.Available tasksAutomatic speech recognition: Input is a file, output is a dict of understood words being said within the fileText generation: Input is a text, output is a dict of generated textImage recognition: Input is an image, output is a dict of generated textQuestion answering: Input is a question + some context, output is a dict containing necessary information to locate the answer to thequestionwithin thecontext.Audio source separation: Input is some audio, and the output is n audio files that sum up to the original audio but contain individual sources of sound (either speakers or instruments for instant).Token classification: Input is some text, and the output is a list of entities mentioned in the text. Entities can be anything remarkable like locations, organisations, persons, times etc...Text to speech: Input is some text, and the output is an audio file saying the text...Sentence Similarity: Input is some sentence and a list of reference sentences, and the list of similarity scores.