SDXL_Test / app.py
Jangai's picture
Update app.py
8f528d9 verified
raw
history blame
1.89 kB
import requests
import io
from PIL import Image
import gradio as gr
import os
# Replace "YOUR_HUGGING_FACE_API_TOKEN" with your actual Hugging Face API token
API_TOKEN = os.getenv("HF_API_TOKEN")
if not API_TOKEN:
raise ValueError("Hugging Face API token not found. Please set the HF_API_TOKEN environment variable.")
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
headers = {"Authorization": f"Bearer {API_TOKEN}"}
def generate_image(prompt, negative_prompt, guidance_scale, width, height, num_inference_steps):
payload = {
"inputs": prompt,
"parameters": {
"negative_prompt": negative_prompt,
"guidance_scale": guidance_scale,
"width": width,
"height": height,
"num_inference_steps": num_inference_steps,
},
}
response = requests.post(API_URL, headers=headers, json=payload)
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
return image
iface = gr.Interface(
fn=generate_image,
inputs=[
gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here (optional)...", optional=True),
gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, value=7.5),
gr.Slider(label="Width", minimum=768, maximum=1024, step=1, value=1024),
gr.Slider(label="Height", minimum=768, maximum=1024, step=1, value=768),
gr.Slider(label="Number of Inference Steps", minimum=20, maximum=50, step=1, value=30)
],
outputs=gr.Image(type="pil"),
title="Stable Diffusion XL Image Generator",
description="Generate images with Stable Diffusion XL. Provide a prompt, optionally specify a negative prompt, and adjust other parameters as desired."
)
iface.launch()