# 16_01 IPVO
https://www.tensorflow.org/
cera api
https://www.tensorflow.org/tfx
https://www.tensorflow.org/tfx/serving/api_rest
https://www.tensorflow.org/tfx/serving/docker
gRPC API
``` docker pull tensorflow/serving ```
``` git clone https://github.com/tensorflow/serving ```
```TESTDATA="$(pwd)/serving/tensorflow_serving/servables/tensorflow/testdata" ```
```
echo $TESTDATA
/home/korisnik/serving/tensorflow_serving/servables/tensorflow/testdata
```
```
ls $TESTDATA ✔
bad_half_plus_two saved_model_half_plus_three
bad_model_config.txt saved_model_half_plus_two_2_versions
batching_config.txt saved_model_half_plus_two_2_versions_metadata.json
BUILD saved_model_half_plus_two_cpu
export_bad_half_plus_two.py saved_model_half_plus_two_gpu
export_counter.py saved_model_half_plus_two_gpu_trt
good_model_config.txt saved_model_half_plus_two_mkl
half_plus_two_model_metadata.json saved_model_half_plus_two_mlmd
mobilenet_v1_quant_tflite saved_model_half_plus_two.py
mobilenet_v1_quant_tflite.README saved_model_half_plus_two_tf2_2_versions_metadata.json
monitoring_config.txt saved_model_half_plus_two_tf2_cpu
parse_example_tflite saved_model_half_plus_two_tflite
parse_example_tflite.py saved_model_half_plus_two_tflite_with_sigdef
parse_example_tflite.README tf_text_regression
saved_model_counter tf_text_regression.README
```
-v --> pristup vanjskom resursu direktoriju
```
docker run -t --rm -p 8501:8501 \
-v "$TESTDATA/saved_model_half_plus_two_cpu:/models/half_plus_two" \
-e MODEL_NAME=half_plus_two \
tensorflow/serving
2023-01-16 15:36:19.053975: I tensorflow_serving/model_servers/server.cc:74] Building single TensorFlow model file config: model_name: half_plus_two model_base_path: /models/half_plus_two
2023-01-16 15:36:19.054557: I tensorflow_serving/model_servers/server_core.cc:465] Adding/updating models.
2023-01-16 15:36:19.054614: I tensorflow_serving/model_servers/server_core.cc:594] (Re-)adding model: half_plus_two
2023-01-16 15:36:19.227991: I tensorflow_serving/core/basic_manager.cc:739] Successfully reserved resources to load servable {name: half_plus_two version: 123}
2023-01-16 15:36:19.228016: I tensorflow_serving/core/loader_harness.cc:66] Approving load for servable version {name: half_plus_two version: 123}
2023-01-16 15:36:19.228026: I tensorflow_serving/core/loader_harness.cc:74] Loading servable version {name: half_plus_two version: 123}
```
```
curl -v -d '{"instances": [1.0, 2.0, 5.0]}' http://localhost:8501/v1/models/half_plus_two:predict
* Trying 127.0.0.1:8501...
* Connected to localhost (127.0.0.1) port 8501 (#0)
> POST /v1/models/half_plus_two:predict HTTP/1.1
> Host: localhost:8501
> User-Agent: curl/7.87.0
> Accept: */*
> Content-Length: 30
> Content-Type: application/x-www-form-urlencoded
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Mon, 16 Jan 2023 15:40:30 GMT
< Content-Length: 43
<
{
"predictions": [2.5, 3.0, 4.5
]
* Connection #0 to host localhost left intact
}%
```
```
curl -d '{"instances": [1.0, 2.0, 5.0]}' http://localhost:8501/v1/models/half_plus_two:predict | jq ✔
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 73 100 43 100 30 37752 26338 --:--:-- --:--:-- --:--:-- 73000
{
"predictions": [
2.5,
3,
4.5
]
}
```
## zad1
isporobajte na isti nacin modele bad_half_plus_two i saved_model_half_plus_three
## vscode
klijent.py
```
import http.client
import json
conn = http.client.HTTPConnection("localhost", 8501)
# /v1/models/saved_model_half_plus_three:predict
conn.request(
"POST",
"/v1/models/half_plus_two:predict",
'{\"instances\": [1.0, 2.0, 5.0]}',
{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "applicaiton/json"
}
)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
print(json.loads(data))
```
```
[korisnik@odj-o366-118 projekt]$ /usr/bin/python /home/korisnik/projekt/klijent.py
200 OK
{'predictions': [2.5, 3.0, 4.5]}
```
isporbajte za slova i cijele brojeve - samo se u instances promijeni 1.0, 2.0 i 5.0
cijeli brojevi rade jer JSON ne razlikuje float i int brojeve
```
[korisnik@odj-o366-118 projekt]$ /usr/bin/python /home/korisnik/projekt/klijent.py
400 Bad Request
{'error': 'Failed to process element: 0 of \'instances\' list. Error: INVALID_ARGUMENT: JSON Value: "aa" Type: String is not of expected type: float'}
[korisnik@odj-o366-118 projekt]$ /usr/bin/python /home/korisnik/projekt/klijent.py
400 Bad Request
{'error': 'JSON Parse error: Invalid value. at offset: 15'}
```
novi klijent.py
```
import http.client
import json
conn = http.client.HTTPConnection("localhost", 8501)
# /v1/models/saved_model_half_plus_three:predict
conn.request(
"POST",
"/v1/models/half_plus_two:predict",
'{\"instances\": [{}]}',
{
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "applicaiton/json"
}
)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
odgovor = json.loads(data)
if response.status == 200:
print(odgovor["predictions"])
else:
print(odgovor["error"])
```