Skip to content

Commit

Permalink
feat: Flux
Browse files Browse the repository at this point in the history
  • Loading branch information
Kadxy committed Aug 7, 2024
1 parent 6d9beec commit 789204d
Show file tree
Hide file tree
Showing 4 changed files with 428 additions and 28 deletions.
70 changes: 68 additions & 2 deletions app/client/FluxProxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// app/client/FluxProxy.ts
// "https://blackforestlabs.ai"

import {noApiKeys} from "@/app/utils";
import {FluxEndpoint, REQUEST_TIMEOUT_MS} from "@/constant";
import {api2ProviderBaseUrl} from "@/app/store";
import {getRequestOptions} from "@/app/client/helper";

export interface FluxImageRequest {
prompt: string;
Expand All @@ -20,14 +26,74 @@ export interface FluxGetResponse {
result: string;
}

// export interface FluxPromptEnhanceRequest {
// messages: string;
// }
//
// export interface FluxPromptEnhanceResponse {
// data: string;
// }

export class FluxAPI {
private readonly apiKey: string;

private readonly website = "https://blackforestlabs.ai";

constructor(apiKey: string) {
this.apiKey = apiKey;
}

noKey(): boolean {
return noApiKeys(this.apiKey);
}

path(endpoint: FluxEndpoint | string): string {
return [api2ProviderBaseUrl.Flux, endpoint].join("/");
}

async onImage(request: FluxImageRequest, signal: AbortSignal, timeoutMs: number = REQUEST_TIMEOUT_MS) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
const abortSignal = signal || controller.signal;

signal && signal.addEventListener("abort", () => controller.abort());

try {
const res = await fetch(this.path(FluxEndpoint.IMAGE), {
...getRequestOptions(this.apiKey, request),
signal: abortSignal
})
clearTimeout(timeoutId);
return res;
} catch (e) {
throw e;
}
}

async getImage(request: FluxGetRequest, signal: AbortSignal, timeoutMs: number = REQUEST_TIMEOUT_MS) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
const abortSignal = signal || controller.signal;

signal && signal.addEventListener("abort", () => controller.abort());

try {
const res = await fetch(this.path(FluxEndpoint.GET.replace("{{request_id}}", request.request_id)), {
...getRequestOptions(this.apiKey, "GET"),
signal: abortSignal
})
clearTimeout(timeoutId);
return res;
} catch (e) {
throw e;
}
}

// async promptEnhance(request: FluxPromptEnhanceRequest): Promise<Response> {
// try {
// return await fetch(this.path(FluxEndpoint.PROMPT_ENHANCE), {
// ...getRequestOptions(this.apiKey, request)
// })
// } catch (e) {
// throw e;
// }
// }
}
Loading

0 comments on commit 789204d

Please sign in to comment.