Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import base64 | |
from PIL import Image | |
from io import BytesIO | |
def query_hf_image_generation(api_key, prompt): | |
API_URL = f"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0" | |
headers = { | |
"Authorization": f"Bearer {api_key}", | |
"Content-Type": "application/json" | |
} | |
data = { | |
"inputs": prompt | |
} | |
response = requests.post(API_URL, headers=headers, json=data) | |
result = response.json() | |
# Check if the API response contains an error. | |
if 'error' in result: | |
return f"Error: {result['error']}", None | |
# Assuming the API returns an image in base64 format. | |
image_data = result['data'][0] # You might need to adjust this path according to the actual API response | |
image = Image.open(BytesIO(base64.b64decode(image_data))) | |
return image | |
iface = gr.Interface( | |
fn=query_hf_image_generation, | |
inputs=[ | |
gr.Textbox(label="Hugging Face API Key", placeholder="Enter your Hugging Face API Key here..."), | |
gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"), | |
], | |
outputs=gr.outputs.Image(label="Generated Image"), | |
title="Stable Diffusion XL Image Generator", | |
description="Enter your API Key and a prompt to generate an image using the Stable Diffusion XL model from Hugging Face." | |
) | |
iface.launch() |