Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,95 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from diffusers import
|
3 |
-
import torch
|
4 |
-
import
|
5 |
-
|
6 |
-
# Initialize the pipelines
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
img2img_pipe.
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Initialize the pipelines
|
7 |
+
text2img_pipe = StableDiffusionPipeline.from_pretrained(
|
8 |
+
"stabilityai/sdxl-turbo",
|
9 |
+
torch_dtype=torch.float16 # Removed the revision argument
|
10 |
+
)
|
11 |
+
text2img_pipe.to("cuda")
|
12 |
+
|
13 |
+
img2img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
14 |
+
"stabilityai/sdxl-turbo",
|
15 |
+
torch_dtype=torch.float16 # Removed the revision argument
|
16 |
+
)
|
17 |
+
img2img_pipe.to("cuda")
|
18 |
+
|
19 |
+
# Helper function to load LoRA weights (if supported by the model)
|
20 |
+
def load_lora_weights(pipe, model_name, weight_name):
|
21 |
+
try:
|
22 |
+
pipe.load_lora_weights(model_name, weight_name=weight_name)
|
23 |
+
except AttributeError:
|
24 |
+
print(f"LoRA weights cannot be loaded for {model_name}. Skipping...")
|
25 |
+
|
26 |
+
# Load LoRA weights (if applicable)
|
27 |
+
load_lora_weights(text2img_pipe, "AbdalrhmanRi/SDXL-Turbo-With-AppleVisionPro", "pytorch_lora_weights.safetensors")
|
28 |
+
load_lora_weights(img2img_pipe, "AbdalrhmanRi/SDXL-Turbo-With-AppleVisionPro", "pytorch_lora_weights.safetensors")
|
29 |
+
|
30 |
+
|
31 |
+
def generate_image(prompt, init_image):
|
32 |
+
if init_image is None:
|
33 |
+
# Text-to-Image generation
|
34 |
+
output_image = text2img_pipe(
|
35 |
+
prompt=prompt,
|
36 |
+
num_inference_steps=40,
|
37 |
+
guidance_scale=7.5,
|
38 |
+
height=512
|
39 |
+
).images[0]
|
40 |
+
|
41 |
+
yield output_image, None
|
42 |
+
|
43 |
+
# Refining step (if needed)
|
44 |
+
output_refiner_image = text2img_pipe(
|
45 |
+
prompt=prompt,
|
46 |
+
image=output_image,
|
47 |
+
num_inference_steps=40,
|
48 |
+
guidance_scale=1.0,
|
49 |
+
height=512
|
50 |
+
).images[0]
|
51 |
+
|
52 |
+
yield output_image, output_refiner_image
|
53 |
+
else:
|
54 |
+
# Image-to-Image generation
|
55 |
+
init_image = init_image.resize((512, 512), Image.LANCZOS)
|
56 |
+
|
57 |
+
output_image = img2img_pipe(
|
58 |
+
prompt=prompt,
|
59 |
+
image=init_image,
|
60 |
+
num_inference_steps=40,
|
61 |
+
strength=0.75,
|
62 |
+
guidance_scale=7.5,
|
63 |
+
height=512
|
64 |
+
).images[0]
|
65 |
+
|
66 |
+
yield output_image, None
|
67 |
+
|
68 |
+
# Refining step (if needed)
|
69 |
+
output_refiner_image = img2img_pipe(
|
70 |
+
prompt=prompt,
|
71 |
+
image=output_image,
|
72 |
+
num_inference_steps=40,
|
73 |
+
strength=0.75,
|
74 |
+
guidance_scale=1.0,
|
75 |
+
height=512
|
76 |
+
).images[0]
|
77 |
+
|
78 |
+
yield output_image, output_refiner_image
|
79 |
+
|
80 |
+
# Define the Gradio interface
|
81 |
+
interface = gr.Interface(
|
82 |
+
fn=generate_image,
|
83 |
+
inputs=[
|
84 |
+
gr.Textbox(label="Prompt", lines=2, placeholder="Enter your prompt here..."),
|
85 |
+
gr.Image(type="pil", label="Initial Image (Optional)", height=360)
|
86 |
+
],
|
87 |
+
outputs=[gr.Image(type="pil", label="Generated Image"), gr.Image(type="pil", label="Refined Image")],
|
88 |
+
live=True, # Add live=True for real-time updates
|
89 |
+
title="Generate Image Using Generative AI",
|
90 |
+
theme=gr.themes.Default(primary_hue="green"),
|
91 |
+
description="Text-to-Image or Image-to-Image Generation with SDXL-Turbo."
|
92 |
+
)
|
93 |
+
|
94 |
+
# Launch the interface
|
95 |
+
interface.launch()
|