File size: 1,598 Bytes
0238a29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<script>

  const HF_TOKEN = "YOUR_HUGGING_FACE_TOKEN"; // Replace with your token



  async function generateImage() {

    const prompt = document.getElementById("prompt").value;

    const imageContainer = document.getElementById("image");

    

    if (!prompt) {

      imageContainer.innerHTML = "Please enter a prompt.";

      return;

    }



    imageContainer.innerHTML = "Generating...";



    try {

      // Generate a RANDOM SEED (0 to 1,000,000)

      const randomSeed = Math.floor(Math.random() * 1000000);



      // Add parameters for randomness

      const response = await fetch(

        "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",

        {

          method: "POST",

          headers: {

            "Authorization": `Bearer ${HF_TOKEN}`,

            "Content-Type": "application/json",

          },

          body: JSON.stringify({

            inputs: prompt,

            parameters: {

              seed: randomSeed,       // Random seed for variation

              guidance_scale: 7.5,   // Controls creativity (7-15)

              num_inference_steps: 50 // More steps = more details (20-100)

            }

          }),

        }

      );



      if (!response.ok) throw new Error("API error: " + response.status);



      const imageBlob = await response.blob();

      const imageUrl = URL.createObjectURL(imageBlob);

      imageContainer.innerHTML = `<img src="${imageUrl}" style="max-width:100%;">`;

    } catch (error) {

      imageContainer.innerHTML = "Error: " + error.message;

    }

  }

</script>