Create and deploy the Vespa app

Create the simplest possible app required by our experiment, containing fields for image and video.

from vespa.package import ApplicationPackage, Field

app_package = ApplicationPackage(name="media")
app_package.schema.add_fields(
    Field(name="image", type="string", indexing=["summary"]),
    Field(name="video", type="string", indexing=["summary"])
)

Locally deploy in a Docker container:

from vespa.deployment import VespaDocker

vespa_docker = VespaDocker()
app = vespa_docker.deploy(app_package)
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for configuration server.
Waiting for application status.
Waiting for application status.
Waiting for application status.
Finished deployment.

Sample image and video

Sample image:

from PIL import Image

image_file_path = "data/2021-12-01-convert-media-to-string/dogs.jpg"
image = Image.open(image_file_path)
image

Sample video:

from IPython.display import Video

video_file_path = "data/2021-12-01-convert-media-to-string/archery.mp4"
Video(video_file_path, embed=True)

Send encoded data

Function to encode media data to a string

import uu
from io import BytesIO

def encode_media_to_string(media_file_path):
    encoded_media_byte_array = BytesIO()
    uu.encode(media_file_path, encoded_media_byte_array)    
    return encoded_media_byte_array.getvalue().decode("ascii")

Send encoded image and video to the vespa app:

send_response = app.feed_data_point(
    data_id=0, 
    fields={
        "image": encode_media_to_string(image_file_path), 
        "video": encode_media_to_string(video_file_path)
    },
    schema="media"
)
send_response.status_code
200

Retrieve and decode data

Function to decode string to media.

def decode_string_to_media(value):
    bytes_string = BytesIO(value.encode("ascii"))
    bytes_media = BytesIO()
    uu.decode(bytes_string, bytes_media)
    return bytes_media

Retrieve data point contained encoded image and video data.

get_response = app.get_data(data_id=0, schema="media")

Decoded image:

decoded_image = decode_string_to_media(get_response.json["fields"]["image"])
Image.open(decoded_image)

Decoded video:

decoded_video = decode_string_to_media(get_response.json["fields"]["video"])
Video(decoded_video.getvalue(), embed=True, mimetype="video/mp4")