ModelDiffusion / app.py
GamerC0der's picture
Update app.py
1daab31 verified
raw
history blame
1.84 kB
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
}
response = requests.post(API_URL, headers=headers, json=data)
if response.status_code != 200:
return "Error: Unexpected status code {} with message {}".format(response.status_code, response.text)
result = response.json()
# Debug output to help figure out what result looks like
print("DEBUG:", result)
# Check if the API response contains an error.
if 'error' in result:
return "Error: " + result['error'], None
# Assuming the API returns an image in base64 format.
if 'data' in result:
base64_image = result['data'][0] # This might need to be adjusted based on API return structure
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
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()