Download ui/src/api.ts from Kazzze/sd-text-fix: direct link, hf CLI and curl.
- Browser
- Download file 4.24 kB
-
https://huggingface.co/Kazzze/sd-text-fix/resolve/main/ui/src/api.ts
- Command line
-
hf download hf://Kazzze/sd-text-fix/ui/src/api.ts
-
curl -L -o api.ts https://huggingface.co/Kazzze/sd-text-fix/resolve/main/ui/src/api.ts
4.24 kB
| import type { FontMatch, Look, Model, Models, Options, Recipe, SessionInfo, Settings, Shape } from './types' | |
| const BASE = '/text_fix' | |
| async function unwrap<T>(response: Response): Promise<T> { | |
| let body: unknown = null | |
| try { | |
| body = await response.json() | |
| } catch { | |
| // not JSON: the status text has to do | |
| } | |
| if (!response.ok) { | |
| const message = body && typeof body === 'object' && 'error' in body ? String((body as { error: unknown }).error) : '' | |
| throw new Error(message || `${response.status} ${response.statusText}`) | |
| } | |
| return body as T | |
| } | |
| function post<T>(path: string, body: unknown): Promise<T> { | |
| return fetch(BASE + path, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(body), | |
| }).then((response) => unwrap<T>(response)) | |
| } | |
| /** How far a whole folder has got. */ | |
| export interface BatchState { | |
| running: boolean | |
| done: number | |
| total: number | |
| changed: number | |
| failed: number | |
| output: string | |
| current: string | |
| warnings: string[] | |
| } | |
| export interface ItemBody { | |
| recipe: Recipe | |
| text: string | |
| erase?: boolean | |
| } | |
| export const api = { | |
| options: () => fetch(`${BASE}/options`).then((response) => unwrap<Options>(response)), | |
| upload: (file: Blob, name: string) => | |
| fetch(`${BASE}/session?name=${encodeURIComponent(name)}`, { method: 'POST', body: file }).then((response) => | |
| unwrap<SessionInfo>(response), | |
| ), | |
| /** ComfyUI: the image the node last received. */ | |
| node: (node: string) => post<SessionInfo>('/session/node', { node }), | |
| pick: (session: string, x: number, y: number, reach: number, extend?: Recipe) => | |
| post<{ recipe: Recipe | null; selection: Shape | null; reading?: string }>('/pick', { session, x, y, reach, extend }), | |
| /** The detector's models: what is here, or fetch the ones a detector needs. */ | |
| models: (action: 'status' | 'download', choice?: 'light' | 'full') => post<Models>('/models', { action, choice }), | |
| resolve: (session: string, items: ItemBody[]) => post<{ selections: (Shape | null)[] }>('/resolve', { session, items }), | |
| find: (session: string) => post<{ found: Shape[] }>('/find', { session }), | |
| /** The fonts most like the old letters of a line, closest first. */ | |
| match: (session: string, recipe: Recipe) => post<{ fonts: FontMatch[] }>('/match', { session, recipe }), | |
| preview: (session: string, items: ItemBody[], look: Look) => | |
| post<{ version: number; empty: boolean; count: number }>('/preview', { session, items, look }), | |
| blend: (session: string, items: ItemBody[], look: Look, model: Model & { prompt: string; negative: string }) => | |
| post<{ | |
| version: number | |
| blended: boolean | |
| count?: number | |
| warning?: string | |
| variants?: number | |
| seeds?: number[] | |
| removed?: string[] | |
| }>('/blend', { | |
| session, | |
| items, | |
| look, | |
| model, | |
| }), | |
| /** One of the variants of the last model pass becomes the result. */ | |
| choose: (session: string, index: number) => post<{ version: number; index: number }>('/choose', { session, index }), | |
| apply: (session: string) => post<SessionInfo>('/apply', { session }), | |
| undo: (session: string) => post<SessionInfo>('/undo', { session }), | |
| save: (session: string, tab: string) => post<{ path: string }>('/save', { session, tab }), | |
| /** Save the look under a name, or delete a saved one; answers with all of them. */ | |
| presets: (action: 'save' | 'delete', name: string, look?: Look) => | |
| post<{ presets: Record<string, Look> }>('/presets', { action, name, look }), | |
| /** A whole folder: start it, stop it, or ask how far it got. */ | |
| batch: ( | |
| body: | |
| | { action: 'status' | 'stop' } | |
| | { action: 'start'; input: string; lines: string; blend: boolean; look: Look; model: Model }, | |
| ) => post<BatchState>('/batch', body), | |
| saveSettings: (settings: Settings) => post<{ settings: Settings }>('/settings', settings), | |
| imageUrl: (session: string, which: 'image' | 'result' | `variant-${number}`, version: number) => | |
| `${BASE}/image/${session}/${which}?v=${version}`, | |
| fontUrl: (name: string) => `${BASE}/font?name=${encodeURIComponent(name)}`, | |
| } | |