enzostvs's picture
enzostvs HF staff
try another way
f4aba6e
raw
history blame
1.11 kB
'use strict'
const nodeCache = require('node-cache');
const dotenv = require('dotenv');
const HfInference = require('@huggingface/inference').HfInference;
dotenv.config();
const inference = new HfInference(process.env.HF_TOKEN);
const cache = new nodeCache(
{
stdTTL: 60 * 60 * 24,
checkperiod: 60 * 60,
useClones: false
}
);
const REPO_NAME = "black-forest-labs/FLUX.1-schnell"
module.exports = async function (fastify, opts) {
fastify.get('/:inputs', async function (request, reply) {
const { inputs } = request.params;
const slug = inputs.replace(/[^a-zA-Z0-9]/g, '');
if (cache.get(slug)) {
const image = await cache.get(slug);
console.log("Cache hit")
return reply
.header('Content-Type', 'image/jpeg')
.send(image);
}
const hfRequest = await inference.textToImage({
inputs,
model: REPO_NAME,
})
const buffer = await hfRequest.arrayBuffer();
const array = new Uint8Array(buffer);
cache.set(slug, array);
return reply
.header('Content-Type', 'image/jpeg')
.send(array);
})
}