Skip to content

Instantly share code, notes, and snippets.

@monmonja
Created January 30, 2025 10:55
Show Gist options
  • Save monmonja/501e33254260cd51ca2e51f981c1631a to your computer and use it in GitHub Desktop.
Save monmonja/501e33254260cd51ca2e51f981c1631a to your computer and use it in GitHub Desktop.
generate image from HG with csv
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell";
const headers = {
"Authorization": "Bearer [API_TOKEN]",
"Content-Type": "application/json"
};
const imagePerRow = 2;
// Delay function to wait for a specified number of milliseconds
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Function to parse CSV rows manually
function parseCSVRow(row) {
const columns = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < row.length; i++) {
const char = row[i];
const nextChar = row[i + 1];
if (char === '"' && inQuotes && nextChar === '"') {
// Handle escaped quotes within a quoted field
current += '"';
i++; // Skip the next character
} else if (char === '"') {
// Toggle inQuotes state
inQuotes = !inQuotes;
} else if (char === ',' && !inQuotes) {
// End of field
columns.push(current.trim());
current = '';
} else {
// Regular character within a field
current += char;
}
}
// Add the last field
columns.push(current.trim());
return columns;
}
// Function to generate a single image based on inputs and save it to the specified folder
async function fetchImageAndSave(folderName, inputText, imageIndex) {
const payload = {
inputs: inputText,
parameters: {
width: 1072,
height: 720,
seed: Math.ceil(Math.random() * 10000000),
}
};
try {
console.log( {
method: 'POST',
headers,
body: JSON.stringify(payload),
});
const response = await fetch(API_URL, {
method: 'POST',
headers,
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`Error fetching image: ${response.statusText}`);
}
const imageBuffer = Buffer.from(await response.arrayBuffer());
// Ensure the folder exists
const folderPath = path.join(__dirname, folderName);
fs.mkdirSync(folderPath, { recursive: true });
// Save the image with a unique filename in the specified folder
const outputPath = path.join(folderPath, `output_image_${imageIndex}.png`);
fs.writeFileSync(outputPath, imageBuffer);
console.log(`Image ${imageIndex} saved to ${outputPath}`);
} catch (error) {
console.error(`Error generating image ${imageIndex}:`, error);
}
}
// Function to process CSV file manually
async function processCSV(imageDelta = 0) {
const fileStream = fs.createReadStream('story.csv');
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
let lineIndex = 0;
for await (const line of rl) {
lineIndex += 1;
// Skip the first two rows
if (lineIndex <= 2) continue;
// Parse the CSV row
const columns = parseCSVRow(line);
// Get folder name from the 1st column and input text from the 6th column
let folderName = columns[0]?.trim(); // First column
const inputText = columns[3]?.trim(); // fouroption_name = 'home' OR option_name = 'siteurl';th column
folderName = folderName.replaceAll(':', '-');
// Reset imageIndex for each new row
let imageIndex = 0 + imageDelta;
if (folderName && inputText) {
// Loop to generate 50 images for each row
for (let i = 0; i < imagePerRow; i++) {
const folderPath = path.join(__dirname, folderName);
// Save the image with a unique filename in the specified folder
const outputPath = path.join(folderPath, `output_image_${imageIndex}.png`);
if (!fs.existsSync(outputPath)){
await fetchImageAndSave(folderName, inputText, imageIndex);
imageIndex += 1;
// Calculate remaining time to wait to ensure a 10-second interval from start time
await delay(20 * 1000);
} else {
console.log(`Skipping image ${imageIndex}`);
imageIndex += 1;
}
}
} else {
console.warn(`Skipping row ${lineIndex}: Missing folder name or input text`);
}
}
}
async function init() {
for (let delta = 0; delta < 50; delta += imagePerRow) {
await processCSV(delta);
}
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment