AbdalrhmanRi commited on
Commit
2343433
·
verified ·
1 Parent(s): f810d23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -80
app.py CHANGED
@@ -1,80 +1,95 @@
1
- import gradio as gr
2
- from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
3
- import torch
4
- import peft
5
-
6
- # Initialize the pipelines
7
-
8
- text2img_pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16")
9
- text2img_pipe.to("cuda")
10
- text2img_pipe.load_lora_weights("AbdalrhmanRi/SDXL-Turbo-With-AppleVisionPro", weight_name="pytorch_lora_weights.safetensors")
11
-
12
- img2img_pipe = AutoPipelineForImage2Image.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16")
13
- img2img_pipe.to("cuda")
14
- img2img_pipe.load_lora_weights("AbdalrhmanRi/SDXL-Turbo-With-AppleVisionPro", weight_name="pytorch_lora_weights.safetensors")
15
-
16
-
17
- def generate_image(prompt, init_image):
18
- if init_image is None:
19
-
20
- # Text-to-Image generation
21
- output_image = text2img_pipe(
22
- prompt,
23
- num_inference_steps=40,
24
- guidance_scale=2.0,
25
- height=480
26
- ).images[0]
27
-
28
- yield output_image, None
29
-
30
- output_refiner_image = text2img_pipe(
31
- prompt=prompt,
32
- image=output_image,
33
- guidance_scale=1.0,
34
- height=480
35
- ).images[0]
36
-
37
- yield output_image, output_refiner_image
38
-
39
-
40
- else:
41
-
42
- # Image-to-Image generation
43
- init_image = init_image.resize((512, 512))
44
-
45
- output_image = img2img_pipe(
46
- prompt,
47
- image=init_image,
48
- num_inference_steps=40,
49
- strength=0.5,
50
- guidance_scale=2.0,
51
- height=480
52
- ).images[0]
53
-
54
- yield output_image, None
55
-
56
- output_refiner_image = img2img_pipe(
57
- prompt=prompt,
58
- image=output_image,
59
- strength=0.5,
60
- guidance_scale=1.0,
61
- height=480
62
- ).images[0]
63
-
64
- yield output_image, output_refiner_image
65
-
66
- # Define the Gradio interface
67
- interface = gr.Interface(
68
- fn=generate_image,
69
- inputs=[
70
- gr.Textbox(label="Prompt", lines=2, placeholder="Enter your prompt here..."),
71
- gr.Image(type="pil", label="Initial Image (Optional)", height=360)
72
- ],
73
- outputs=[gr.Image(type="pil", label="Generated Image"), gr.Image(type="pil", label="Refined Image")],
74
- title="Genrate Image Using Generative AI",
75
- theme=gr.themes.Default(primary_hue="green"),
76
- description="Text-to-Image or Image-to-Image Generation with SDXL-Turbo."
77
- )
78
-
79
- # Launch the interface
80
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()