Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,52 @@
|
|
1 |
-
|
2 |
-
import
|
|
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
8 |
-
).to("cuda")
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return image
|
16 |
|
17 |
-
#
|
18 |
-
iface = gr.Interface(
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
# Launch the Gradio app
|
26 |
-
iface.launch()
|
|
|
1 |
+
import requests
|
2 |
+
import io
|
3 |
+
from PIL import Image
|
4 |
import gradio as gr
|
5 |
+
import os # To access environment variables
|
6 |
|
7 |
+
# Access the Hugging Face API token securely
|
8 |
+
API_TOKEN = os.getenv("HF_API_TOKEN")
|
|
|
|
|
9 |
|
10 |
+
if not API_TOKEN:
|
11 |
+
raise ValueError("Hugging Face API token not found. Please check your Space's secrets configuration.")
|
12 |
+
|
13 |
+
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
14 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
15 |
+
|
16 |
+
def query(payload):
|
17 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
+
return response.content
|
19 |
+
|
20 |
+
def generate_image(prompt, negative_prompt, guidance_scale, width, height, num_inference_steps):
|
21 |
+
payload = {
|
22 |
+
"inputs": prompt,
|
23 |
+
"parameters": {
|
24 |
+
"negative_prompt": negative_prompt,
|
25 |
+
"guidance_scale": guidance_scale,
|
26 |
+
"width": width,
|
27 |
+
"height": height,
|
28 |
+
"num_inference_steps": num_inference_steps,
|
29 |
+
},
|
30 |
+
}
|
31 |
+
image_bytes = query(payload)
|
32 |
+
image = Image.open(io.BytesIO(image_bytes))
|
33 |
return image
|
34 |
|
35 |
+
# Create Gradio interface
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=generate_image,
|
38 |
+
inputs=[
|
39 |
+
gr.Textbox(label="Prompt"),
|
40 |
+
gr.Textbox(label="Negative Prompt"),
|
41 |
+
gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, default=7.5),
|
42 |
+
gr.Slider(label="Width", minimum=768, maximum=1024, step=1, default=1024),
|
43 |
+
gr.Slider(label="Height", minimum=768, maximum=1024, step=1, default=768),
|
44 |
+
gr.Slider(label="Number of Inference Steps", minimum=20, maximum=50, step=1, default=30)
|
45 |
+
],
|
46 |
+
outputs=gr.Image(type="pil"),
|
47 |
+
title="Stable Diffusion XL Image Generator",
|
48 |
+
description="Generate images with Stable Diffusion XL. Provide a prompt, specify any negative prompts, and adjust the image generation parameters.",
|
49 |
+
)
|
50 |
|
51 |
+
# Launch the Gradio app, setting share=True for Hugging Face Spaces
|
52 |
+
iface.launch(share=True)
|