Reputation: 1
import requests
from PIL import Image
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
headers = {"Authorization": f"Bearer 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"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
image_bytes = query({
"inputs": "Astronaut riding a horse",
})
image = Image.open(io.BytesIO(image_bytes))
I ran through Inference API, created my token.. pasted it in headers. i was expecting that as soon as i run the code .
The image will appear on screen on the terminal but it didnt happen and also no error was shown.
Upvotes: 0
Views: 626
Reputation: 122240
You need to use .show()
to display the image in pillow, e.g.
import io
import requests
from PIL import Image
endpoint = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
headers = {"Authorization": f"Bearer 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"}
payload = {"inputs": "Astronaut riding a horse"}
response = requests.post(endpoint, headers=headers, json=payload)
im = Image.open(io.BytesIO(response.content))
im.show() # This will open up the image in your OS default image viewer
To save the image to file, there are several options https://stackoverflow.com/a/75824531/610569, you can try this to save as a numpy array:
import io
import requests
from PIL import Image
import numpy as np
endpoint = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
headers = {"Authorization": f"Bearer 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"}
payload = {"inputs": "Astronaut riding a horse"}
response = requests.post(endpoint, headers=headers, json=payload)
img = Image.open(io.BytesIO(response.content))
# Converts and save image into numpy array.
np.save('myimage.npy', np.asarray(img))
Then to read the saved file:
import numpy as np
from PIL import Image
# Loads a npy file to Image
img_arr = np.load('dpreview.npy')
img = Image.fromarray(img_arr.astype(np.uint8))
Note: While the code snippet above works, I'm not sure if (ab)using the API to to do requests.post()
on api-inference.huggingface.co/models/runwayml
is encouraged.
Upvotes: 0