import { ImageGenerateParams } from "openai/resources"; import { VisionInterface } from "../../interfaces/Vision"; import { FluxModels } from "../../types/vision/flux"; import { Logger } from "../../utils/Logger"; import { VisionResultStream } from "../../utils/stream"; import * as fal from '@fal-ai/serverless-client' import { ImageParams, ImageResult, VideoParams, VideoResult } from "../../types/vision/vision"; export class VersionFluxService implements VisionInterface { apiKey: string constructor(apiKey: string) { Logger.debug("apiKey:", apiKey) this.apiKey = apiKey fal.config({ credentials: this.apiKey }) } async image(params: ImageGenerateParams & ImageParams): Promise> { const stream = new VisionResultStream() const model = params?.model || FluxModels.SCHNELL const seed = params?.seed || Math.floor(Math.random() * 1000000000) const size = "landscape_4_3" if (params?.size) { } const num = params.n || 1 const input: any = { prompt: params.prompt, seed: seed, image_size: size, num_images: num, } if (params?.image_url) { input.image_url = params.image_url } fal.subscribe(model, { input: input, logs: true, onQueueUpdate: (update) => { if (update.status === "IN_PROGRESS") { // Logger.debug("Queue update:", update) stream.push({ status: "processing", created_at: Date.now() } as ImageResult) } } }).then(async (result: any) => { const images = result.images || [] if (images.length > 0) { stream.push({ status: "completed", created_at: Date.now(), data: images.map((image: any) => { return { format: "url", url: image.url } }) } as ImageResult) } else { stream.push({ status: "failed", created_at: Date.now() } as ImageResult) stream.end() } }).catch((error: any) => { // Logger.error("Error:", error) stream.push({ status: "failed", created_at: Date.now(), message: error.message || error } as ImageResult) stream.end() }) return stream } video(params: VideoParams): Promise> { throw new Error("Method not implemented."); } }