184 lines
6.9 KiB
C++
184 lines
6.9 KiB
C++
#include "xai/core/videos.h"
|
|||
|
|
|
||
|
|
#include "xai/core/caps.h"
|
||
|
|
|
||
|
|
namespace molo::xai::core {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
void write_common(Json& body, const std::string& model, const std::optional<StorageOptions>& storage,
|
||
|
|
const std::string& upload_url, const std::string& user) {
|
||
|
|
if (!model.empty()) {
|
||
|
|
Check::model(model);
|
||
|
|
body.set("model", Json(model));
|
||
|
|
}
|
||
|
|
if (storage) body.set("storage_options", storage->to_json());
|
||
|
|
if (!upload_url.empty()) {
|
||
|
|
Check::http_url("upload url", upload_url);
|
||
|
|
Json out = Json::object();
|
||
|
|
out.set("upload_url", Json(upload_url));
|
||
|
|
body.set("output", out);
|
||
|
|
}
|
||
|
|
if (!user.empty()) {
|
||
|
|
Check::text("user", user, 256);
|
||
|
|
body.set("user", Json(user));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string request_id_of(const Json& reply) {
|
||
|
|
std::string id = jx::str(reply, "request_id");
|
||
|
|
if (id.empty()) throw llm::ApiError(kVendor, 502, "BAD_RESPONSE", "reply has no request_id");
|
||
|
|
return id;
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
Json VideoGenerationRequest::to_json() const {
|
||
|
|
bool has_refs = !reference_images.empty() || !reference_audios.empty();
|
||
|
|
bool has_frames = image || last_frame || !keyframes.empty();
|
||
|
|
if (prompt.empty() && !has_refs && !has_frames) Check::fail("prompt is required without images or references");
|
||
|
|
Check::text("prompt", prompt, Caps::kMaxPromptBytes, true);
|
||
|
|
Json body = Json::object();
|
||
|
|
if (!prompt.empty()) body.set("prompt", Json(prompt));
|
||
|
|
if (duration) {
|
||
|
|
Check::range("duration", *duration, Caps::kMinVideoSeconds, Caps::kMaxVideoSeconds);
|
||
|
|
body.set("duration", Json(*duration));
|
||
|
|
}
|
||
|
|
if (!aspect_ratio.empty()) {
|
||
|
|
Check::one_of("aspect_ratio", aspect_ratio, {"1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "21:9", "5:2"});
|
||
|
|
body.set("aspect_ratio", Json(aspect_ratio));
|
||
|
|
}
|
||
|
|
if (!resolution.empty()) {
|
||
|
|
Check::one_of("resolution", resolution, {"480p", "720p", "1080p"});
|
||
|
|
body.set("resolution", Json(resolution));
|
||
|
|
}
|
||
|
|
if (image) body.set("image", image->to_json());
|
||
|
|
if (last_frame) body.set("last_frame", last_frame->to_json());
|
||
|
|
Check::upto("reference_images", reference_images.size(), Caps::kMaxReferenceImages);
|
||
|
|
if (!reference_images.empty()) {
|
||
|
|
Json arr = Json::array();
|
||
|
|
for (const auto& r : reference_images) arr.push(r.to_json());
|
||
|
|
body.set("reference_images", arr);
|
||
|
|
}
|
||
|
|
Check::upto("reference_audios", reference_audios.size(), Caps::kMaxReferenceAudios);
|
||
|
|
if (!reference_audios.empty()) {
|
||
|
|
Json arr = Json::array();
|
||
|
|
for (const auto& r : reference_audios) arr.push(r.to_json());
|
||
|
|
body.set("reference_audios", arr);
|
||
|
|
}
|
||
|
|
Check::upto("keyframes", keyframes.size(), Caps::kMaxKeyframes);
|
||
|
|
if (!keyframes.empty()) {
|
||
|
|
Json arr = Json::array();
|
||
|
|
for (const auto& k : keyframes) {
|
||
|
|
if (k.image.empty()) Check::fail("keyframe image is required");
|
||
|
|
double limit = duration ? *duration : Caps::kMaxVideoSeconds;
|
||
|
|
if (!(k.timestamp_s > 0 && k.timestamp_s < limit)) Check::fail("keyframe timestamp_s must be inside the clip");
|
||
|
|
Json o = Json::object();
|
||
|
|
o.set("image", k.image.to_json());
|
||
|
|
o.set("timestamp_s", Json(k.timestamp_s));
|
||
|
|
arr.push(o);
|
||
|
|
}
|
||
|
|
body.set("keyframes", arr);
|
||
|
|
}
|
||
|
|
write_common(body, model, storage, upload_url, user);
|
||
|
|
return body;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json VideoEditRequest::to_json() const {
|
||
|
|
Check::text("prompt", prompt, Caps::kMaxPromptBytes);
|
||
|
|
if (video.empty()) Check::fail("video is required");
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("prompt", Json(prompt));
|
||
|
|
body.set("video", video.to_json());
|
||
|
|
write_common(body, model, storage, upload_url, user);
|
||
|
|
return body;
|
||
|
|
}
|
||
|
|
|
||
|
|
Json VideoExtendRequest::to_json() const {
|
||
|
|
Check::text("prompt", prompt, Caps::kMaxPromptBytes);
|
||
|
|
if (video.empty()) Check::fail("video is required");
|
||
|
|
Json body = Json::object();
|
||
|
|
body.set("prompt", Json(prompt));
|
||
|
|
body.set("video", video.to_json());
|
||
|
|
if (duration) {
|
||
|
|
Check::range("duration", *duration, Caps::kMinExtensionSeconds, Caps::kMaxExtensionSeconds);
|
||
|
|
body.set("duration", Json(*duration));
|
||
|
|
}
|
||
|
|
write_common(body, model, storage, upload_url, "");
|
||
|
|
return body;
|
||
|
|
}
|
||
|
|
|
||
|
|
VideoJob VideoJob::parse(const Json& doc, const std::string& request_id) {
|
||
|
|
VideoJob j;
|
||
|
|
j.request_id = request_id;
|
||
|
|
j.raw = doc;
|
||
|
|
j.status = jx::str(doc, "status");
|
||
|
|
j.model = jx::str(doc, "model");
|
||
|
|
if (const Json* p = jx::get(doc, "progress"); p && p->is_number()) j.progress = p->as_int();
|
||
|
|
if (const Json* u = jx::get(doc, "usage"); u && u->is_object()) j.usage = MediaUsage::from_json(*u);
|
||
|
|
j.error = MediaError::parse(doc, "error");
|
||
|
|
if (const Json* v = jx::get(doc, "video"); v && v->is_object()) {
|
||
|
|
GeneratedVideo g;
|
||
|
|
g.url = jx::str(*v, "url");
|
||
|
|
g.storage_error = jx::str(*v, "storage_error");
|
||
|
|
g.duration = jx::num(*v, "duration");
|
||
|
|
g.respect_moderation = jx::flag(*v, "respect_moderation");
|
||
|
|
if (const Json* f = jx::get(*v, "file_output"); f && f->is_object()) g.file = StoredFile::parse(*f);
|
||
|
|
j.video = std::move(g);
|
||
|
|
}
|
||
|
|
if (j.status.empty()) j.status = j.error ? "failed" : (j.video ? "done" : "pending");
|
||
|
|
return j;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string VideoService::start_generate(const VideoGenerationRequest& request) {
|
||
|
|
Json body = request.to_json();
|
||
|
|
return request_id_of(call_json(Call::kVideosGenerate, {}, &body));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string VideoService::start_edit(const VideoEditRequest& request) {
|
||
|
|
Json body = request.to_json();
|
||
|
|
return request_id_of(call_json(Call::kVideosEdit, {}, &body));
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string VideoService::start_extend(const VideoExtendRequest& request) {
|
||
|
|
Json body = request.to_json();
|
||
|
|
return request_id_of(call_json(Call::kVideosExtend, {}, &body));
|
||
|
|
}
|
||
|
|
|
||
|
|
VideoJob VideoService::poll(const std::string& request_id) {
|
||
|
|
llm::RawResponse raw = call_raw(Call::kVideosGet, {request_id});
|
||
|
|
if (raw.status == 202) {
|
||
|
|
VideoJob j;
|
||
|
|
j.request_id = request_id;
|
||
|
|
j.status = "pending";
|
||
|
|
return j;
|
||
|
|
}
|
||
|
|
return VideoJob::parse(parse_body(raw), request_id);
|
||
|
|
}
|
||
|
|
|
||
|
|
VideoJob VideoService::wait(const std::string& request_id, const WaitPolicy& policy, const Sleeper& sleeper) {
|
||
|
|
return wait(request_id, policy, sleeper, Progress{});
|
||
|
|
}
|
||
|
|
|
||
|
|
VideoJob VideoService::wait(const std::string& request_id, const WaitPolicy& policy, const Sleeper& sleeper,
|
||
|
|
const Progress& progress) {
|
||
|
|
Check::id("request", request_id);
|
||
|
|
bool cancelled = false;
|
||
|
|
std::function<std::optional<VideoJob>()> step = [&]() -> std::optional<VideoJob> {
|
||
|
|
VideoJob j = poll(request_id);
|
||
|
|
if (progress && !progress(j)) {
|
||
|
|
cancelled = true;
|
||
|
|
return j;
|
||
|
|
}
|
||
|
|
if (j.pending()) return std::nullopt;
|
||
|
|
return j;
|
||
|
|
};
|
||
|
|
std::optional<VideoJob> j = poll_until<VideoJob>(policy, step, sleeper);
|
||
|
|
if (!j) throw llm::ApiError(kVendor, 504, "WAIT_TIMEOUT", "video not ready within the wait budget");
|
||
|
|
if (cancelled && j->pending()) throw llm::ApiError(kVendor, 499, "WAIT_CANCELLED", "wait stopped by the caller");
|
||
|
|
if (j->failed())
|
||
|
|
throw llm::ApiError(kVendor, 502, "GENERATION_FAILED",
|
||
|
|
j->error ? j->error->code + ": " + j->error->message : "video generation failed");
|
||
|
|
return *j;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace molo::xai::core
|