Jangai commited on
Commit
7a58110
·
verified ·
1 Parent(s): 31f5d1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -1,26 +1,52 @@
1
- from diffusers import StableDiffusionXLPipeline
2
- import torch
 
3
  import gradio as gr
 
4
 
5
- # Load the Stable Diffusion XL model
6
- pipeline = StableDiffusionXLPipeline.from_pretrained(
7
- "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
8
- ).to("cuda")
9
 
10
- def generate_image(prompt, prompt_2):
11
- # Generate the image based on the prompts
12
- image = pipeline(prompt=prompt, prompt_2=prompt_2).images[0]
13
-
14
- # Convert the PIL image to a format that Gradio can display
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  return image
16
 
17
- # Define the Gradio interface
18
- iface = gr.Interface(fn=generate_image,
19
- inputs=[gr.Textbox(label="Prompt 1: Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"),
20
- gr.Textbox(label="Prompt 2: Van Gogh painting")],
21
- outputs="image",
22
- title="Stable Diffusion XL Image Generator",
23
- description="Generate images with Stable Diffusion XL based on two prompts.")
 
 
 
 
 
 
 
 
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)