5252#endif
5353namespace ovms {
5454
55+ static std::string inMemoryGraphContent;
56+
57+ bool GraphExport::hasInMemoryGraphContent () {
58+ return !inMemoryGraphContent.empty ();
59+ }
60+
61+ const std::string& GraphExport::getInMemoryGraphContent () {
62+ return inMemoryGraphContent;
63+ }
64+
65+ void GraphExport::clearInMemoryGraphContent () {
66+ inMemoryGraphContent.clear ();
67+ }
68+
5569static const std::string OVMS_VERSION_GRAPH_LINE = std::string(" # File created with: " ) + PROJECT_NAME + std::string(" " ) + PROJECT_VERSION + std::string(" \n " );
5670
5771static std::string constructModelsPath (const std::string& modelPath, const std::optional<std::string>& ggufFilenameOpt) {
@@ -91,22 +105,26 @@ std::string GraphExport::getDraftModelDirectoryPath(const std::string& directory
91105 } \
92106 auto pluginConfigOpt = std::get<std::optional<std::string>>(pluginConfigOrStatus)
93107
94- static Status createPbtxtFile (const std::string& directoryPath, const std::string& pbtxtContent) {
108+ static Status createPbtxtFile (const std::string& directoryPath, const std::string& pbtxtContent, bool writeToFile ) {
95109#if (MEDIAPIPE_DISABLE == 0)
96110 ::mediapipe::CalculatorGraphConfig config;
97- SPDLOG_TRACE (" Generated pbtxt: {}" , pbtxtContent);
111+ SPDLOG_TRACE (" Created graph config file: \n {}" , pbtxtContent);
98112 bool success = ::google::protobuf::TextFormat::ParseFromString (pbtxtContent, &config);
99113 if (!success) {
100114 SPDLOG_ERROR (" Created graph config file couldn't be parsed - check used task parameters values." );
101115 return StatusCode::MEDIAPIPE_GRAPH_CONFIG_FILE_INVALID;
102116 }
103117#endif
118+ if (!writeToFile) {
119+ inMemoryGraphContent = pbtxtContent;
120+ return StatusCode::OK;
121+ }
104122 // clang-format on
105123 std::string fullPath = FileSystem::joinPath ({directoryPath, " graph.pbtxt" });
106124 return FileSystem::createFileOverwrite (fullPath, pbtxtContent);
107125}
108126
109- static Status createTextGenerationGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings) {
127+ static Status createTextGenerationGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings, bool writeToFile ) {
110128 if (!std::holds_alternative<TextGenGraphSettingsImpl>(hfSettings.graphSettings )) {
111129 SPDLOG_ERROR (" Graph options not initialized for text generation." );
112130 return StatusCode::INTERNAL_ERROR;
@@ -198,10 +216,10 @@ static Status createTextGenerationGraphTemplate(const std::string& directoryPath
198216 }
199217 }
200218 })" ;
201- return createPbtxtFile (directoryPath, oss.str ());
219+ return createPbtxtFile (directoryPath, oss.str (), writeToFile );
202220}
203221
204- static Status createRerankGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings) {
222+ static Status createRerankGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings, bool writeToFile ) {
205223 if (!std::holds_alternative<RerankGraphSettingsImpl>(hfSettings.graphSettings )) {
206224 SPDLOG_ERROR (" Graph options not initialized for reranking." );
207225 return StatusCode::INTERNAL_ERROR;
@@ -242,10 +260,10 @@ node {
242260 }
243261 }
244262})" ;
245- return createPbtxtFile (directoryPath, oss.str ());
263+ return createPbtxtFile (directoryPath, oss.str (), writeToFile );
246264}
247265
248- static Status createEmbeddingsGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings) {
266+ static Status createEmbeddingsGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings, bool writeToFile ) {
249267 if (!std::holds_alternative<EmbeddingsGraphSettingsImpl>(hfSettings.graphSettings )) {
250268 SPDLOG_ERROR (" Graph options not initialized for embeddings." );
251269 return StatusCode::INTERNAL_ERROR;
@@ -289,10 +307,10 @@ node {
289307 oss << R"( }
290308 }
291309})" ;
292- return createPbtxtFile (directoryPath, oss.str ());
310+ return createPbtxtFile (directoryPath, oss.str (), writeToFile );
293311}
294312
295- static Status createTextToSpeechGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings) {
313+ static Status createTextToSpeechGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings, bool writeToFile ) {
296314 if (!std::holds_alternative<TextToSpeechGraphSettingsImpl>(hfSettings.graphSettings )) {
297315 SPDLOG_ERROR (" Graph options not initialized for speech generation." );
298316 return StatusCode::INTERNAL_ERROR;
@@ -339,11 +357,15 @@ node {
339357 }
340358#endif
341359 // clang-format on
360+ if (!writeToFile) {
361+ inMemoryGraphContent = oss.str ();
362+ return StatusCode::OK;
363+ }
342364 std::string fullPath = FileSystem::joinPath ({directoryPath, " graph.pbtxt" });
343365 return FileSystem::createFileOverwrite (fullPath, oss.str ());
344366}
345367
346- static Status createSpeechToTextGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings) {
368+ static Status createSpeechToTextGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings, bool writeToFile ) {
347369 if (!std::holds_alternative<SpeechToTextGraphSettingsImpl>(hfSettings.graphSettings )) {
348370 SPDLOG_ERROR (" Graph options not initialized for speech to text." );
349371 return StatusCode::INTERNAL_ERROR;
@@ -405,11 +427,15 @@ node {
405427 }
406428#endif
407429 // clang-format on
430+ if (!writeToFile) {
431+ inMemoryGraphContent = oss.str ();
432+ return StatusCode::OK;
433+ }
408434 std::string fullPath = FileSystem::joinPath ({directoryPath, " graph.pbtxt" });
409435 return FileSystem::createFileOverwrite (fullPath, oss.str ());
410436}
411437
412- static Status createImageGenerationGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings) {
438+ static Status createImageGenerationGraphTemplate (const std::string& directoryPath, const HFSettingsImpl& hfSettings, bool writeToFile ) {
413439 if (!std::holds_alternative<ImageGenerationGraphSettingsImpl>(hfSettings.graphSettings )) {
414440 SPDLOG_ERROR (" Graph options not initialized for image generation." );
415441 return StatusCode::INTERNAL_ERROR;
@@ -489,13 +515,13 @@ node: {
489515}
490516)" ;
491517 // clang-format on
492- return createPbtxtFile (directoryPath, oss.str ());
518+ return createPbtxtFile (directoryPath, oss.str (), writeToFile );
493519}
494520
495521GraphExport::GraphExport () {
496522}
497523
498- Status GraphExport::createServableConfig (const std::string& directoryPath, const HFSettingsImpl& hfSettings) {
524+ Status GraphExport::createServableConfig (const std::string& directoryPath, const HFSettingsImpl& hfSettings, bool writeToFile ) {
499525 if (directoryPath.empty ()) {
500526 SPDLOG_ERROR (" Directory path empty: {}" , directoryPath);
501527 return StatusCode::PATH_INVALID;
@@ -518,17 +544,17 @@ Status GraphExport::createServableConfig(const std::string& directoryPath, const
518544 }
519545 }
520546 if (hfSettings.task == TEXT_GENERATION_GRAPH) {
521- return createTextGenerationGraphTemplate (directoryPath, hfSettings);
547+ return createTextGenerationGraphTemplate (directoryPath, hfSettings, writeToFile );
522548 } else if (hfSettings.task == EMBEDDINGS_GRAPH) {
523- return createEmbeddingsGraphTemplate (directoryPath, hfSettings);
549+ return createEmbeddingsGraphTemplate (directoryPath, hfSettings, writeToFile );
524550 } else if (hfSettings.task == RERANK_GRAPH) {
525- return createRerankGraphTemplate (directoryPath, hfSettings);
551+ return createRerankGraphTemplate (directoryPath, hfSettings, writeToFile );
526552 } else if (hfSettings.task == IMAGE_GENERATION_GRAPH) {
527- return createImageGenerationGraphTemplate (directoryPath, hfSettings);
553+ return createImageGenerationGraphTemplate (directoryPath, hfSettings, writeToFile );
528554 } else if (hfSettings.task == TEXT_TO_SPEECH_GRAPH) {
529- return createTextToSpeechGraphTemplate (directoryPath, hfSettings);
555+ return createTextToSpeechGraphTemplate (directoryPath, hfSettings, writeToFile );
530556 } else if (hfSettings.task == SPEECH_TO_TEXT_GRAPH) {
531- return createSpeechToTextGraphTemplate (directoryPath, hfSettings);
557+ return createSpeechToTextGraphTemplate (directoryPath, hfSettings, writeToFile );
532558 } else if (hfSettings.task == UNKNOWN_GRAPH) {
533559 SPDLOG_ERROR (" Graph options not initialized." );
534560 return StatusCode::INTERNAL_ERROR;
0 commit comments