MCPcopy Create free account
hub / github.com/OpenNMT/CTranslate2 / create_model_reader

Function create_model_reader

python/cpp/replica_pool.h:11–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9 namespace python {
10
11 inline std::shared_ptr<models::ModelReader>
12 create_model_reader(const std::string& model, py::object files) {
13 if (files.is_none())
14 return std::make_shared<models::ModelFileReader>(model);
15
16 if (!py::isinstance<py::dict>(files))
17 throw pybind11::type_error("files argument must be a dictionary mapping file names "
18 "to the file contents");
19
20 auto reader = std::make_shared<models::ModelMemoryReader>(model);
21
22 for (const auto& pair : files.cast<py::dict>()) {
23 auto filename = pair.first;
24 auto content = pair.second;
25
26 auto read = py::getattr(content, "read", py::none());
27 if (!read.is_none())
28 content = read();
29 else if (!py::isinstance<py::bytes>(content))
30 throw pybind11::type_error("File content must be a file-like or bytes object");
31
32 reader->register_file(filename.cast<std::string>(), content.cast<std::string>());
33 }
34
35 return reader;
36 }
37
38 template <typename T>
39 class ReplicaPoolHelper {

Callers 1

ReplicaPoolHelperMethod · 0.85

Calls 2

type_errorClass · 0.85
register_fileMethod · 0.45

Tested by

no test coverage detected