File size: 1,934 Bytes
7a58110 31f5d1f 7a58110 31f5d1f 7a58110 31f5d1f 7a58110 31f5d1f 7a58110 31f5d1f 7a58110 |
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 |
import requests
import io
from PIL import Image
import gradio as gr
import os # To access environment variables
# Access the Hugging Face API token securely
API_TOKEN = os.getenv("HF_API_TOKEN")
if not API_TOKEN:
raise ValueError("Hugging Face API token not found. Please check your Space's secrets configuration.")
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
headers = {"Authorization": f"Bearer {API_TOKEN}"}
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
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,
},
}
image_bytes = query(payload)
image = Image.open(io.BytesIO(image_bytes))
return image
# Create Gradio interface
iface = gr.Interface(
fn=generate_image,
inputs=[
gr.Textbox(label="Prompt"),
gr.Textbox(label="Negative Prompt"),
gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, default=7.5),
gr.Slider(label="Width", minimum=768, maximum=1024, step=1, default=1024),
gr.Slider(label="Height", minimum=768, maximum=1024, step=1, default=768),
gr.Slider(label="Number of Inference Steps", minimum=20, maximum=50, step=1, default=30)
],
outputs=gr.Image(type="pil"),
title="Stable Diffusion XL Image Generator",
description="Generate images with Stable Diffusion XL. Provide a prompt, specify any negative prompts, and adjust the image generation parameters.",
)
# Launch the Gradio app, setting share=True for Hugging Face Spaces
iface.launch(share=True)
|