Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules/
dist/

*.env
/tests/script.ts
/tests/script.ts
.npmrc
50 changes: 48 additions & 2 deletions Runware/Runware-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
IRequestAudio,
IAudio,
MediaUUID,
IRequestThreeD,
IThreeDImage,
} from "./types";
import {
BASE_RUNWARE_URLS,
Expand Down Expand Up @@ -101,7 +103,7 @@ export class RunwareBase {


private getUniqueUUID(item: MediaUUID): string | undefined {
return item.mediaUUID || item.audioUUID || item.imageUUID || item.videoUUID;
return item.mediaUUID || item.audioUUID || item.imageUUID || item.videoUUID || item.outputs?.files?.map((file) => file.uuid).join("-");
}

/**
Expand Down Expand Up @@ -1000,9 +1002,53 @@ export class RunwareBase {
}
};


threeDInference = async (
payload: IRequestThreeD
): Promise<IThreeDImage[] | IThreeDImage> => {
const { skipResponse, deliveryMethod = "sync", ...rest } = payload;

try {
const requestMethod =
deliveryMethod === "sync"
? this.baseSyncRequest
: this.baseSingleRequest;

const request = await requestMethod<IThreeDImage>({
payload: {
...rest,
numberResults: rest.numberResults || 1,
taskType: ETaskType.THREE_D_INFERENCE,
deliveryMethod: deliveryMethod,
},
groupKey: LISTEN_TO_MEDIA_KEY.REQUEST_IMAGES,
debugKey: "three-d-inference",
skipResponse,
});


if (skipResponse) {
return request;
}


const taskUUID = request?.taskUUID;
if (deliveryMethod === "async") {
return this.pollForAsyncResults<IThreeDImage>({
taskUUID,
numberResults: payload?.numberResults,
});
}

// If not async, just return the initial result
return request;
} catch (e) {
throw e;
}
};

getResponse = async <T>(payload: IAsyncResults): Promise<T[]> => {
const taskUUID = payload.taskUUID;
// const mock = getRandomTaskResponses({ count: 2, taskUUID });
return this.baseSingleRequest({
payload: {
...payload,
Expand Down
3 changes: 0 additions & 3 deletions Runware/Runware-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import WebSocket from "ws";

import { RunwareBase } from "./Runware-base";
import { ETaskType, RunwareBaseType, SdkType } from "./types";
import { delay } from "./utils";

// let allImages: IImage[] = [];

Expand Down Expand Up @@ -103,8 +102,6 @@ export class RunwareServer extends RunwareBase {
if (!data) return;
const m = JSON.parse(data);

// console.log("response", JSON.stringify(m, null, 4));

this._listeners.forEach((lis) => {
const result = lis.listener(m);
if (result) {
Expand Down
66 changes: 56 additions & 10 deletions Runware/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum ETaskType {
VIDEO_INFERENCE = "videoInference",
CAPTION = "caption",
AUDIO_INFERENCE = "audioInference",
THREE_D_INFERENCE = "3dInference",
GET_RESPONSE = "getResponse",
PHOTO_MAKER = "photoMaker",
IMAGE_CONTROL_NET_PRE_PROCESS = "imageControlNetPreProcess",
Expand All @@ -39,7 +40,8 @@ export type RunwareBaseType = {
export type IOutputType = "base64Data" | "dataURI" | "URL";
export type IOutputFormat = "JPG" | "PNG" | "WEBP";
export type IVideoOutputFormat = "MP4" | "WEBM" | "MOV";
export type IAudioOutputFormat = "MP3"
export type IAudioOutputFormat = "MP3";
export type IThreeDOutputFormat = "GLB" | "PLY";

export interface IAdditionalResponsePayload {
includePayload?: boolean;
Expand Down Expand Up @@ -77,6 +79,25 @@ export interface IVideoToImage {
videoURL?: string;
}

interface TOutputFile {
uuid: string;
url: string;
}

interface TOutputFiles {
files: TOutputFile[];
}

export interface IThreeDImage {
taskType: ETaskType;
taskUUID: string;
status: string;
NSFWContent?: boolean;
cost?: number;
seed: number;
outputs?: TOutputFiles;
}

export interface IControlNetImage {
taskUUID: string;
inputImageUUID: string;
Expand Down Expand Up @@ -279,12 +300,12 @@ export interface IImageToText {

export interface IRemoveImageBackground extends IRequestImageToText {
outputType?: IOutputType;
outputFormat?: IOutputFormat| "MP4" | "WEBM" | "MOV";
outputFormat?: IOutputFormat | "MP4" | "WEBM" | "MOV";
model: string;
inputs?: {
video?: InputsValue;
image?: InputsValue;
}
};
settings?: {
rgba?: number[];
postProcessMask?: boolean;
Expand All @@ -302,7 +323,6 @@ export interface IRemoveImageBackground extends IRequestImageToText {
deliveryMethod?: string;
}


type InputsValue = string | Record<string, unknown>;

export interface IRequestVideo extends IRequestImageToText {
Expand Down Expand Up @@ -379,7 +399,7 @@ export interface IRequestAudio {
[key: string]: unknown;
};
deliveryMethod?: string;

taskUUID?: string;
customTaskUUID?: string;

Expand All @@ -389,6 +409,31 @@ export interface IRequestAudio {
[key: string]: unknown;
}

export interface IRequestThreeD {
model: string;
numberResults?: number;
outputType?: IOutputType;
outputFormat?: IThreeDOutputFormat;
uploadEndpoint?: string;
includeCost?: boolean;
positivePrompt?: string;
onPartialResponse?: (results: IThreeDImage[], error?: IError) => void;

inputs?: {
image?: InputsValue;
mask?: InputsValue;
} & {
[key: string]: unknown;
};
deliveryMethod?: string;
taskUUID?: string;
customTaskUUID?: string;

skipResponse?: boolean;
retry?: number;
[key: string]: unknown;
}

export interface IAsyncResults {
taskUUID: string;
onPartialImages?: (images: IImage[], error?: IError) => void;
Expand Down Expand Up @@ -434,7 +479,7 @@ export interface IUpscaleGan extends IAdditionalResponsePayload {
image?: InputsValue;
} & {
[key: string]: unknown;
}
};
model?: string;

customTaskUUID?: string;
Expand All @@ -449,7 +494,7 @@ export type ReconnectingWebsocketProps = {
addEventListener: (
type: string,
listener: EventListener,
options: any
options: any,
) => void;
send: (data: any) => void;
} & WebSocket;
Expand Down Expand Up @@ -600,7 +645,7 @@ export type TAddModelBaseType = {
retry?: number;
onUploadStream?: (
response?: IAddModelResponse,
error?: IErrorResponse
error?: IErrorResponse,
) => void;
};

Expand Down Expand Up @@ -855,7 +900,7 @@ export type TImageMaskingResponse = {
y_min: number;
x_max: number;
y_max: number;
}
},
];
maskImageURL: string;
cost: number;
Expand All @@ -876,4 +921,5 @@ export type MediaUUID = {
audioUUID?: string;
imageUUID?: string;
videoUUID?: string;
}
outputs?: TOutputFiles;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@runware/sdk-js",
"version": "1.2.3",
"version": "1.2.4-beta.2",
"description": "The SDK is used to run image inference with the Runware API, powered by the RunWare inference platform. It can be used to generate imaged with text-to-image and image-to-image. It also allows the use of an existing gallery of models or selecting any model or LoRA from the CivitAI gallery. The API also supports upscaling, background removal, inpainting and outpainting, and a series of other ControlNet models.",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ export type TImageMaskingResponse = {

## Changelog

### - v1.2.4

- Add 3d inference

### - v1.2.3

- Handle async delivery method for image inference
Expand Down Expand Up @@ -774,7 +778,6 @@ Breaking change: `imageUUID` can now be undefined when using removeBackground/re
**Added or Changed**

- Added method aliases so task types match those of official API. It is recommended to use these new aliases going forward:

- imageInference > requestImages
- controlNetPreprocess > controlNetPreProcess
- caption > requestImageToText
Expand Down
Loading