Spaces:
Sleeping
Sleeping
Re-try
Browse files
app.py
CHANGED
|
@@ -4,8 +4,11 @@ from PIL import Image
|
|
| 4 |
import base64
|
| 5 |
|
| 6 |
import torch
|
| 7 |
-
|
|
|
|
| 8 |
from diffusers import StableDiffusionXLPipeline
|
|
|
|
|
|
|
| 9 |
|
| 10 |
#from transformers import pipeline
|
| 11 |
import gradio as gr
|
|
@@ -13,21 +16,45 @@ import gradio as gr
|
|
| 13 |
# Set Hugging Face API (needed for gated models)
|
| 14 |
hf_api_key = os.environ.get('HF_API_KEY')
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Load the Stable Diffusion pipeline
|
| 17 |
-
#model_id = "
|
| 18 |
-
#pipe =
|
| 19 |
# model_id,
|
|
|
|
| 20 |
# torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, # Use float16 on GPU, float32 on CPU
|
| 21 |
# use_auth_token=hf_api_key # Required for gated model
|
| 22 |
#)
|
| 23 |
|
| 24 |
# Load the Stable Diffusion XL pipeline
|
| 25 |
-
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 26 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 27 |
-
model_id,
|
| 28 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, # Use float16 on GPU, float32 on CPU
|
| 29 |
-
use_auth_token=hf_api_key # Required for gated model
|
| 30 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Move pipeline to GPU if available
|
| 33 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -44,26 +71,10 @@ def base64_to_pil(img_base64):
|
|
| 44 |
pil_image = Image.open(byte_stream)
|
| 45 |
return pil_image
|
| 46 |
|
| 47 |
-
def generate(prompt):
|
| 48 |
-
output = get_completion(prompt)
|
| 49 |
-
result_image = base64_to_pil(output)
|
| 50 |
-
return result_image
|
| 51 |
-
|
| 52 |
-
#def generate(prompt, negative_prompt, steps, guidance, width, height):
|
| 53 |
-
# # Ensure width and height are multiples of 8 (required by Stable Diffusion)
|
| 54 |
-
# width = int(width) - (int(width) % 8)
|
| 55 |
-
# height = int(height) - (int(height) % 8)
|
| 56 |
-
|
| 57 |
-
# # Generate image with Stable Diffusion
|
| 58 |
-
# output = pipe(
|
| 59 |
-
# prompt,
|
| 60 |
-
# negative_prompt=negative_prompt or None, # Handle empty negative prompt
|
| 61 |
-
# num_inference_steps=int(steps),
|
| 62 |
-
# guidance_scale=float(guidance),
|
| 63 |
-
# width=width,
|
| 64 |
-
# height=height
|
| 65 |
-
# )
|
| 66 |
-
# return output.images[0] # Return the first generated image (PIL format)
|
| 67 |
|
| 68 |
# Generate function
|
| 69 |
def generate(prompt, negative_prompt, steps, guidance, width, height):
|
|
@@ -82,6 +93,7 @@ def generate(prompt, negative_prompt, steps, guidance, width, height):
|
|
| 82 |
)
|
| 83 |
return output.images[0] # Return the first generated image (PIL format)
|
| 84 |
|
|
|
|
| 85 |
# Create Gradio interface
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("# Image Generation with Stable Diffusion")
|
|
|
|
| 4 |
import base64
|
| 5 |
|
| 6 |
import torch
|
| 7 |
+
|
| 8 |
+
from diffusers import EulerDiscreteScheduler
|
| 9 |
from diffusers import StableDiffusionXLPipeline
|
| 10 |
+
from diffusers import StableDiffusion3Pipeline
|
| 11 |
+
from diffusers import StableDiffusionPipeline
|
| 12 |
|
| 13 |
#from transformers import pipeline
|
| 14 |
import gradio as gr
|
|
|
|
| 16 |
# Set Hugging Face API (needed for gated models)
|
| 17 |
hf_api_key = os.environ.get('HF_API_KEY')
|
| 18 |
|
| 19 |
+
# Use the Euler scheduler here instead
|
| 20 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
|
| 21 |
+
|
| 22 |
+
# Load the Stable Diffusion pipeline
|
| 23 |
+
model_id = "sd-legacy/stable-diffusion-v1-5"
|
| 24 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 25 |
+
model_id,
|
| 26 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, # Use float16 on GPU, float32 on CPU
|
| 27 |
+
scheduler=scheduler,
|
| 28 |
+
use_auth_token=hf_api_key # Required for gated model
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
# Load the Stable Diffusion pipeline
|
| 32 |
+
#model_id = "stabilityai/stable-diffusion-3.5-medium"
|
| 33 |
+
#pipe = SD3Transformer2DModel.from_pretrained(
|
| 34 |
# model_id,
|
| 35 |
+
# subfolder="transformer",
|
| 36 |
# torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, # Use float16 on GPU, float32 on CPU
|
| 37 |
# use_auth_token=hf_api_key # Required for gated model
|
| 38 |
#)
|
| 39 |
|
| 40 |
# Load the Stable Diffusion XL pipeline
|
| 41 |
+
#model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 42 |
+
#pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 43 |
+
# model_id,
|
| 44 |
+
# torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, # Use float16 on GPU, float32 on CPU
|
| 45 |
+
# use_auth_token=hf_api_key # Required for gated model
|
| 46 |
+
#)
|
| 47 |
+
|
| 48 |
+
# Load the Stable Diffusion pipeline
|
| 49 |
+
#model_id = "stabilityai/stable-diffusion-3-medium"
|
| 50 |
+
#model_id = "stabilityai/stable-diffusion-3-medium-diffusers"
|
| 51 |
+
#pipe = StableDiffusion3Pipeline.from_pretrained(
|
| 52 |
+
# model_id,
|
| 53 |
+
# torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, # Use float16 on GPU, float32 on CPU,
|
| 54 |
+
# scheduler=scheduler,
|
| 55 |
+
# use_auth_token=hf_api_key # Required for gated model
|
| 56 |
+
#)
|
| 57 |
+
|
| 58 |
|
| 59 |
# Move pipeline to GPU if available
|
| 60 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 71 |
pil_image = Image.open(byte_stream)
|
| 72 |
return pil_image
|
| 73 |
|
| 74 |
+
#def generate(prompt):
|
| 75 |
+
# output = get_completion(prompt)
|
| 76 |
+
# result_image = base64_to_pil(output)
|
| 77 |
+
# return result_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# Generate function
|
| 80 |
def generate(prompt, negative_prompt, steps, guidance, width, height):
|
|
|
|
| 93 |
)
|
| 94 |
return output.images[0] # Return the first generated image (PIL format)
|
| 95 |
|
| 96 |
+
|
| 97 |
# Create Gradio interface
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
gr.Markdown("# Image Generation with Stable Diffusion")
|