Skip to content

Commit

Permalink
*: improve docs + specify Prodia type
Browse files Browse the repository at this point in the history
  • Loading branch information
montyanderson committed Oct 12, 2023
1 parent ce2f209 commit a63a05a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 59 deletions.
59 changes: 38 additions & 21 deletions prodia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ export type ProdiaControlnetRequest = ImageInput & {

type MaskInput = { maskUrl: string } | { maskData: string };

export type ProdiaInpaintingRequest =
& ImageInput
& MaskInput
& {
export type ProdiaInpaintingRequest = ImageInput &
MaskInput & {
prompt: string;
model?: string;
denoising_strength?: number;
Expand Down Expand Up @@ -104,28 +102,47 @@ export type ProdiaXlGenerateRequest = {

/* Constructor Definions */

export type Prodia = ReturnType<typeof createProdia>;
export type Prodia = {
// sd generations
generate: (params: ProdiaGenerateRequest) => Promise<ProdiaJobQueued>;
transform: (params: ProdiaTransformRequest) => Promise<ProdiaJobQueued>;
controlnet: (params: ProdiaControlnetRequest) => Promise<ProdiaJobQueued>;
inpainting: (params: ProdiaInpaintingRequest) => Promise<ProdiaJobQueued>;

// sdxl generations
xlGenerate: (params: ProdiaXlGenerateRequest) => Promise<ProdiaJobQueued>;

// job info
getJob: (jobId: string) => Promise<ProdiaJob>;
wait: (params: ProdiaJob) => Promise<ProdiaJobSucceeded | ProdiaJobFailed>;

// models
listModels: () => Promise<string[]>;
};

export type CreateProdiaOptions = {
apiKey: string;
base?: string;
};

export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {
export const createProdia = ({
apiKey,
base: _base
}: CreateProdiaOptions): Prodia => {
const base = _base || "https://api.prodia.com/v1";

const headers = {
"X-Prodia-Key": apiKey,
"X-Prodia-Key": apiKey
};

const generate = async (params: ProdiaGenerateRequest) => {
const response = await fetch(`${base}/sd/generate`, {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(params),
body: JSON.stringify(params)
});

if (response.status !== 200) {
Expand All @@ -140,9 +157,9 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(params),
body: JSON.stringify(params)
});

if (response.status !== 200) {
Expand All @@ -157,9 +174,9 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(params),
body: JSON.stringify(params)
});

if (response.status !== 200) {
Expand All @@ -174,9 +191,9 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(params),
body: JSON.stringify(params)
});

if (response.status !== 200) {
Expand All @@ -191,9 +208,9 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {
method: "POST",
headers: {
...headers,
"Content-Type": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(params),
body: JSON.stringify(params)
});

if (response.status !== 200) {
Expand All @@ -205,7 +222,7 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {

const getJob = async (jobId: string) => {
const response = await fetch(`${base}/job/${jobId}`, {
headers,
headers
});

if (response.status !== 200) {
Expand All @@ -215,7 +232,7 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {
return (await response.json()) as ProdiaJob;
};

const wait = async (job: ProdiaJobQueued | ProdiaJobGenerating) => {
const wait = async (job: ProdiaJob) => {
let jobResult: ProdiaJob = job;

while (
Expand All @@ -232,7 +249,7 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {

const listModels = async () => {
const response = await fetch(`${base}/models/list`, {
headers,
headers
});

if (response.status !== 200) {
Expand All @@ -250,6 +267,6 @@ export const createProdia = ({ apiKey, base: _base }: CreateProdiaOptions) => {
xlGenerate,
wait,
getJob,
listModels,
listModels
};
};
42 changes: 4 additions & 38 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

Official TypeScript library for Prodia's AI inference API.

- [Get an API Key](https://app.prodia.com/api)
- [Get an API Key](https://app.prodia.com/api)

- [View Docs + Pricing](https://docs.prodia.com/reference/getting-started)
- [View Docs + Pricing](https://docs.prodia.com/reference/getting-started)

## Usage

Expand All @@ -19,50 +19,16 @@ npm install prodia --save
import { createProdia } from "prodia";

const prodia = createProdia({
apiKey: "...",
apiKey: "..."
});

(async () => {
const job = await prodia.generate({
prompt: "puppies in a cloud, 4k",
prompt: "puppies in a cloud, 4k"
});

const { imageUrl, status } = await prodia.wait(job);

// check status and view your image :)
})();
```

## API

### `createProdia(opts)` => `Prodia`

Create a new Prodia object.

#### `opts`

- `apiKey` (`string`)

Your API key from the Prodia dashboard.

- `base` (`string`)

Optional: an enterprise-specific Prodia island.

#### `Prodia`

##### `generate(` `params` `)` => `ProdiaJob`

Create a new text to image job.

Supports all parameters [listed in the documentation](https://docs.prodia.com/reference/generate).

##### `transform(` `params` `)` => `ProdiaJob`

Create a new image to image job.

Supports all parameters [listed in the documentation](https://docs.prodia.com/reference/transform).

##### `wait(` `ProdiaJob` `)` => `{` `status`, `imageUrl` `}`

Returns once as job has completed.

0 comments on commit a63a05a

Please sign in to comment.