Skip to content

Commit de4fbfa

Browse files
committedNov 26, 2024
feat: add flex pro
1 parent 48a0f75 commit de4fbfa

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
 

‎modules/bfl/flex.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as Flex from '.';
2+
const BFL_API = 'https://api.bfl.ml';
3+
const FLEX_PRO = `${BFL_API}/v1/flux-pro-1.1`;
4+
const FLEX_PRO_ULTRA = `${BFL_API}/v1/flux-pro-1.1-ultra`;
5+
const GET_RESULT = `${BFL_API}/v1/get_result?id=`;
6+
7+
export async function generateImage(prompt: string): Promise<Flex.Response> {
8+
const promptReq: Flex.ProPrompt = {
9+
prompt: prompt,
10+
width: 1024,
11+
height: 768,
12+
prompt_upsampling: false,
13+
seed: null,
14+
safety_tolerance: 6,
15+
output_format: 'jpeg',
16+
};
17+
const options = {
18+
method: 'POST',
19+
body: JSON.stringify(promptReq),
20+
headers: new Headers({
21+
'Content-Type': 'application/json',
22+
'X-Key': `${process.env.BFL_KEY}`,
23+
}),
24+
};
25+
const response: Flex.Response = await fetch(new URL(FLEX_PRO), options).then(
26+
data => data.json()
27+
);
28+
29+
console.log('response:\n', response);
30+
31+
let imgResponse = null;
32+
while (imgResponse === null) {
33+
await sleep(500);
34+
imgResponse = await getImageFromAPI(response.id);
35+
console.log('imgResponse:\n', imgResponse);
36+
}
37+
38+
return imgResponse;
39+
}
40+
41+
async function getImageFromAPI(
42+
id: string
43+
): Promise<Flex.GetResponse | Flex.Error | null> {
44+
const res: Flex.GetResponse = await fetch(`${GET_RESULT}${id}`).then(data =>
45+
data.json()
46+
);
47+
console.log('res:', res);
48+
if (res.status === 'Ready') return res;
49+
50+
return null;
51+
}
52+
53+
async function sleep(msec: number) {
54+
return new Promise(resolve => setTimeout(resolve, msec));
55+
}

‎modules/bfl/index.d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export interface PostResponse {
2+
id: string;
3+
}
4+
5+
export interface GetResponse {
6+
id: string;
7+
status: string;
8+
result: object | null;
9+
}
10+
11+
export interface Error {
12+
detail: [
13+
{
14+
loc: [number | string];
15+
msg: string;
16+
type: string;
17+
},
18+
];
19+
id: string;
20+
}
21+
22+
export type Response = PostResponse | GetResponse | Error;
23+
24+
export type ProPrompt = {
25+
prompt: string;
26+
width: number | null;
27+
height: number | null;
28+
prompt_upsampling: boolean | null;
29+
seed: number | null;
30+
safety_tolerance: 0 | 1 | 2 | 3 | 4 | 5 | 6;
31+
output_format: string;
32+
};

‎routes/flex.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import express, { Request, Response } from 'express';
2+
import { generateImage } from '../modules/bfl/flex';
3+
const router = express.Router();
4+
5+
router.get('/', (req: Request, res: Response) => {
6+
// Check if model is specified
7+
// if (!req.query.model) {
8+
// res.json({
9+
// error: {
10+
// message: 'Model not specified!',
11+
// type: 'invalid_model_error',
12+
// param: null,
13+
// code: null,
14+
// },
15+
// });
16+
// return;
17+
// }
18+
19+
// Check if there is a prompt
20+
if (!req.query.prompt) {
21+
res.json({
22+
error: {
23+
message: 'Prompt cannot be empty!',
24+
type: 'invalid_model_error',
25+
param: null,
26+
code: null,
27+
},
28+
});
29+
return;
30+
}
31+
32+
// Set max tokens
33+
// const maxTokens = parseInt(req.query.max_tokens?.toString() ?? '3000');
34+
// const model = req.query.model as string;
35+
const prompt = req.query.prompt as string;
36+
generateImage(prompt).then(data => res.json(data));
37+
});
38+
39+
export default router;

‎routes/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import express from 'express';
22
// import users from './users';
33
import gpt from './gpt';
44
import authorize from './authorize';
5+
import flex from './flex';
56

67
const router = express.Router();
78

89
// router.use('/users', users);
910
router.use('/gpt', gpt);
1011
router.use('/authorize', authorize);
12+
router.use('/flex', flex);
1113

1214
export default router;

0 commit comments

Comments
 (0)
Please sign in to comment.