Skip to content

Instantly share code, notes, and snippets.

@monmonja
Created November 14, 2024 03:03
Show Gist options
  • Save monmonja/68127c80875f0dc486bc9cec19ad59f7 to your computer and use it in GitHub Desktop.
Save monmonja/68127c80875f0dc486bc9cec19ad59f7 to your computer and use it in GitHub Desktop.
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 <Hugging face token>",
"Content-Type": "application/json"
};
// 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: 720,
height: 480,
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() {
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[5]?.trim(); // Sixth column
folderName = folderName.replaceAll(':', '-');
// Reset imageIndex for each new row
let imageIndex = 0;
if (folderName && inputText) {
// Loop to generate 70 images for each row
for (let i = 0; i < 70; i++) {
await fetchImageAndSave(folderName, inputText, imageIndex);
imageIndex += 1;
await delay(20 * 1000);
}
} else {
console.warn(`Skipping row ${lineIndex}: Missing folder name or input text`);
}
}
}
processCSV();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment