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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -4,7 +4,7 @@ from PIL import Image
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.")
@@ -12,17 +12,19 @@ if not API_TOKEN:
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))
@@ -32,7 +34,7 @@ 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),
 
4
  import gradio as gr
5
  import os
6
 
7
+ # Ensure your API token is correctly set in your environment variables
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.")
 
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=7.5, width=1024, height=768, num_inference_steps=30):
16
  payload = {
17
  "inputs": prompt,
18
  "parameters": {
 
19
  "guidance_scale": guidance_scale,
20
  "width": width,
21
  "height": height,
22
  "num_inference_steps": num_inference_steps,
23
  },
24
  }
25
+ if negative_prompt: # Only add negative_prompt to payload if it's provided
26
+ payload["parameters"]["negative_prompt"] = negative_prompt
27
+
28
  response = requests.post(API_URL, headers=headers, json=payload)
29
  image_bytes = response.content
30
  image = Image.open(io.BytesIO(image_bytes))
 
34
  fn=generate_image,
35
  inputs=[
36
  gr.Textbox(label="Prompt", placeholder="Enter your prompt here..."),
37
+ gr.Textbox(label="Negative Prompt", placeholder="Enter a negative prompt here (optional)..."),
38
  gr.Slider(label="Guidance Scale", minimum=1, maximum=20, step=0.1, value=7.5),
39
  gr.Slider(label="Width", minimum=768, maximum=1024, step=1, value=1024),
40
  gr.Slider(label="Height", minimum=768, maximum=1024, step=1, value=768),