Skip to content

Commit

Permalink
Implementada API de Stable Difusion
Browse files Browse the repository at this point in the history
  • Loading branch information
TeenBiscuits committed Feb 17, 2024
1 parent 370a08d commit 591b2da
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions obtenerImagen.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const UnplashAccessKey = 'Tv4pz2NLEpgwFQ0034shKC_Q_rh10nzVmIOMY-ULLhY'
const StabilityAIKey = 'Bq7nC8nNKyTMLFwMY0eJfNKPgOPa2RHgJjVIPs3QxPmOgBHMFTfgBAwUbAne'
const gameGrid = document.getElementById('game-grid');
const nextButton = document.getElementById('next-btn');
const timerDisplay = document.getElementById('timer');
Expand All @@ -9,19 +10,49 @@ let originalEmptyTilePosition;
// Fetch a random square image from Unsplash

async function fetchRandomSquareImage() {
try {
const response = await fetch(`https://api.unsplash.com/photos/random?client_id=${UnplashAccessKey}&query=square`);
const data = await response.json();
return data.urls.regular; // Return the regular-sized image URL
} catch (error) {
console.error('Error fetching image:', error);
}
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
"key": StabilityAIKey,
"prompt": "ultra realistic close up portrait ((beautiful pale cyberpunk female with heavy black eyeliner))",
"negative_prompt": null,
"width": "512",
"height": "512",
"samples": "1",
"num_inference_steps": "20",
"seed": null,
"guidance_scale": 7.5,
"safety_checker": "yes",
"multi_lingual": "no",
"panorama": "no",
"self_attention": "no",
"upscale": "no",
"embeddings_model": null,
"webhook": null,
"track_id": null
});

var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};

try {
const response = await fetch("https://stablediffusionapi.com/api/v3/text2img", requestOptions);
const data = await response.json();
return data.output; // Return the regular-sized image URL
} catch (error) {
console.error('Error fetching image:', error);
}
}

// Slice the image into parts
async function sliceImage(imageUrl) {
// Coment esto cuando tengamos acceso a la app de nuevo
imageUrl = 'https://raw.githubusercontent.com/TeenBiscuits/CronoSquare/main/imagenes/imagenPrueba.jpg?token=GHSAT0AAAAAACNYDOKMXD2KJMUOMT4L5CDAZOQTYDQ';
//imageUrl = 'https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/generations/0-2fb9fd49-01b4-4bed-b8da-49d779e191de.png';
// Esperar respuesta de la api
const response = await fetch(imageUrl);
const blob = await response.blob();
Expand Down Expand Up @@ -181,13 +212,13 @@ function shuffle(array) {
let currentIndex = array.length;
let randomIndex;

/*

while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
*/


return array;
}
Expand Down

0 comments on commit 591b2da

Please sign in to comment.