Skip to content

Commit

Permalink
Merge pull request #43 from BuildFire/handle-stock-images
Browse files Browse the repository at this point in the history
fix(generate ai) : replace unsplash api with buildfire stock images
  • Loading branch information
mas-iota authored Jul 11, 2024
2 parents 05c857e + 4d4dcd9 commit af0c87f
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/control/content/containers/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,33 @@ const Home = ({

/** UTILS */
const elimanateNotFoundImages = (url) => {
const optimisedURL = url.replace('1080x720', '100x100');
return new Promise((resolve) => {
if (url.includes("http")){
if (url.includes("http")) {
const xhr = new XMLHttpRequest();
xhr.open("GET", optimisedURL);
xhr.open("GET", url);
xhr.onerror = (error) => {
console.warn('provided URL is not a valid image', error);
console.warn('Provided URL is not a valid image', error);
resolve({isValid: false, newURL: null});
}
};
xhr.onload = () => {
if (xhr.responseURL.includes('source-404') || xhr.status == 404) {
return resolve({isValid: false ,newURL: null});
resolve({isValid: false, newURL: null});
} else {
return resolve({isValid: true, newURL: xhr.responseURL.replace('h=100', 'h=720').replace('w=100', 'w=1080') });
resolve({isValid: true, newURL: xhr.responseURL});
}
};
xhr.send();
} else resolve(false);
});
} else {
resolve({isValid: false, newURL: null});
}
});
};
/** UTILS END */

const seeder = new buildfire.components.aiStateSeeder({
generateOptions: {
userMessage: 'Provide me with images that relate to [Traveling]',
systemMessage: 'Use source.unsplash.com for image URLs, image should be 1080x720, use different image for each entry, URL should not have premium_photo or source.unsplash.com/random. Do not remove white spaces in the subject string.',
systemMessage: 'Use https://app.buildfire.com/api/stockImages/?topic={topic}&imageType=medium for image URLs, and use "random" as a default, A maximum of 2 comma-separated topic can be used for each URL, make sure to generate 10 images',
hintText: 'Replace values between brackets to match your requirements.',
maxRecords: 10,
jsonTemplate: {
Expand Down Expand Up @@ -85,17 +86,15 @@ const Home = ({
});
}
const promises = imageList.map(
item =>
item =>
{
return new Promise((resolve, reject) => {
elimanateNotFoundImages(item.url).then(result => {
return elimanateNotFoundImages(item.url).then(result => {
if (result.isValid) {
resolve(result.newURL + `?v=${generateRandomNumber()}` );
return result.newURL;
} else {
reject('image URL is not valid');
throw new Error('image URL not valid');
}
})
})
}
);
if (!promises.length){
Expand Down Expand Up @@ -132,7 +131,7 @@ const Home = ({
images: processedImages,
folders: processedFolders,
};

saveAIData(data, true);
seeder.requestResult.complete();
});
Expand All @@ -142,9 +141,9 @@ const Home = ({
jsonTemplate: {
folders: [{ name: '', images: [{ url: '' }] }]
},
sampleCSV: 'Weather, https://source.unsplash.com/featured/?sunny\n'
+ 'Weather, https://source.unsplash.com/featured/?rainy\n'
+ 'City, https://source.unsplash.com/featured/?street',
sampleCSV: 'Weather, https://app.buildfire.com/api/stockImages/?topic=sunny&imageType=medium\n'
+ 'Weather, https://app.buildfire.com/api/stockImages/?topic=rainy&imageType=medium\n'
+ 'City, https://app.buildfire.com/api/stockImages/?topic=city&imageType=medium',
systemMessage: 'Each row contains a folder name and image URL that should belong to. if nothing provided or no URLs provided then return null',
maxRecords: 5,
hintText: 'You may repeat a folder name on multiple lines to assign multiple images to the same folder.',
Expand Down Expand Up @@ -201,7 +200,7 @@ const Home = ({
});
processedImages.push(image);
const folder = processedFolders.find(f => f.name === item.value.getAttribute('data-folder'));

if (!folder) {
processedFolders.push(new Folder({ name: item.value.getAttribute('data-folder'), images: [image] }));
} else {
Expand Down

0 comments on commit af0c87f

Please sign in to comment.