MCPcopy Create free account
hub / github.com/WasmEdge/WasmEdge / extractBase64ImagePayload

Function extractBase64ImagePayload

plugins/wasi_nn/GGML/utils.cpp:54–83  ·  view source on GitHub ↗

Extract base64 image payload and image type. Replace it with placeholder.

Source from the content-addressed store, hash-verified

52
53// Extract base64 image payload and image type. Replace it with placeholder.
54std::optional<std::pair<std::vector<uint8_t>, std::string>>
55extractBase64ImagePayload(std::string &Prompt,
56 std::tuple<size_t, size_t, size_t> ImagePos,
57 const std::string_view Placeholder) noexcept {
58 // Locate the payload and image type.
59 size_t BeginTagPos = std::get<0>(ImagePos);
60 size_t TypePos = std::get<0>(ImagePos) + Base64ImageTagPrefix.size();
61 size_t PayloadPos = std::get<1>(ImagePos);
62 size_t BeginBytePos = std::get<1>(ImagePos) + Base64ImageBytesPrefix.size();
63 size_t EndTagPos = std::get<2>(ImagePos);
64 std::string_view Payload =
65 std::string_view(Prompt).substr(BeginBytePos, EndTagPos - BeginBytePos);
66 std::string ImageType = Prompt.substr(TypePos, PayloadPos - TypePos);
67
68 // Decode the base64 payload.
69 auto RequiredBytes = base64::required_encode_size(Payload.size());
70 std::vector<uint8_t> ImageBytes(RequiredBytes);
71 try {
72 base64::decode(Payload.begin(), Payload.end(), ImageBytes.begin());
73 } catch (const base64_error &E) {
74 RET_ERROR(std::make_pair(std::vector<uint8_t>(), ""),
75 "base64: Error when calling base64::decode: {}"sv, E.what())
76 }
77
78 // Replace the base64 image with the placeholder.
79 Prompt.replace(BeginTagPos,
80 EndTagPos - BeginTagPos + Base64ImageTagSuffix.size(),
81 Placeholder);
82 return std::make_pair(ImageBytes, ImageType);
83}
84
85#endif
86} // namespace WasmEdge::Host::WASINN::GGML

Callers 1

setInputFunction · 0.85

Calls 6

substrMethod · 0.80
decodeFunction · 0.50
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected