| 55 | } |
| 56 | |
| 57 | bool Qwen2VL::insert(chat_meta_info_t& meta_info, lm_uniform_input_t& input, std::function<bool()> is_cancelled) { |
| 58 | // preprocess |
| 59 | constexpr int image_soft_token_id = 151655; |
| 60 | this->profiler_list[TKOEN_ENCODE_TIME].start(); |
| 61 | std::string templated_text; |
| 62 | if (input.messages.empty() && input.prompt.empty()) { |
| 63 | header_print_r("WARNING", "No messages or prompt provided"); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | constexpr bool DEBUG_IMAGE_PREPROCESS = false; |
| 68 | qwen2vl_image_payload_t image_payload; |
| 69 | image_payload.num_images = 0; |
| 70 | if (input.images.size() > 0) { |
| 71 | |
| 72 | |
| 73 | // header_print("info", "Processing images..."); |
| 74 | |
| 75 | // time_utils::time_point preprocess_start = time_utils::now(); |
| 76 | for(const auto& img_str : input.images){ |
| 77 | qwen2vl_image_t image = this->load_image(img_str); |
| 78 | |
| 79 | preprocess_image(image, image_payload._data__processed); |
| 80 | |
| 81 | // Push the image AFTER preprocessing so grid_h and grid_w are set |
| 82 | image_payload.images.push_back(image); |
| 83 | image_payload.num_images++; |
| 84 | } |
| 85 | } |
| 86 | try { |
| 87 | if (!input.messages.empty()) { // already a formated messages, usually from REST API |
| 88 | json qwenvl_message = json::array(); |
| 89 | std::vector<const std::string*> pending_images; |
| 90 | int total_images = 0; |
| 91 | for (const auto& item : input.messages) { |
| 92 | if (!item.contains("images")) { |
| 93 | qwenvl_message.push_back(item); |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | json newContent = json::array(); |
| 98 | for (const auto& img : item["images"]) { |
| 99 | if (img.is_string()) { |
| 100 | pending_images.push_back(&img.get_ref<const std::string&>()); |
| 101 | total_images++; |
| 102 | } |
| 103 | newContent.push_back({ |
| 104 | {"type", "image"} |
| 105 | }); |
| 106 | } |
| 107 | newContent.push_back({ |
| 108 | {"type", "text"}, |
| 109 | {"text", item["content"]} |
| 110 | }); |
| 111 | |
| 112 | json newItem = { |
| 113 | {"role", item["role"]}, |
| 114 | {"content", newContent} |
no test coverage detected