Jangai commited on
Commit
8f528d9
·
verified ·
1 Parent(s): d4ee5ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -4,42 +4,43 @@ 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)..."),
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()
 
4
  import gradio as gr
5
  import os
6
 
7
+ # Replace "YOUR_HUGGING_FACE_API_TOKEN" with your actual Hugging Face API token
8
+ API_TOKEN = os.getenv("HF_API_TOKEN")
9
+ if not API_TOKEN:
10
+ raise ValueError("Hugging Face API token not found. Please set the HF_API_TOKEN environment variable.")
11
 
12
+ API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
13
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
14
+
15
+ def generate_image(prompt, negative_prompt, guidance_scale, width, height, num_inference_steps):
16
  payload = {
17
  "inputs": prompt,
18
  "parameters": {
19
+ "negative_prompt": negative_prompt,
20
+ "guidance_scale": guidance_scale,
21
  "width": width,
22
  "height": height,
 
23
  "num_inference_steps": num_inference_steps,
24
  },
25
  }
 
 
 
 
26
  response = requests.post(API_URL, headers=headers, json=payload)
27
  image_bytes = response.content
28
  image = Image.open(io.BytesIO(image_bytes))
29
  return image
30
 
 
31
  iface = gr.Interface(
32
  fn=generate_image,
33
  inputs=[
34
  gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
35
+ gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here (optional)...", optional=True),
36
+ gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, value=7.5),
37
+ gr.Slider(label="Width", minimum=768, maximum=1024, step=1, value=1024),
38
+ gr.Slider(label="Height", minimum=768, maximum=1024, step=1, value=768),
39
+ gr.Slider(label="Number of Inference Steps", minimum=20, maximum=50, step=1, value=30)
40
  ],
41
  outputs=gr.Image(type="pil"),
42
  title="Stable Diffusion XL Image Generator",
43
  description="Generate images with Stable Diffusion XL. Provide a prompt, optionally specify a negative prompt, and adjust other parameters as desired."
44
  )
45
 
 
46
  iface.launch()