Skip to content

Commit

Permalink
feat: add schema for the plugin, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pminev committed Jan 7, 2025
1 parent b01b15f commit 28c6772
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ init_ac_plugin_option(TORTOISE)
#######################################
# packages

add_ac_local(0.1.3)
add_ac_local(0.1.4)
CPMAddPackage(gh:iboB/[email protected])
CPMAddPackage(gh:iboB/[email protected])

Expand Down
2 changes: 2 additions & 0 deletions ac-local-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) Alpaca Core
# SPDX-License-Identifier: MIT
#
add_subdirectory(schema)
add_subdirectory(schema-gen)
add_subdirectory(code)

if(AC_TORTOISE_BUILD_EXAMPLES)
Expand Down
1 change: 1 addition & 0 deletions ac-local-plugin/code/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ add_ac_local_plugin(
LocalTortoise.cpp
LIBRARIES
ac::tortoise
ac::tortoise.cpp-schema
)
34 changes: 22 additions & 12 deletions ac-local-plugin/code/LocalTortoise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <ac/local/Model.hpp>
#include <ac/local/ModelLoader.hpp>

#include <ac/schema/TortoiseCpp.hpp>
#include <ac/schema/DispatchHelpers.hpp>

#include <astl/move.hpp>
#include <astl/move_capture.hpp>
#include <astl/iile.h>
Expand All @@ -25,40 +28,47 @@ namespace {
class TortoiseInstance final : public Instance {
std::shared_ptr<tortoise::Model> m_model;
tortoise::Instance m_instance;
schema::OpDispatcherData m_dispatcherData;
public:
using Schema = ac::local::schema::TortoiseCppLoader::InstanceGeneral;
using Interface = ac::local::schema::TortoiseCppInterface;

TortoiseInstance(std::shared_ptr<tortoise::Model> model, tortoise::Instance::InitParams params)
: m_model(astl::move(model))
, m_instance(*m_model, astl::move(params))
{}
{
schema::registerHandlers<Interface::Ops>(m_dispatcherData, *this);
}

Dict runTTS(Dict& params) {
auto text = Dict_optValueAt(params, "text", std::string());
auto voicePath = Dict_optValueAt(params, "voicePath", std::string());
Interface::OpTTS::Return on(Interface::OpTTS, Interface::OpTTS::Params params) {
const auto& text = params.text.value();
const auto& voicePath = params.voicePath.value();

auto result = m_instance.textToSpeech(text, voicePath);

ac::Blob resultBlob;
resultBlob.resize(result.size() * sizeof(float));
memcpy(resultBlob.data(), result.data(), resultBlob.size());
Dict resultDict;
resultDict["result"] = std::move(resultBlob);
return resultDict;

return {
.result = std::move(resultBlob)
};
}

virtual Dict runOp(std::string_view op, Dict params, ProgressCb) override {
if (op == "tts") {
return runTTS(params);
auto ret = m_dispatcherData.dispatch(op, astl::move(params));
if (!ret) {
throw_ex{} << "tortoise: unknown op: " << op;
}

throw_ex{} << "tortoise: unknown op: " << op;
MSVC_WO_10766806();
return *ret;
}
};

class TortoiseModel final : public Model {
std::shared_ptr<tortoise::Model> m_model;
public:
using Schema = ac::local::schema::TortoiseCppLoader;

TortoiseModel(
std::string_view autoregressiveModelPath,
std::string_view diffusionModelPath,
Expand Down
10 changes: 10 additions & 0 deletions ac-local-plugin/schema-gen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) Alpaca Core
# SPDX-License-Identifier: MIT
#
add_executable(ac-tortoise.cpp-schema-gen)
target_sources(ac-tortoise.cpp-schema-gen PRIVATE
schema-gen.cpp
)
target_link_libraries(ac-tortoise.cpp-schema-gen PRIVATE
ac::tortoise.cpp-schema
)
12 changes: 12 additions & 0 deletions ac-local-plugin/schema-gen/schema-gen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Alpaca Core
// SPDX-License-Identifier: MIT
//
#include <ac/schema/TortoiseCpp.hpp>
#include <ac/schema/GenerateLoaderSchemaDict.hpp>
#include <iostream>

int main() {
auto d = ac::local::schema::generateLoaderSchema<acnl::ordered_json, ac::local::schema::TortoiseCppLoader>();
std::cout << d.dump(2) << std::endl;
return 0;
}
7 changes: 7 additions & 0 deletions ac-local-plugin/schema/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Alpaca Core
# SPDX-License-Identifier: MIT
#
add_library(ac-tortoise.cpp-schema INTERFACE)
add_library(ac::tortoise.cpp-schema ALIAS ac-tortoise.cpp-schema)
target_link_libraries(ac-tortoise.cpp-schema INTERFACE ac::schema)
target_include_directories(ac-tortoise.cpp-schema INTERFACE .)
63 changes: 63 additions & 0 deletions ac-local-plugin/schema/ac/schema/TortoiseCpp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) Alpaca Core
// SPDX-License-Identifier: MIT
//
#pragma once
#include <ac/schema/Field.hpp>
#include <ac/Dict.hpp>
#include <vector>
#include <string>
#include <tuple>

namespace ac::local::schema {

struct TortoiseCppInterface {
static inline constexpr std::string_view id = "tortoise.cpp";
static inline constexpr std::string_view description = "ilib-ac-tortoise.cpp-specific interface";

struct OpTTS {
static inline constexpr std::string_view id = "tts";
static inline constexpr std::string_view description = "Run the tortoise.cpp inference and produce audio";

struct Params {
Field<std::string> text;
Field<std::string> voicePath;

template <typename Visitor>
void visitFields(Visitor& v) {
v(text, "text", "Text to generate audio from");
v(voicePath, "voicePath", "Path to the voice model");
}
};

struct Return {
Field<ac::Blob> result;

template <typename Visitor>
void visitFields(Visitor& v) {
v(result, "result", "Audio of the text");
}
};
};

using Ops = std::tuple<OpTTS>;
};

struct TortoiseCppLoader {
static inline constexpr std::string_view id = "tortoise.cpp";
static inline constexpr std::string_view description = "Inference based on our fork of https://github.com/ggerganov/tortoise.cpp";

using Params = nullptr_t;

struct InstanceGeneral {
static inline constexpr std::string_view id = "general";
static inline constexpr std::string_view description = "General instance";

using Params = nullptr_t;

using Interfaces = std::tuple<TortoiseCppInterface>;
};

using Instances = std::tuple<InstanceGeneral>;
};

} // namespace ac::local::schema

0 comments on commit 28c6772

Please sign in to comment.