-
-
Save marty1885/0c10523aa9df618e60adb9d8c14c2eea to your computer and use it in GitHub Desktop.
client for vits-models in C++ https://huggingface.co/spaces/sayashi/vits-models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <drogon/drogon.h> | |
#include <trantor/utils/Utilities.h> | |
#include <drogon/WebSocketClient.h> | |
using namespace drogon; | |
#include <nlohmann/json.hpp> | |
Task<> service() | |
{ | |
auto client = WebSocketClient::newWebSocketClient("ws://127.0.0.1:7860"); | |
client->setMessageHandler([](std::string &&message, | |
const WebSocketClientPtr &client, | |
const WebSocketMessageType &type) | |
{ | |
if(type != WebSocketMessageType::Text) | |
return; | |
auto json = nlohmann::json::parse(message); | |
auto msg = json["msg"]; | |
static std::string session_hash = [](){ | |
std::vector<uint8_t> garbage(8); | |
utils::secureRandomBytes(garbage.data(), garbage.size()); | |
return utils::base64Encode(garbage.data(), garbage.size()); | |
}(); | |
if(msg == "send_hash") { | |
static bool first = true; | |
if(!first) { | |
LOG_ERROR << "The server re-requested the session hash. Something went wrong in the last request. Exit."; | |
client->stop(); | |
app().quit(); | |
} | |
first = false; | |
nlohmann::json j; | |
j["session_hash"] = session_hash; | |
j["fn_index"] = 0; | |
client->getConnection()->send(j.dump()); | |
} | |
else if(msg == "send_data") { | |
std::string text = "天気がいいですね。"; | |
nlohmann::json json; | |
json["fn_index"] = 0; | |
json["data"] = {text, "Japanese", 0.6, 0.668, 1, false}; | |
json["event_data"] = nullptr; | |
json["session_hash"] = session_hash; | |
client->getConnection()->send(json.dump()); | |
} | |
else if(msg == "process_completed") { | |
auto data = json["output"]["data"]; | |
auto encoded = data[1]; | |
auto b64_begin = encoded.get<std::string>().find(","); | |
auto b64 = encoded.get<std::string>().substr(b64_begin + 1); | |
auto decoded = utils::base64DecodeToVector(b64); | |
std::ofstream file("out.wav", std::ios::binary); | |
file.write((char*)decoded.data(), decoded.size()); | |
file.close(); | |
app().quit(); | |
} | |
return; | |
}); | |
auto req = HttpRequest::newHttpRequest(); | |
req->setPath("/queue/join"); | |
co_await client->connectToServerCoro(req); | |
LOG_INFO << "Connected to server"; | |
co_await untilQuit(app().getLoop()); | |
LOG_INFO << "Finished. exiting."; | |
} | |
int main() { | |
app().getLoop()->queueInLoop(async_func(service)); | |
app().run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment