Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
73fcebf7
Commit
73fcebf7
authored
Dec 20, 2019
by
thomwolf
Browse files
update serving command
parent
15dda5ea
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
18 deletions
+29
-18
setup.py
setup.py
+3
-3
transformers-cli
transformers-cli
+1
-1
transformers/commands/serving.py
transformers/commands/serving.py
+25
-14
No files found.
setup.py
View file @
73fcebf7
...
@@ -38,9 +38,9 @@ from setuptools import find_packages, setup
...
@@ -38,9 +38,9 @@ from setuptools import find_packages, setup
extras
=
{
extras
=
{
'serving'
:
[
'uvicorn'
,
'fastapi'
],
'serving'
:
[
'pydantic'
,
'uvicorn'
,
'fastapi'
],
'serving-tf'
:
[
'uvicorn'
,
'fastapi'
,
'tensorflow'
],
'serving-tf'
:
[
'pydantic'
,
'uvicorn'
,
'fastapi'
,
'tensorflow'
],
'serving-torch'
:
[
'uvicorn'
,
'fastapi'
,
'torch'
]
'serving-torch'
:
[
'pydantic'
,
'uvicorn'
,
'fastapi'
,
'torch'
]
}
}
extras
[
'all'
]
=
[
package
for
package
in
extras
.
values
()]
extras
[
'all'
]
=
[
package
for
package
in
extras
.
values
()]
...
...
transformers-cli
View file @
73fcebf7
...
@@ -3,9 +3,9 @@ from argparse import ArgumentParser
...
@@ -3,9 +3,9 @@ from argparse import ArgumentParser
from
transformers.commands.download
import
DownloadCommand
from
transformers.commands.download
import
DownloadCommand
from
transformers.commands.run
import
RunCommand
from
transformers.commands.run
import
RunCommand
from
transformers.commands.serving
import
ServeCommand
from
transformers.commands.user
import
UserCommands
from
transformers.commands.user
import
UserCommands
from
transformers.commands.convert
import
ConvertCommand
from
transformers.commands.convert
import
ConvertCommand
from
transformers.commands.serving
import
ServeCommand
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
parser
=
ArgumentParser
(
'Transformers CLI tool'
,
usage
=
'transformers-cli <command> [<args>]'
)
parser
=
ArgumentParser
(
'Transformers CLI tool'
,
usage
=
'transformers-cli <command> [<args>]'
)
...
...
transformers/commands/serving.py
View file @
73fcebf7
from
argparse
import
ArgumentParser
,
Namespace
from
argparse
import
ArgumentParser
,
Namespace
from
typing
import
List
,
Optional
,
Union
,
Any
from
typing
import
List
,
Optional
,
Union
,
Any
from
fastapi
import
FastAPI
,
HTTPException
,
Body
import
logging
from
logging
import
getLogger
try
:
from
pydantic
import
BaseModel
from
uvicorn
import
run
from
uvicorn
import
run
from
fastapi
import
FastAPI
,
HTTPException
,
Body
from
pydantic
import
BaseModel
_serve_dependancies_installed
=
True
except
(
ImportError
,
AttributeError
):
BaseModel
=
object
Body
=
lambda
*
x
,
**
y
:
None
_serve_dependancies_installed
=
False
from
transformers
import
Pipeline
from
transformers
import
Pipeline
from
transformers.commands
import
BaseTransformersCLICommand
from
transformers.commands
import
BaseTransformersCLICommand
from
transformers.pipelines
import
SUPPORTED_TASKS
,
pipeline
from
transformers.pipelines
import
SUPPORTED_TASKS
,
pipeline
logger
=
logging
.
getLogger
(
'transformers-cli/serving'
)
def
serve_command_factory
(
args
:
Namespace
):
def
serve_command_factory
(
args
:
Namespace
):
"""
"""
...
@@ -70,13 +77,17 @@ class ServeCommand(BaseTransformersCLICommand):
...
@@ -70,13 +77,17 @@ class ServeCommand(BaseTransformersCLICommand):
serve_parser
.
set_defaults
(
func
=
serve_command_factory
)
serve_parser
.
set_defaults
(
func
=
serve_command_factory
)
def
__init__
(
self
,
pipeline
:
Pipeline
,
host
:
str
,
port
:
int
):
def
__init__
(
self
,
pipeline
:
Pipeline
,
host
:
str
,
port
:
int
):
self
.
_logger
=
getLogger
(
'transformers-cli/serving'
)
self
.
_pipeline
=
pipeline
self
.
_pipeline
=
pipeline
self
.
_logger
.
info
(
'Serving model over {}:{}'
.
format
(
host
,
port
))
self
.
_host
=
host
self
.
_host
=
host
self
.
_port
=
port
self
.
_port
=
port
if
not
_serve_dependancies_installed
:
raise
ImportError
(
"Using serve command requires FastAPI and unicorn. "
"Please install transformers with [serving]: pip install transformers[serving]."
"Or install FastAPI and unicorn separatly."
)
else
:
logger
.
info
(
'Serving model over {}:{}'
.
format
(
host
,
port
))
self
.
_app
=
FastAPI
()
self
.
_app
=
FastAPI
()
# Register routes
# Register routes
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment