Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

service.py 1.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  1. import logging
  2. import warnings
  3. import os
  4. import numpy as np
  5. import bentoml
  6. from bentoml.io import Text, NumpyNdarray
  7. from skimage.transform import resize
  8. warnings.filterwarnings("ignore")
  9. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # FATAL
  10. logging.getLogger('tensorflow').setLevel(logging.FATAL)
  11. def create_bento_service_keras(bento_name):
  12. """
  13. Create a Bento service for a Keras model.
  14. """
  15. # Load the model
  16. keras_model = bentoml.keras.load_runner(bento_name)
  17. # Create the service
  18. service = bentoml.Service(bento_name + "_service", runners=[keras_model])
  19. return keras_model, service
  20. model, service = create_bento_service_keras("conv2d_larger_dropout")
  21. # Create an API function
  22. @service.api(input=Text(), output=NumpyNdarray())
  23. def predict(image_str) -> np.ndarray:
  24. """
  25. Predict pet pawpularity from an image using the given Bento.
  26. """
  27. # Convert the image back to numpy array
  28. image = np.fromstring(image_str, np.uint8)
  29. image = resize(image, (224, 224, 3))
  30. image = image / 255.0
  31. result = model.run(image)
  32. return result
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...