Can you help me to create a block like this code?

Please help me make blocks,
so there are 2 inputs containing a prompt and a negative prompt,
2 buttons to send and download images
when sent, api will send us a picture.
api also often has problems, usually the status is 503 which means the model is being loaded and takes 30 seconds, so repeat to send if the status remains 503

import requests
from PIL import Image
import io
import time  # Import module time
import random

modelsd = "tensor-diffusion/Dark-Sushi-2.5D"
API_URL = "https://api-inference.huggingface.co/models/" + str(modelsd)
headers = {"Authorization": "Bearer hf_CfvnJwQkxxxxxxxRxxxxKOwmWkhty"}

Prompt = input("Enter Prompt:")
# NPrompt = input("Negative Prompt")
# Steps = input("Steps:")

acak = random.randint(245789, 945789)
acak_str = str(acak)

def query(payload):
    while True:
        response = requests.post(API_URL, headers=headers, json=payload)
        if response.status_code == 200:
            return response.content
        else:
            print("API request failed with status code:", response.status_code)
            # Tunggu beberapa saat sebelum mencoba lagi
            time.sleep(5)  # Ganti angka 5 dengan jangka waktu yang sesuai

image_bytes = query({
    "inputs": Prompt + " seed:" + acak_str,
    "negative_prompt": "earrings, deformed, blurry, badanatomy, bad proportions, cloned face, disfigured, out of frame, extralimbs, bad anatomy, gross proportions, malformed limbs, missing arms,missing legs, extra arms, extra legs, mutated hands, fused fingers, toomany fingers, long neck, text, letters, signature, web address, copyrightname, username, error, extra digit, fewer digits, loadscreen, grid, stockimage, a stock photo, promo poster, fat, text, logo, brand, watermark,water mark, low quality,earrings",
    "num_inference_steps": 50,
})

if image_bytes is not None:
    try:
        image = Image.open(io.BytesIO(image_bytes))
        # Menyimpan gambar ke file (opsional)
        image.save("outputs_image.png")
        image.show()  # Menampilkan gambar
        
    except Exception as e:
        print("Error while opening or displaying the image:", str(e))


It’s probably a lot easier just to use our OpenAI integration if you’re just getting started @mailx9hj


However, if you’ve been using the platform for a while (and gone through all the tutorials) then you can use the API blocks to connect directly to the Hugging Face API


FInally:

I would strongly advise against publishing your bearer token publicly like this, it’s a bad idea

1 Like

What does the initial api call to stable diffusion return? An image url or a binary file or something else?

The api call is easy but looks like there’s some file conversion going on too that might(probably) not be possible with Thunkable