Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 5,353 Bytes
461b74e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | /**
* Client for the FastAPI annotation backend in `backend/`.
*
* The backend URL is configured via the `NEXT_PUBLIC_ANNOTATE_BACKEND_URL`
* env var so it can be statically substituted by Next.js. When unset, all
* annotation write paths are disabled and the UI falls back to sessionStorage
* for read/edit only.
*/
import type { LanguageAtom } from "../types/language.types";
const ENV_URL = (() => {
const v =
typeof process !== "undefined"
? process.env.NEXT_PUBLIC_ANNOTATE_BACKEND_URL
: undefined;
return (v || "").trim() || null;
})();
export function isAnnotateBackendEnabled(): boolean {
return !!ENV_URL;
}
export function getAnnotateBackendUrl(): string | null {
return ENV_URL;
}
interface DatasetIdent {
repoId?: string | null;
localPath?: string | null;
revision?: string | null;
}
function buildUrl(path: string, ident: DatasetIdent): string {
if (!ENV_URL) throw new Error("Annotate backend not configured");
const url = new URL(path, ENV_URL);
if (ident.repoId) url.searchParams.set("repo_id", ident.repoId);
if (ident.revision) url.searchParams.set("revision", ident.revision);
if (ident.localPath) url.searchParams.set("local_path", ident.localPath);
return url.toString();
}
export async function pingBackend(): Promise<boolean> {
if (!ENV_URL) return false;
try {
const res = await fetch(new URL("/api/health", ENV_URL).toString());
return res.ok;
} catch {
return false;
}
}
export async function loadDataset(
ident: DatasetIdent,
): Promise<{ ok: boolean }> {
if (!ENV_URL) return { ok: false };
const res = await fetch(new URL("/api/dataset/load", ENV_URL).toString(), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
repo_id: ident.repoId || null,
revision: ident.revision || null,
local_path: ident.localPath || null,
}),
});
return { ok: res.ok };
}
export async function fetchEpisodeAtoms(
episodeId: number,
ident: DatasetIdent,
): Promise<LanguageAtom[]> {
if (!ENV_URL) return [];
await loadDataset(ident);
const res = await fetch(buildUrl(`/api/episodes/${episodeId}/atoms`, ident));
if (!res.ok) {
throw new Error(`fetch atoms: ${res.status}`);
}
const data = (await res.json()) as { atoms?: LanguageAtom[] };
return data.atoms || [];
}
export async function saveEpisodeAtoms(
episodeId: number,
ident: DatasetIdent,
atoms: LanguageAtom[],
): Promise<{ path: string | null }> {
if (!ENV_URL) return { path: null };
const res = await fetch(
new URL(`/api/episodes/${episodeId}/atoms`, ENV_URL).toString(),
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
episode_index: episodeId,
repo_id: ident.repoId || null,
local_path: ident.localPath || null,
atoms,
}),
},
);
if (!res.ok) {
const text = await res.text().catch(() => `${res.status}`);
throw new Error(text || `save atoms: ${res.status}`);
}
const data = (await res.json().catch(() => ({}))) as { path?: string | null };
return { path: data.path ?? null };
}
export async function fetchFrameTimestamps(
episodeId: number,
ident: DatasetIdent,
): Promise<number[]> {
if (!ENV_URL) return [];
const res = await fetch(
buildUrl(`/api/episodes/${episodeId}/frame_timestamps`, ident),
);
if (!res.ok) return [];
const data = (await res.json()) as { timestamps?: number[] };
return data.timestamps || [];
}
export async function exportDataset(
ident: DatasetIdent,
outputDir?: string | null,
copyVideos = false,
): Promise<{
output_dir: string;
persistent_rows: number;
event_rows: number;
}> {
if (!ENV_URL) throw new Error("Annotate backend not configured");
const res = await fetch(new URL("/api/export", ENV_URL).toString(), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
repo_id: ident.repoId || null,
revision: ident.revision || null,
local_path: ident.localPath || null,
output_dir: outputDir || null,
copy_videos: !!copyVideos,
}),
});
if (!res.ok) {
const text = await res.text().catch(() => `${res.status}`);
throw new Error(text || `export: ${res.status}`);
}
return res.json();
}
export interface PushToHubResult {
ok: boolean;
repo_id: string;
url: string;
message: string;
}
export async function pushToHub(
ident: DatasetIdent,
hfToken: string,
pushInPlace: boolean,
newRepoId: string | null,
privateRepo: boolean,
commitMessage: string,
): Promise<PushToHubResult> {
if (!ENV_URL) throw new Error("Annotate backend not configured");
const res = await fetch(new URL("/api/push_to_hub", ENV_URL).toString(), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
repo_id: ident.repoId || null,
revision: ident.revision || null,
local_path: ident.localPath || null,
hf_token: hfToken,
push_in_place: pushInPlace,
new_repo_id: newRepoId || null,
private: privateRepo,
commit_message: commitMessage,
}),
});
if (!res.ok) {
const text = await res.text().catch(() => `${res.status}`);
throw new Error(text || `push: ${res.status}`);
}
return res.json();
}
|