Update app.py
Browse files
app.py
CHANGED
@@ -2,50 +2,44 @@ import requests
|
|
2 |
import io
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
-
import os
|
6 |
|
7 |
-
#
|
8 |
-
API_TOKEN = os.getenv("HF_API_TOKEN")
|
9 |
|
10 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
image = Image.open(io.BytesIO(image_bytes))
|
33 |
return image
|
34 |
|
35 |
-
#
|
36 |
iface = gr.Interface(
|
37 |
fn=generate_image,
|
38 |
inputs=[
|
39 |
-
gr.
|
40 |
-
gr.
|
41 |
-
gr.Slider(label="
|
42 |
-
gr.Slider(label="
|
43 |
-
|
|
|
44 |
],
|
45 |
outputs=gr.Image(type="pil"),
|
46 |
title="Stable Diffusion XL Image Generator",
|
47 |
-
description="Generate images with Stable Diffusion XL. Provide a prompt, specify
|
48 |
)
|
49 |
|
50 |
-
# Launch the Gradio app
|
51 |
-
iface.launch(
|
|
|
2 |
import io
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
+
import os
|
6 |
|
7 |
+
# Assuming you've set up API_TOKEN and other constants as before
|
|
|
8 |
|
9 |
+
def generate_image(prompt, negative_prompt=None, guidance_scale=7.5, width=1024, height=768, num_inference_steps=30):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
payload = {
|
11 |
"inputs": prompt,
|
12 |
"parameters": {
|
|
|
|
|
13 |
"width": width,
|
14 |
"height": height,
|
15 |
+
"guidance_scale": guidance_scale,
|
16 |
"num_inference_steps": num_inference_steps,
|
17 |
},
|
18 |
}
|
19 |
+
# Include negative prompt in the payload if provided
|
20 |
+
if negative_prompt:
|
21 |
+
payload["parameters"]["negative_prompt"] = negative_prompt
|
22 |
+
|
23 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
24 |
+
image_bytes = response.content
|
25 |
image = Image.open(io.BytesIO(image_bytes))
|
26 |
return image
|
27 |
|
28 |
+
# Define Gradio interface components
|
29 |
iface = gr.Interface(
|
30 |
fn=generate_image,
|
31 |
inputs=[
|
32 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
|
33 |
+
gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here (optional)...", optional=True),
|
34 |
+
gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, default=7.5),
|
35 |
+
gr.Slider(label="Width", minimum=768, maximum=1024, step=1, default=1024),
|
36 |
+
gr.Slider(label="Height", minimum=768, maximum=1024, step=1, default=768),
|
37 |
+
gr.Slider(label="Number of Inference Steps", minimum=20, maximum=50, step=1, default=30)
|
38 |
],
|
39 |
outputs=gr.Image(type="pil"),
|
40 |
title="Stable Diffusion XL Image Generator",
|
41 |
+
description="Generate images with Stable Diffusion XL. Provide a prompt, optionally specify a negative prompt, and adjust other parameters as desired."
|
42 |
)
|
43 |
|
44 |
+
# Launch the Gradio app
|
45 |
+
iface.launch()
|