Error: cannot identify image file <_io.BytesIO object at 0x000001F608AD6860>
code:
import gradio as gr
import requests
import base64
from PIL import Image
from io import BytesIO
API_URL = “https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5”
headers = {
“Authorization”: “Bearer hf_aklgxxxxxxxxxxxxxxxxxx”,
“Content-Type”: “application/json”,
}
def get_image(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
def generate(prompt):
print(“Generating image for prompt:”, prompt)
payload = {“prompt”: prompt}
response = get_image(payload)
try:
# Convert the base64 image data to a PIL Image
image_data = response
image = Image.open(BytesIO(image_data))
return image
except Exception as e:
print("Error:", e)
return None
gr.close_all()
demo = gr.Interface(
fn=generate,
inputs=[gr.Textbox(label=“Your prompt”)],
outputs=[gr.Image(label=“Result”)],
title=“Image Generation with Stable Diffusion”,
description=“Generate any image with Stable Diffusion”,
allow_flagging=“never”,
examples=[ “a red bus”, “a mecha robot in a favela”]
)
demo.launch()