-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample datatype for Serve Component (#15623)
* introducing serve component * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * clean up tests * clean up tests * doctest * mypy * structure-fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * cleanup * cleanup * test fix * addition * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * test fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * requirements * getting future url * url for local * sample data typeg * changes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * prediction * updates * updates * manifest * fix type error * fixed test Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Rick Izzo <[email protected]> Co-authored-by: Jirka <[email protected]>
- Loading branch information
1 parent
61d3253
commit 136a090
Showing
8 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# !pip install torchvision pydantic | ||
import base64 | ||
import io | ||
|
||
import torch | ||
import torchvision | ||
from PIL import Image | ||
from pydantic import BaseModel | ||
|
||
import lightning as L | ||
from lightning.app.components.serve import Image as InputImage | ||
from lightning.app.components.serve import PythonServer | ||
|
||
|
||
class PyTorchServer(PythonServer): | ||
def setup(self): | ||
self._model = torchvision.models.resnet18(pretrained=True) | ||
self._device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | ||
self._model.to(self._device) | ||
|
||
def predict(self, request): | ||
image = base64.b64decode(request.image.encode("utf-8")) | ||
image = Image.open(io.BytesIO(image)) | ||
transforms = torchvision.transforms.Compose( | ||
[ | ||
torchvision.transforms.Resize(224), | ||
torchvision.transforms.ToTensor(), | ||
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), | ||
] | ||
) | ||
image = transforms(image) | ||
image = image.to(self._device) | ||
prediction = self._model(image.unsqueeze(0)) | ||
return {"prediction": prediction.argmax().item()} | ||
|
||
|
||
class OutputData(BaseModel): | ||
prediction: int | ||
|
||
|
||
component = PyTorchServer(input_type=InputImage, output_type=OutputData, cloud_compute=L.CloudCompute("gpu")) | ||
app = L.LightningApp(component) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from lightning_app.components.serve.gradio import ServeGradio | ||
from lightning_app.components.serve.python_server import PythonServer | ||
from lightning_app.components.serve.python_server import Image, Number, PythonServer | ||
from lightning_app.components.serve.streamlit import ServeStreamlit | ||
|
||
__all__ = ["ServeGradio", "ServeStreamlit", "PythonServer"] | ||
__all__ = ["ServeGradio", "ServeStreamlit", "PythonServer", "Image", "Number"] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters