Archived
radiance 1.8.0
- video: MiniMax-H3 and FastVideo's FastH3 8-Step V2, a video with its stereo soundtrack from text on OpenAI's /v1/videos, block fp8 on two cards - music: MiniMax-Music3, songs from lyrics and a description on /v1/audio/speech - speech: Breeze-TTS-2 on /v1/audio/speech and vLLM-Omni's realtime WebSocket - runtime LoRA adapters beside the base model, picked per request by `model` (baKon, #105) - several agent streams keep their cached history: --checkpoint-slots auto, the new default, sizes the snapshot store by --max-num-seqs (#108) - the Swift 1.5 27B W4 releases convert and serve: compressed-tensors AWQ and GPTQ int4, AutoRound, ModelOpt NVFP4 (#110) - the image's host tests pass where perf_event_open is refused, as in BuildKit's sandbox (#106) - plugin ABI 21: out-of-tree plugins must be rebuilt
This commit is contained in:
1 parent
91127395ee
commit
7a7707a955
179 files changed
+37542
-1892
No files matched your search
@@ -0,0 +1,621 @@
|
||||
/* videos.cpp -- OpenAI's video endpoints over a video generator (core/gen/video_driver.h): a video
|
||||
* and its soundtrack from a prompt.
|
||||
*
|
||||
* A video takes minutes, so it is a job, as OpenAI's API has it: POST /v1/videos queues one and
|
||||
* answers with the video object at once, GET /v1/videos/{id} reports its status and progress, and
|
||||
* GET /v1/videos/{id}/content downloads the MP4 once it is completed (`variant` thumbnail gives
|
||||
* the first frame as WebP). GET /v1/videos lists the jobs, newest first, and DELETE removes one,
|
||||
* cancelling it when it is still running. A finished video is kept for `video_ttl_s` seconds
|
||||
* (`expires_at`), at most `max_videos` of them.
|
||||
*
|
||||
* The request takes OpenAI's fields (`prompt`, `model`, `seconds`, `size`) as JSON or as a
|
||||
* multipart form, and SGLang's for the same model: `task` (t2va), `target` {`short_edge`,
|
||||
* `aspect_ratio`, `duration_seconds`}, `conditions` (empty), `seed`, and `num_inference_steps`.
|
||||
* `size` is the frame's width x height, multiples of the model's size multiple; `aspect_ratio`
|
||||
* (`w:h`) instead lays the model's own canvas over that ratio: the short edge (the model's, or
|
||||
* `short_edge`), the area capped at the model's largest, both sides rounded to the multiple.
|
||||
* A video's length is a whole number of the model's chunks: `seconds` is rounded up to the next.
|
||||
* Nothing that conditions on media is served: `input_reference` and non-empty `conditions` are
|
||||
* refused.
|
||||
*
|
||||
* The prompt is the model's template with `{prompt}` filled; the template is tokenised with its
|
||||
* special tokens and the prompt without, so a caller cannot write control tokens into it.
|
||||
*/
|
||||
#include "server.h"
|
||||
#include "oai.h"
|
||||
|
||||
#include "gen/video.h"
|
||||
#include "mm/media.h"
|
||||
#include "mm/mp4.h"
|
||||
#include "rad_internal.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
namespace rad {
|
||||
namespace server {
|
||||
|
||||
struct Server::VideoJob {
|
||||
std::string id, model, prompt, seconds, size;
|
||||
int64_t created_at = 0;
|
||||
uint64_t seed = 0;
|
||||
std::shared_ptr<gen::VideoTicket> ticket;
|
||||
std::thread worker;
|
||||
/* Under Server::videos_m_ from here on. */
|
||||
std::string status = "queued";
|
||||
int progress = 0;
|
||||
int64_t completed_at = 0, expires_at = 0;
|
||||
bool failed = false;
|
||||
std::string err_code, err_message;
|
||||
std::string mp4;
|
||||
std::vector<uint8_t> first; /* the first frame, RGB, for the thumbnail */
|
||||
int32_t w = 0, h = 0;
|
||||
bool worker_done = false; /* its thread has nothing left to do but return */
|
||||
};
|
||||
|
||||
namespace {
|
||||
|
||||
std::string new_video_id() {
|
||||
static const char* hex = "0123456789abcdef";
|
||||
std::random_device rd;
|
||||
std::string id = "video_";
|
||||
for (int i = 0; i < 32; ++i) id.push_back(hex[rd() & 15]);
|
||||
return id;
|
||||
}
|
||||
|
||||
/* "1344x768" -> (1344, 768); false for anything else. */
|
||||
bool parse_size(const std::string& s, int64_t* w, int64_t* h) {
|
||||
const size_t x = s.find_first_of("xX");
|
||||
if (x == std::string::npos || x == 0 || x + 1 >= s.size()) return false;
|
||||
char* e = nullptr;
|
||||
*w = std::strtoll(s.c_str(), &e, 10);
|
||||
if (e != s.c_str() + x) return false;
|
||||
*h = std::strtoll(s.c_str() + x + 1, &e, 10);
|
||||
return *e == '\0' && *w > 0 && *h > 0;
|
||||
}
|
||||
|
||||
/* "16:9" -> 16/9 as two positives; false for anything else. */
|
||||
bool parse_ratio(const std::string& s, double* a, double* b) {
|
||||
const size_t c = s.find(':');
|
||||
if (c == std::string::npos) return false;
|
||||
char* e = nullptr;
|
||||
*a = std::strtod(s.c_str(), &e);
|
||||
if (e != s.c_str() + c) return false;
|
||||
*b = std::strtod(s.c_str() + c + 1, &e);
|
||||
return *e == '\0' && *a > 0 && *b > 0;
|
||||
}
|
||||
|
||||
/* The model's canvas for a ratio: the short edge, the area capped, both sides rounded to the
|
||||
* multiple (ties to even, as the reference's float round). */
|
||||
void canvas_for(double rw, double rh, int64_t short_edge, int64_t max_area, int64_t mult,
|
||||
int64_t* w, int64_t* h) {
|
||||
const double ratio = rw / rh;
|
||||
double W = ratio >= 1.0 ? short_edge * ratio : (double)short_edge;
|
||||
double H = ratio >= 1.0 ? (double)short_edge : short_edge / ratio;
|
||||
if (W * H > (double)max_area) {
|
||||
const double sc = std::sqrt((double)max_area / (W * H));
|
||||
W *= sc;
|
||||
H *= sc;
|
||||
}
|
||||
*w = std::max<int64_t>(mult, (int64_t)std::nearbyint(W / mult) * mult);
|
||||
*h = std::max<int64_t>(mult, (int64_t)std::nearbyint(H / mult) * mult);
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
if (!deps_.videos || !deps_.tok)
|
||||
return error_response(err_not_implemented(
|
||||
"/v1/videos requires a video generation model; this one does not make video"),
|
||||
req.peer);
|
||||
const VideoGenInfo& info = deps_.videos->info();
|
||||
|
||||
/* JSON, or a form whose fields are strings (OpenAI's clients send the create call as a form,
|
||||
* since it may carry a file). */
|
||||
json body = json::object();
|
||||
if (!req.form.empty()) {
|
||||
for (const FormPart& p : req.form) {
|
||||
if (p.name == "input_reference")
|
||||
return error_response(err_unsupported("input_reference", "this server makes video "
|
||||
"from text only"), req.peer);
|
||||
if (!p.filename.empty())
|
||||
return error_response(err_bad(p.name, "'" + p.name + "' takes text, not a file"),
|
||||
req.peer);
|
||||
json v = p.data;
|
||||
if (p.name == "target" || p.name == "conditions") {
|
||||
v = json::parse(p.data, nullptr, false);
|
||||
if (v.is_discarded())
|
||||
return error_response(err_bad(p.name, "expected JSON"), req.peer);
|
||||
}
|
||||
body[p.name] = v;
|
||||
}
|
||||
} else {
|
||||
ApiError err;
|
||||
if (parse_request_object(req.body, lim_, &body, &err) < 0)
|
||||
return error_response(err, req.peer);
|
||||
}
|
||||
static const char* kKnown[] = {
|
||||
"model", "prompt", "seconds", "size", "input_reference", "seed", "num_inference_steps",
|
||||
"task", "target", "conditions", "user",
|
||||
};
|
||||
for (auto it = body.begin(); it != body.end(); ++it) {
|
||||
bool known = false;
|
||||
for (const char* k : kKnown) known = known || it.key() == k;
|
||||
if (!known)
|
||||
return error_response(err_bad(it.key(), "'" + it.key() + "' is not a field of "
|
||||
"/v1/videos"), req.peer);
|
||||
}
|
||||
auto bad = [&](const char* k, const std::string& why) { return error_response(err_bad(k, why), req.peer); };
|
||||
auto has = [&](const json& o, const char* k) { return o.contains(k) && !o[k].is_null(); };
|
||||
/* A number, or a string holding one (a form's fields are strings). */
|
||||
auto number = [&](const json& v, double* out) {
|
||||
if (v.is_number()) { *out = v.get<double>(); return true; }
|
||||
if (!v.is_string()) return false;
|
||||
const std::string s = v.get<std::string>();
|
||||
char* e = nullptr;
|
||||
*out = std::strtod(s.c_str(), &e);
|
||||
return !s.empty() && *e == '\0';
|
||||
};
|
||||
|
||||
if (!has(body, "prompt") || !body["prompt"].is_string() ||
|
||||
body["prompt"].get<std::string>().find_first_not_of(" \t\r\n") == std::string::npos)
|
||||
return bad("prompt", "the prompt is required, as a non-empty string");
|
||||
const std::string prompt = body["prompt"].get<std::string>();
|
||||
if (has(body, "input_reference"))
|
||||
return error_response(err_unsupported("input_reference", "this server makes video from "
|
||||
"text only"), req.peer);
|
||||
if (has(body, "task") && (!body["task"].is_string() || body["task"].get<std::string>() != "t2va")) {
|
||||
ApiError e = err_unsupported("task", "this server makes video from text: t2va");
|
||||
e.supported = { "t2va" };
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
if (has(body, "conditions") && (!body["conditions"].is_array() || !body["conditions"].empty()))
|
||||
return error_response(err_unsupported("conditions", "this server makes video from text "
|
||||
"only; conditions must be empty"), req.peer);
|
||||
json target = json::object();
|
||||
if (has(body, "target")) {
|
||||
if (!body["target"].is_object()) return bad("target", "expected an object");
|
||||
target = body["target"];
|
||||
for (auto it = target.begin(); it != target.end(); ++it)
|
||||
if (it.key() != "short_edge" && it.key() != "aspect_ratio" &&
|
||||
it.key() != "duration_seconds")
|
||||
return bad("target", "'" + it.key() + "' is not a field of target");
|
||||
}
|
||||
|
||||
/* ---- the size */
|
||||
int64_t W = info.default_width, H = info.default_height;
|
||||
const int64_t short_def = std::min(info.default_width, info.default_height);
|
||||
if (has(body, "size")) {
|
||||
if (!body["size"].is_string() || !parse_size(body["size"].get<std::string>(), &W, &H))
|
||||
return bad("size", "expected WIDTHxHEIGHT, e.g. " + std::to_string(info.default_width) +
|
||||
"x" + std::to_string(info.default_height));
|
||||
if (has(target, "aspect_ratio") || has(target, "short_edge"))
|
||||
return bad("size", "give size or target's aspect_ratio and short_edge, not both");
|
||||
} else if (has(target, "aspect_ratio") || has(target, "short_edge")) {
|
||||
double rw = (double)info.default_width, rh = (double)info.default_height;
|
||||
if (has(target, "aspect_ratio")) {
|
||||
if (!target["aspect_ratio"].is_string())
|
||||
return bad("target", "aspect_ratio is a string, w:h or auto");
|
||||
const std::string ar = target["aspect_ratio"].get<std::string>();
|
||||
if (ar != "auto" && !parse_ratio(ar, &rw, &rh))
|
||||
return bad("target", "aspect_ratio must be w:h (16:9) or auto");
|
||||
}
|
||||
int64_t se = short_def;
|
||||
if (has(target, "short_edge")) {
|
||||
double v = 0;
|
||||
if (!number(target["short_edge"], &v) || v < 1 || v != std::floor(v))
|
||||
return bad("target", "short_edge must be a positive whole number of pixels");
|
||||
se = (int64_t)v;
|
||||
}
|
||||
if (std::max(rw, rh) > (double)info.max_aspect * std::min(rw, rh))
|
||||
return bad("target", "the aspect ratio may be at most " +
|
||||
std::to_string(info.max_aspect) + ":1 either way");
|
||||
canvas_for(rw, rh, se, info.max_area, info.size_multiple, &W, &H);
|
||||
}
|
||||
if (W % info.size_multiple || H % info.size_multiple || W < info.min_side ||
|
||||
H < info.min_side || W * H > info.max_area ||
|
||||
std::max(W, H) > info.max_aspect * std::min(W, H)) {
|
||||
int64_t sw = 0, sh = 0;
|
||||
canvas_for((double)W, (double)H, short_def, info.max_area, info.size_multiple, &sw, &sh);
|
||||
return bad("size", std::to_string(W) + "x" + std::to_string(H) + " is not a size this "
|
||||
"model makes: sides are multiples of " + std::to_string(info.size_multiple) +
|
||||
" from " + std::to_string(info.min_side) + ", at most " +
|
||||
std::to_string(info.max_area) + " pixels and " +
|
||||
std::to_string(info.max_aspect) + ":1; its canvas at that ratio is " +
|
||||
std::to_string(sw) + "x" + std::to_string(sh));
|
||||
}
|
||||
|
||||
/* ---- the length, in whole chunks */
|
||||
const double fps = (double)info.fps_num / (double)info.fps_den;
|
||||
const double min_s = (double)(info.frames_per_chunk * info.min_chunks + info.frames_tail) / fps;
|
||||
const double max_s = (double)(info.frames_per_chunk * info.max_chunks + info.frames_tail) / fps;
|
||||
int64_t chunks = info.default_chunks;
|
||||
const bool has_sec = has(body, "seconds"), has_dur = has(target, "duration_seconds");
|
||||
if (has_sec && has_dur)
|
||||
return bad("seconds", "give seconds or target's duration_seconds, not both");
|
||||
if (has_sec || has_dur) {
|
||||
double sec = 0;
|
||||
if (!number(has_sec ? body["seconds"] : target["duration_seconds"], &sec) || !(sec > 0))
|
||||
return bad(has_sec ? "seconds" : "target", "the length is a positive number of seconds");
|
||||
const int64_t want = (int64_t)std::ceil(sec * fps - 1e-9);
|
||||
chunks = std::max<int64_t>(info.min_chunks,
|
||||
(want - info.frames_tail + info.frames_per_chunk - 1) /
|
||||
info.frames_per_chunk);
|
||||
if (chunks > info.max_chunks)
|
||||
return bad(has_sec ? "seconds" : "target",
|
||||
"this model makes " + rad::fmt("%.2f", min_s) + " to " +
|
||||
rad::fmt("%.2f", max_s) + " seconds");
|
||||
}
|
||||
const int64_t frames = info.frames_per_chunk * chunks + info.frames_tail;
|
||||
|
||||
std::unique_ptr<gen::VideoRequest> r = std::make_unique<gen::VideoRequest>();
|
||||
r->width = (int32_t)W;
|
||||
r->height = (int32_t)H;
|
||||
r->chunks = (int32_t)chunks;
|
||||
if (has(body, "num_inference_steps")) {
|
||||
double v = 0;
|
||||
if (info.fixed_steps && (!number(body["num_inference_steps"], &v) ||
|
||||
v != (double)info.max_steps))
|
||||
return bad("num_inference_steps",
|
||||
"this model runs the schedule it was trained on: " +
|
||||
std::to_string(info.max_steps) + " sigma grid points, the last one 0");
|
||||
if (!number(body["num_inference_steps"], &v) || v != std::floor(v) || v < 2 ||
|
||||
v > (double)info.max_steps)
|
||||
return bad("num_inference_steps", "expected an integer in 2.." +
|
||||
std::to_string(info.max_steps) +
|
||||
" (sigma grid points, the last one 0)");
|
||||
r->steps = (int32_t)v;
|
||||
}
|
||||
bool seed_given = false;
|
||||
if (has(body, "seed")) {
|
||||
double v = 0;
|
||||
if (!number(body["seed"], &v) || v != std::floor(v)) return bad("seed", "expected an integer");
|
||||
r->seed = (uint64_t)(int64_t)v;
|
||||
seed_given = v != -1;
|
||||
}
|
||||
if (!seed_given) {
|
||||
/* 53 bits, as the other generators choose one: JavaScript reads a number as a double. */
|
||||
std::random_device rd;
|
||||
r->seed = (((uint64_t)rd() << 32) ^ rd()) & (((uint64_t)1 << 53) - 1);
|
||||
}
|
||||
|
||||
/* ---- the prompt: the template's text with special tokens, the prompt without */
|
||||
{
|
||||
static const std::string kSlot = "{prompt}";
|
||||
const std::string& t = info.tmpl;
|
||||
const size_t p = t.find(kSlot);
|
||||
auto put = [&](const std::string& s, bool special) {
|
||||
if (s.empty()) return;
|
||||
const auto ids = deps_.tok->encode(s, false, special);
|
||||
r->tokens.insert(r->tokens.end(), ids.begin(), ids.end());
|
||||
};
|
||||
put(t.substr(0, p), true);
|
||||
put(prompt, false);
|
||||
put(t.substr(p + kSlot.size()), true);
|
||||
if ((int64_t)r->tokens.size() > info.max_text)
|
||||
return bad("prompt", "the prompt is " + std::to_string(r->tokens.size()) +
|
||||
" tokens; this model takes at most " +
|
||||
std::to_string(info.max_text));
|
||||
}
|
||||
|
||||
/* ---- admitted and queued: the job holds its place until its file is written */
|
||||
if (auto no = admit(1, req.peer)) return *no;
|
||||
auto j = std::make_shared<VideoJob>();
|
||||
j->id = new_video_id();
|
||||
j->model = opt_.model_id;
|
||||
j->prompt = prompt;
|
||||
j->seconds = rad::fmt("%.2f", (double)frames / fps);
|
||||
j->size = std::to_string(W) + "x" + std::to_string(H);
|
||||
j->created_at = unix_now();
|
||||
j->seed = r->seed;
|
||||
j->ticket = std::make_shared<gen::VideoTicket>();
|
||||
r->ticket = j->ticket;
|
||||
const int64_t n_tok = (int64_t)r->tokens.size();
|
||||
const int32_t steps = r->steps;
|
||||
{
|
||||
std::string w;
|
||||
const int s = deps_.videos->submit(std::move(r), &w);
|
||||
if (s < 0) {
|
||||
release(1);
|
||||
ApiError e = err_bad("", w.empty() ? std::string(rad_strerror(s)) : w);
|
||||
if (s != RAD_E_INVAL) { e.status = 503; e.type = "server_error"; }
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
}
|
||||
RAD_INFO("video %s: %s, %lld frames (%s s), %lld prompt tokens, %d steps, seed=%llu",
|
||||
j->id.c_str(), j->size.c_str(), (long long)frames, j->seconds.c_str(),
|
||||
(long long)n_tok, steps > 0 ? steps : (int)j->ticket->steps,
|
||||
(unsigned long long)j->seed);
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
sweep_videos_locked(unix_now());
|
||||
videos_.push_back(j);
|
||||
j->worker = std::thread([this, j] { video_work(j); });
|
||||
return HttpResponse::json_body(200, json_dump(video_json(*j)));
|
||||
}
|
||||
|
||||
/* The job's thread. Progress is the steps' share of 90, then the frames' of the last 10. */
|
||||
void Server::video_work(std::shared_ptr<VideoJob> j) {
|
||||
gen::VideoTicket& t = *j->ticket;
|
||||
const VideoGenInfo& info = deps_.videos->info();
|
||||
mm::Mp4Writer mp4;
|
||||
std::string why;
|
||||
int st = mp4.open(t.width, t.height, info.fps_num, info.fps_den, t.sample_rate, t.channels,
|
||||
&why);
|
||||
int32_t frames = 0;
|
||||
bool aborted = false;
|
||||
while (st >= 0) {
|
||||
gen::VideoEvent e;
|
||||
bool to = false;
|
||||
if (!t.next(&e, 500, &to)) {
|
||||
if (!to) break;
|
||||
if (stopping_.load()) {
|
||||
t.cancel.store(true);
|
||||
aborted = true;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (e.kind == gen::VideoEvent::STEP) {
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
j->status = "in_progress";
|
||||
j->progress = t.steps > 0 ? (int)(90LL * e.steps_done / t.steps) : 0;
|
||||
} else if (e.kind == gen::VideoEvent::AUDIO) {
|
||||
if (t.channels > 0)
|
||||
st = mp4.add_audio(e.samples.data(), (int64_t)e.samples.size() / t.channels, &why);
|
||||
} else if (e.kind == gen::VideoEvent::FRAMES) {
|
||||
if (e.first == 0 && e.n > 0) {
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
j->first.assign(e.rgb.begin(), e.rgb.begin() + (size_t)t.width * t.height * 3);
|
||||
j->w = t.width;
|
||||
j->h = t.height;
|
||||
}
|
||||
st = mp4.add_frames(e.rgb.data(), e.n, &why);
|
||||
frames += e.n;
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
j->progress = 90 + (t.frames > 0 ? (int)(10LL * frames / t.frames) : 0);
|
||||
}
|
||||
}
|
||||
if (st < 0) t.cancel.store(true);
|
||||
std::string file;
|
||||
if (st >= 0 && !aborted && t.status() >= 0 && frames == t.frames)
|
||||
st = mp4.finish(&file, &why);
|
||||
release(1);
|
||||
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
const int64_t now = unix_now();
|
||||
if (st >= 0 && !aborted && t.status() >= 0 && frames == t.frames) {
|
||||
j->status = "completed";
|
||||
j->progress = 100;
|
||||
j->completed_at = now;
|
||||
j->expires_at = now + opt_.video_ttl_s;
|
||||
j->mp4 = std::move(file);
|
||||
RAD_INFO("video %s: completed, %.1f MB (%s), %.1f s", j->id.c_str(),
|
||||
(double)j->mp4.size() / 1e6, mp4.video_encoder().c_str(),
|
||||
(double)(t.t_done_ns - t.t_submit_ns) / 1e9);
|
||||
} else {
|
||||
j->status = "failed";
|
||||
j->failed = true;
|
||||
j->completed_at = now;
|
||||
if (st < 0) {
|
||||
j->err_code = "encoding_failed";
|
||||
j->err_message = "the video could not be written: " + why;
|
||||
} else if (aborted || t.status() == RAD_E_CANCELLED) {
|
||||
j->err_code = "cancelled";
|
||||
j->err_message = aborted ? "the server is shutting down" : "the video was cancelled";
|
||||
} else {
|
||||
j->err_code = t.status() == RAD_E_INVAL ? "invalid_request" : "generation_failed";
|
||||
j->err_message = t.why().empty() ? std::string(rad_strerror(t.status())) : t.why();
|
||||
}
|
||||
RAD_WARN("video %s: failed: %s", j->id.c_str(), j->err_message.c_str());
|
||||
}
|
||||
j->worker_done = true;
|
||||
}
|
||||
|
||||
void Server::sweep_videos_locked(int64_t now) {
|
||||
/* Deleted jobs whose threads are done are joined. */
|
||||
for (size_t i = 0; i < videos_gone_.size();) {
|
||||
if (!videos_gone_[i]->worker_done) { ++i; continue; }
|
||||
if (videos_gone_[i]->worker.joinable()) videos_gone_[i]->worker.join();
|
||||
videos_gone_.erase(videos_gone_.begin() + (std::ptrdiff_t)i);
|
||||
}
|
||||
/* Expired files go first, then the oldest finished jobs past the count. A running job is
|
||||
* never dropped here: it holds an admission place and its thread. */
|
||||
auto drop = [&](size_t i) {
|
||||
if (videos_[i]->worker.joinable()) videos_[i]->worker.join();
|
||||
videos_.erase(videos_.begin() + (std::ptrdiff_t)i);
|
||||
};
|
||||
for (size_t i = 0; i < videos_.size();) {
|
||||
const VideoJob& j = *videos_[i];
|
||||
const bool done = j.status == "completed" || j.status == "failed";
|
||||
if (done && j.expires_at > 0 && j.expires_at <= now) drop(i);
|
||||
else if (done && j.failed && j.completed_at + opt_.video_ttl_s <= now) drop(i);
|
||||
else ++i;
|
||||
}
|
||||
int64_t finished = 0;
|
||||
for (const auto& j : videos_) finished += j->status == "completed" || j->status == "failed";
|
||||
for (size_t i = 0; i < videos_.size() && finished > opt_.max_videos;) {
|
||||
const VideoJob& j = *videos_[i];
|
||||
if (j.status == "completed" || j.status == "failed") {
|
||||
drop(i);
|
||||
--finished;
|
||||
} else {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::stop_videos() {
|
||||
std::vector<std::shared_ptr<VideoJob>> all;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
all = videos_;
|
||||
all.insert(all.end(), videos_gone_.begin(), videos_gone_.end());
|
||||
}
|
||||
for (auto& j : all) j->ticket->cancel.store(true);
|
||||
for (auto& j : all)
|
||||
if (j->worker.joinable()) j->worker.join();
|
||||
}
|
||||
|
||||
/* The video object, OpenAI's shape. */
|
||||
json Server::video_json(const VideoJob& j) {
|
||||
json o;
|
||||
o["id"] = j.id;
|
||||
o["object"] = "video";
|
||||
o["model"] = j.model;
|
||||
o["status"] = j.status;
|
||||
o["progress"] = j.progress;
|
||||
o["created_at"] = j.created_at;
|
||||
o["completed_at"] = j.completed_at ? json(j.completed_at) : json(nullptr);
|
||||
o["expires_at"] = j.expires_at ? json(j.expires_at) : json(nullptr);
|
||||
o["prompt"] = j.prompt;
|
||||
o["seconds"] = j.seconds;
|
||||
o["size"] = j.size;
|
||||
o["seed"] = j.seed;
|
||||
o["remixed_from_video_id"] = nullptr;
|
||||
if (j.failed) {
|
||||
json e;
|
||||
e["code"] = j.err_code;
|
||||
e["message"] = j.err_message;
|
||||
o["error"] = e;
|
||||
} else {
|
||||
o["error"] = nullptr;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
HttpResponse Server::handle_video_get(const HttpRequest& req) {
|
||||
if (!deps_.videos)
|
||||
return error_response(err_not_implemented("/v1/videos requires a video generation model"),
|
||||
req.peer);
|
||||
const std::string id = req.param("id");
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
for (const auto& j : videos_)
|
||||
if (j->id == id) return HttpResponse::json_body(200, json_dump(video_json(*j)));
|
||||
ApiError e = err_bad("id", "no video '" + id + "'; it may have expired");
|
||||
e.status = 404;
|
||||
e.type = "not_found_error";
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
|
||||
HttpResponse Server::handle_video_list(const HttpRequest& req) {
|
||||
if (!deps_.videos)
|
||||
return error_response(err_not_implemented("/v1/videos requires a video generation model"),
|
||||
req.peer);
|
||||
int64_t limit = 20;
|
||||
if (auto it = req.query.find("limit"); it != req.query.end()) {
|
||||
char* e = nullptr;
|
||||
limit = std::strtoll(it->second.c_str(), &e, 10);
|
||||
if (*e || limit < 1 || limit > 100)
|
||||
return error_response(err_bad("limit", "expected an integer in 1..100"), req.peer);
|
||||
}
|
||||
const auto ord = req.query.find("order");
|
||||
const bool asc = ord != req.query.end() && ord->second == "asc";
|
||||
if (ord != req.query.end() && ord->second != "asc" && ord->second != "desc")
|
||||
return error_response(err_bad("order", "expected asc or desc"), req.peer);
|
||||
const auto after = req.query.find("after");
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
sweep_videos_locked(unix_now());
|
||||
std::vector<std::shared_ptr<VideoJob>> v = videos_;
|
||||
if (!asc) std::reverse(v.begin(), v.end());
|
||||
size_t at = 0;
|
||||
if (after != req.query.end()) {
|
||||
while (at < v.size() && v[at]->id != after->second) ++at;
|
||||
if (at == v.size())
|
||||
return error_response(err_bad("after", "no video '" + after->second + "'"), req.peer);
|
||||
++at;
|
||||
}
|
||||
json data = json::array();
|
||||
for (size_t i = at; i < v.size() && (int64_t)data.size() < limit; ++i)
|
||||
data.push_back(video_json(*v[i]));
|
||||
json o;
|
||||
o["object"] = "list";
|
||||
o["data"] = data;
|
||||
o["first_id"] = data.empty() ? json(nullptr) : data.front()["id"];
|
||||
o["last_id"] = data.empty() ? json(nullptr) : data.back()["id"];
|
||||
o["has_more"] = at + data.size() < v.size();
|
||||
return HttpResponse::json_body(200, json_dump(o));
|
||||
}
|
||||
|
||||
HttpResponse Server::handle_video_delete(const HttpRequest& req) {
|
||||
if (!deps_.videos)
|
||||
return error_response(err_not_implemented("/v1/videos requires a video generation model"),
|
||||
req.peer);
|
||||
const std::string id = req.param("id");
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
std::shared_ptr<VideoJob> j;
|
||||
for (size_t i = 0; i < videos_.size(); ++i)
|
||||
if (videos_[i]->id == id) {
|
||||
j = videos_[i];
|
||||
videos_.erase(videos_.begin() + (std::ptrdiff_t)i);
|
||||
break;
|
||||
}
|
||||
if (!j) {
|
||||
ApiError e = err_bad("id", "no video '" + id + "'");
|
||||
e.status = 404;
|
||||
e.type = "not_found_error";
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
/* A running job is cancelled, and its thread ends once the driver has seen that (at its next
|
||||
* pass, which may be seconds away); it is joined then. */
|
||||
j->ticket->cancel.store(true);
|
||||
videos_gone_.push_back(j);
|
||||
sweep_videos_locked(unix_now());
|
||||
json o;
|
||||
o["id"] = id;
|
||||
o["object"] = "video.deleted";
|
||||
o["deleted"] = true;
|
||||
return HttpResponse::json_body(200, json_dump(o));
|
||||
}
|
||||
|
||||
HttpResponse Server::handle_video_content(const HttpRequest& req) {
|
||||
if (!deps_.videos)
|
||||
return error_response(err_not_implemented("/v1/videos requires a video generation model"),
|
||||
req.peer);
|
||||
const std::string id = req.param("id");
|
||||
std::string variant = "video";
|
||||
if (auto it = req.query.find("variant"); it != req.query.end()) variant = it->second;
|
||||
if (variant != "video" && variant != "thumbnail") {
|
||||
ApiError e = err_unsupported("variant", "this server gives the video or its thumbnail");
|
||||
e.supported = { "video", "thumbnail" };
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
std::lock_guard<std::mutex> lk(videos_m_);
|
||||
for (const auto& j : videos_) {
|
||||
if (j->id != id) continue;
|
||||
if (variant == "thumbnail") {
|
||||
if (j->first.empty())
|
||||
return error_response(err_bad("id", "video '" + id + "' has no frames yet"),
|
||||
req.peer);
|
||||
std::vector<uint8_t> file;
|
||||
std::string why;
|
||||
if (mm::encode_image(j->first.data(), j->w, j->h, 3, "webp", 90, &file, &why) < 0)
|
||||
return error_response(err_internal("the thumbnail could not be encoded: " + why),
|
||||
req.peer);
|
||||
HttpResponse res;
|
||||
res.content_type = "image/webp";
|
||||
res.body.assign((const char*)file.data(), file.size());
|
||||
return res;
|
||||
}
|
||||
if (j->status != "completed")
|
||||
return error_response(err_bad("id", "video '" + id + "' is " + j->status +
|
||||
(j->failed ? ": " + j->err_message : "")),
|
||||
req.peer);
|
||||
HttpResponse res;
|
||||
res.content_type = "video/mp4";
|
||||
res.body = j->mp4;
|
||||
return res;
|
||||
}
|
||||
ApiError e = err_bad("id", "no video '" + id + "'; it may have expired");
|
||||
e.status = 404;
|
||||
e.type = "not_found_error";
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
|
||||
} /* namespace server */
|
||||
} /* namespace rad */
|
||||
Reference in new issue
Block a user