Spaces:
Runtime error
Runtime error
File size: 2,070 Bytes
f966bca 6934dad 7015481 7bb3bd1 7015481 f966bca 7015481 6934dad 18598ad 6934dad 18598ad 1daab31 18598ad 1daab31 18598ad 1daab31 18598ad 7015481 18598ad 6934dad 7015481 1daab31 18598ad 1daab31 6934dad 7015481 6934dad 7bb3bd1 6934dad 7bb3bd1 7015481 6934dad f966bca |
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 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import gradio as gr
import requests
from PIL import Image
import base64
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
}
# Make the request
response = requests.post(API_URL, headers=headers, json=data)
# Check if the response was successful
if response.status_code != 200:
return f"Error: Received status code {response.status_code} with message: {response.text}"
# Try parsing JSON response
try:
result = response.json()
except ValueError as e:
return f"Error decoding JSON: {e}"
# Debug output to diagnose the structure of the returned 'result'
print("DEBUG:", result)
# Check if the API response contains an error message.
if 'error' in result:
return f"Error: {result['error']}"
# Assuming the API returns an image in base64 format.
if 'data' in result:
try:
base64_image = result['data'][0]
base64_data = base64_image.split(',')[1] if ',' in base64_image else base64_image
image_bytes = base64.b64decode(base64_data)
image = Image.open(BytesIO(image_bytes))
return image
except Exception as e:
return f"Error processing image data: {e}"
else:
return "Error: 'data' not found in response."
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.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() |