AbdalrhmanRi commited on
Commit
ffe06c3
·
verified ·
1 Parent(s): b075ba0

Upload img-text2img.py

Browse files
Files changed (1) hide show
  1. img-text2img.py +80 -0
img-text2img.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()