Spaces:
Running
Running
Download src/lib/api.ts from suplo6669/sharpify-ai: direct link, hf CLI and curl.
- Browser
- Download file 13.7 kB
-
https://huggingface.co/spaces/suplo6669/sharpify-ai/resolve/main/src/lib/api.ts
- Command line
-
hf download hf://spaces/suplo6669/sharpify-ai/src/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/suplo6669/sharpify-ai/resolve/main/src/lib/api.ts
13.7 kB
| import { Client, handle_file } from "@gradio/client"; | |
| import type { | |
| EnhancementResponse, | |
| EnhancementSettings, | |
| ImageMetadata, | |
| } from "@/types/enhancement"; | |
| import { HF_INFERENCE_SPACE, REQUIRES_HF_SIGN_IN } from "@/lib/deployment"; | |
| const API_BASE_URL = (process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000").replace(/\/+$/, ""); | |
| const ERROR_MESSAGES: Record<string, string> = { | |
| upload_too_large: "This image is larger than the 20 MB upload limit.", | |
| auth_required: "Sign in with Hugging Face before enhancing images.", | |
| gpu_quota_exceeded: "Your Hugging Face GPU time is used up for now. Try again after your quota resets.", | |
| queue_unavailable: "The shared AI queue is full right now. Please try again shortly.", | |
| resource_unavailable: "The AI service could not prepare this image. Try a smaller input.", | |
| unauthorized: "Your Hugging Face sign-in expired. Sign in again, then retry this image.", | |
| unsupported_media_type: "Choose a JPG, PNG, or WEBP image.", | |
| invalid_image: "This image is unsupported or exceeds the AI service's image limits. Try a supported still image with fewer pixels.", | |
| invalid_request: "Choose an upscale factor and output format, then try again.", | |
| model_not_ready: "The AI service is starting up. Please try again in a moment.", | |
| gpu_out_of_memory: "This image is too large for the available GPU memory. Try a smaller image.", | |
| enhancement_failed: "The AI service could not finish this image. Try a smaller image.", | |
| inference_failed: "The AI service could not enhance this image. Please try again.", | |
| encoding_failed: "The enhanced image could not be prepared for download.", | |
| internal_error: "Something went wrong while preparing the enhanced image.", | |
| }; | |
| export type EnhancementProgress = { | |
| stage: "connecting" | "queued" | "working"; | |
| position?: number; | |
| etaSeconds?: number; | |
| }; | |
| export type EnhancementProgressCallback = (progress: EnhancementProgress) => void; | |
| function uploadBlob(file: File): Blob { | |
| if (file.type.trim()) return file; | |
| const extension = file.name.split(".").pop()?.toLowerCase(); | |
| const inferredType = extension === "jpg" || extension === "jpeg" | |
| ? "image/jpeg" | |
| : extension === "png" | |
| ? "image/png" | |
| : extension === "webp" | |
| ? "image/webp" | |
| : "application/octet-stream"; | |
| return file.slice(0, file.size, inferredType); | |
| } | |
| export class EnhancementApiError extends Error { | |
| constructor( | |
| message: string, | |
| readonly status: number, | |
| readonly code?: string, | |
| ) { | |
| super(message); | |
| this.name = "EnhancementApiError"; | |
| } | |
| } | |
| function readHeaderInteger(headers: Headers, name: string, fallback: number): number { | |
| const value = Number(headers.get(name)); | |
| return Number.isSafeInteger(value) && value > 0 ? value : fallback; | |
| } | |
| function readFilename(headers: Headers, fallback: string): string { | |
| const value = headers.get("content-disposition")?.match(/filename="([^"]+)"/i)?.[1]; | |
| if (!value) return fallback; | |
| const safeName = value.replace(/[^A-Za-z0-9._-]/g, "").slice(0, 120); | |
| return safeName || fallback; | |
| } | |
| async function responseError(response: Response): Promise<EnhancementApiError> { | |
| let code = ""; | |
| try { | |
| const payload: unknown = await response.json(); | |
| if (typeof payload === "object" && payload !== null && "error" in payload) { | |
| const error = payload.error; | |
| if (typeof error === "object" && error !== null && "code" in error) { | |
| code = typeof error.code === "string" ? error.code : ""; | |
| } | |
| } | |
| } catch { | |
| // Use the safe status-based message when the backend did not return JSON. | |
| } | |
| const message = ERROR_MESSAGES[code] ?? (response.status === 413 | |
| ? ERROR_MESSAGES.upload_too_large | |
| : response.status >= 500 | |
| ? "The AI service is unavailable right now. Please try again shortly." | |
| : "The image could not be processed. Check the file and try again."); | |
| return new EnhancementApiError(message, response.status, code || undefined); | |
| } | |
| function hostedSpaceOrigin(): string { | |
| if (/^https?:\/\//i.test(HF_INFERENCE_SPACE)) { | |
| return new URL(HF_INFERENCE_SPACE).origin; | |
| } | |
| const [owner, space] = HF_INFERENCE_SPACE.split("/"); | |
| if (!owner || !space) { | |
| throw new EnhancementApiError( | |
| "The hosted AI service is not configured correctly.", | |
| 0, | |
| "invalid_configuration", | |
| ); | |
| } | |
| return `https://${owner}-${space}.hf.space`; | |
| } | |
| function asRecord(value: unknown): Record<string, unknown> | null { | |
| return typeof value === "object" && value !== null | |
| ? value as Record<string, unknown> | |
| : null; | |
| } | |
| async function hostedOutputBlob(value: unknown, contentType: string): Promise<Blob> { | |
| if (value instanceof Blob) return new Blob([value], { type: contentType }); | |
| const fileData = asRecord(value); | |
| if (fileData?.blob instanceof Blob) { | |
| return new Blob([fileData.blob], { type: contentType }); | |
| } | |
| const candidate = typeof value === "string" | |
| ? value | |
| : [fileData?.data, fileData?.url, fileData?.path] | |
| .find((item): item is string => typeof item === "string" && item.length > 0); | |
| if (typeof candidate !== "string") { | |
| throw new EnhancementApiError( | |
| "The service did not return a downloadable image.", | |
| 502, | |
| "invalid_response", | |
| ); | |
| } | |
| let url: URL; | |
| try { | |
| url = candidate.startsWith("data:") || candidate.startsWith("blob:") | |
| ? new URL(candidate) | |
| : new URL(candidate, `${hostedSpaceOrigin()}/`); | |
| } catch { | |
| throw new EnhancementApiError( | |
| "The service returned an invalid image reference.", | |
| 502, | |
| "invalid_response", | |
| ); | |
| } | |
| if (url.protocol === "data:" || url.protocol === "blob:") { | |
| const inlineResponse = await fetch(url); | |
| if (!inlineResponse.ok) { | |
| throw new EnhancementApiError( | |
| "The generated image could not be read.", | |
| inlineResponse.status, | |
| "invalid_response", | |
| ); | |
| } | |
| return new Blob([await inlineResponse.blob()], { type: contentType }); | |
| } | |
| if (url.origin !== hostedSpaceOrigin() || url.protocol !== "https:") { | |
| throw new EnhancementApiError( | |
| "The service returned an unexpected image location.", | |
| 502, | |
| "invalid_response", | |
| ); | |
| } | |
| const response = await fetch(url, { credentials: "omit" }); | |
| if (!response.ok) { | |
| throw new EnhancementApiError( | |
| response.status === 401 || response.status === 403 | |
| ? ERROR_MESSAGES.unauthorized | |
| : "The generated image could not be downloaded.", | |
| response.status, | |
| response.status === 401 || response.status === 403 | |
| ? "unauthorized" | |
| : "invalid_response", | |
| ); | |
| } | |
| return new Blob([await response.blob()], { type: contentType }); | |
| } | |
| function metadataInteger( | |
| metadata: Record<string, unknown>, | |
| name: string, | |
| fallback: number, | |
| ): number { | |
| const value = metadata[name]; | |
| return typeof value === "number" && Number.isSafeInteger(value) && value > 0 | |
| ? value | |
| : fallback; | |
| } | |
| function hostedFailure(error: unknown): EnhancementApiError { | |
| if (error instanceof EnhancementApiError) return error; | |
| const message = error instanceof Error ? error.message : ""; | |
| if (/oauth|sign.?in|unauthori[sz]ed|401|expired token/i.test(message)) { | |
| return new EnhancementApiError(ERROR_MESSAGES.unauthorized, 401, "auth_required"); | |
| } | |
| if (/quota|over.?quota|daily limit/i.test(message)) { | |
| return new EnhancementApiError(ERROR_MESSAGES.gpu_quota_exceeded, 429, "gpu_quota_exceeded"); | |
| } | |
| if (/queue.*(full|limit)|queue is full/i.test(message)) { | |
| return new EnhancementApiError(ERROR_MESSAGES.queue_unavailable, 503, "queue_full"); | |
| } | |
| if (/out of memory/i.test(message)) { | |
| return new EnhancementApiError(ERROR_MESSAGES.gpu_out_of_memory, 503, "gpu_out_of_memory"); | |
| } | |
| return new EnhancementApiError( | |
| "The AI service is unavailable right now. Please try again shortly.", | |
| 503, | |
| "inference_failed", | |
| ); | |
| } | |
| async function enhanceWithHostedSpace( | |
| file: File, | |
| settings: EnhancementSettings, | |
| original: { width: number; height: number }, | |
| accessToken: string | null | undefined, | |
| onProgress?: EnhancementProgressCallback, | |
| ): Promise<EnhancementResponse> { | |
| if (!accessToken || !accessToken.startsWith("hf_")) { | |
| throw new EnhancementApiError(ERROR_MESSAGES.auth_required, 401, "auth_required"); | |
| } | |
| onProgress?.({ stage: "connecting" }); | |
| try { | |
| const client = await Client.connect(HF_INFERENCE_SPACE, { | |
| token: accessToken as `hf_${string}`, | |
| oauth_token: accessToken, | |
| events: ["data", "status"], | |
| credentials: "omit", | |
| record_history: false, | |
| }); | |
| const submission = client.submit("/enhance", [ | |
| handle_file(uploadBlob(file)), | |
| file.name || "pasted-image.png", | |
| String(settings.scale), | |
| settings.outputFormat, | |
| ]); | |
| let resultData: unknown[] | null = null; | |
| let queueError = ""; | |
| for await (const event of submission) { | |
| if (event.type === "status") { | |
| if (event.stage === "error") { | |
| queueError = typeof event.message === "string" ? event.message : ""; | |
| } else if (event.stage === "pending") { | |
| onProgress?.({ | |
| stage: "queued", | |
| position: event.position, | |
| etaSeconds: event.eta, | |
| }); | |
| } else if (event.stage === "generating") { | |
| onProgress?.({ stage: "working" }); | |
| } | |
| } else if (event.type === "data") { | |
| resultData = Array.isArray(event.data) ? event.data : null; | |
| } | |
| } | |
| if (queueError) throw hostedFailure(new Error(queueError)); | |
| if (!resultData || resultData.length < 2) { | |
| throw new EnhancementApiError( | |
| "The service returned an incomplete enhancement result.", | |
| 502, | |
| "invalid_response", | |
| ); | |
| } | |
| const metadata = asRecord(resultData[1]); | |
| const errorPayload = asRecord(metadata?.error); | |
| if (errorPayload) { | |
| const code = typeof errorPayload.code === "string" ? errorPayload.code : "inference_failed"; | |
| throw new EnhancementApiError( | |
| ERROR_MESSAGES[code] ?? "The image could not be processed. Check the file and try again.", | |
| code === "auth_required" ? 401 : 422, | |
| code, | |
| ); | |
| } | |
| if (!metadata) { | |
| throw new EnhancementApiError( | |
| "The service returned invalid image metadata.", | |
| 502, | |
| "invalid_response", | |
| ); | |
| } | |
| const allowedContentTypes = new Set(["image/png", "image/jpeg", "image/webp"]); | |
| const declaredContentType = metadata.content_type; | |
| const contentType = typeof declaredContentType === "string" | |
| && allowedContentTypes.has(declaredContentType) | |
| ? declaredContentType | |
| : `image/${settings.outputFormat === "jpeg" ? "jpeg" : settings.outputFormat}`; | |
| const blob = await hostedOutputBlob(resultData[0], contentType); | |
| const filename = typeof metadata.filename === "string" | |
| && /^[A-Za-z0-9._-]{1,120}$/.test(metadata.filename) | |
| ? metadata.filename | |
| : `${file.name.replace(/\.[^.]+$/, "") || "image"}-enhanced-${settings.scale}x.${settings.outputFormat}`; | |
| const result: ImageMetadata = { | |
| originalWidth: metadataInteger(metadata, "original_width", original.width), | |
| originalHeight: metadataInteger(metadata, "original_height", original.height), | |
| enhancedWidth: metadataInteger(metadata, "enhanced_width", original.width * settings.scale), | |
| enhancedHeight: metadataInteger(metadata, "enhanced_height", original.height * settings.scale), | |
| scale: settings.scale, | |
| model: typeof metadata.model === "string" ? metadata.model : "Real-ESRGAN", | |
| filename, | |
| }; | |
| return { blob, metadata: result }; | |
| } catch (error) { | |
| throw hostedFailure(error); | |
| } | |
| } | |
| export async function enhanceImage( | |
| file: File, | |
| settings: EnhancementSettings, | |
| original: { width: number; height: number }, | |
| accessToken?: string | null, | |
| onProgress?: EnhancementProgressCallback, | |
| ): Promise<EnhancementResponse> { | |
| if (REQUIRES_HF_SIGN_IN) { | |
| return enhanceWithHostedSpace(file, settings, original, accessToken, onProgress); | |
| } | |
| const formData = new FormData(); | |
| formData.append("file", uploadBlob(file), file.name || "pasted-image.png"); | |
| formData.append("scale", String(settings.scale)); | |
| formData.append("output_format", settings.outputFormat); | |
| let response: Response; | |
| try { | |
| response = await fetch(`${API_BASE_URL}/api/enhance`, { | |
| method: "POST", | |
| body: formData, | |
| }); | |
| } catch { | |
| throw new EnhancementApiError( | |
| "Could not reach the AI service. Check that the backend is running and try again.", | |
| 0, | |
| "network_error", | |
| ); | |
| } | |
| if (!response.ok) throw await responseError(response); | |
| const contentType = response.headers.get("content-type")?.split(";", 1)[0].trim(); | |
| if (!contentType?.startsWith("image/")) { | |
| throw new EnhancementApiError( | |
| "The service returned an unexpected result. Please try again.", | |
| response.status, | |
| "invalid_response", | |
| ); | |
| } | |
| const blob = await response.blob(); | |
| const metadata: ImageMetadata = { | |
| originalWidth: readHeaderInteger(response.headers, "x-original-width", original.width), | |
| originalHeight: readHeaderInteger(response.headers, "x-original-height", original.height), | |
| enhancedWidth: readHeaderInteger( | |
| response.headers, | |
| "x-enhanced-width", | |
| original.width * settings.scale, | |
| ), | |
| enhancedHeight: readHeaderInteger( | |
| response.headers, | |
| "x-enhanced-height", | |
| original.height * settings.scale, | |
| ), | |
| scale: settings.scale, | |
| model: response.headers.get("x-model") || "Real-ESRGAN", | |
| filename: readFilename( | |
| response.headers, | |
| `${file.name.replace(/\.[^.]+$/, "") || "image"}-enhanced-${settings.scale}x.${settings.outputFormat}`, | |
| ), | |
| }; | |
| return { blob, metadata }; | |
| } | |