OmPrakashSingh1704 commited on
Commit
8d9a1a3
1 Parent(s): acc0f68
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from options import Banner, Video
4
+ from huggingface_hub import login
5
+ import os
6
+ login(token=os.getenv("TOKEN"))
7
+
8
+ MAX_SEED = np.iinfo(np.int32).max
9
+ MAX_IMAGE_SIZE = 2048
10
+
11
+ with gr.Blocks() as demo:
12
+ gr.Markdown("# Create your own Advertisement")
13
+ with gr.Tab("Banner"):
14
+ gr.Markdown("# Take your banner to the next LEVEL!")
15
+ with gr.TabItem("Create your Banner"):
16
+ textInput = gr.Textbox(label="Enter the text to get a good start")
17
+ with gr.Accordion("Advanced Settings", open=False):
18
+ with gr.Row():
19
+ width = gr.Slider(
20
+ label="Width",
21
+ minimum=256,
22
+ maximum=MAX_IMAGE_SIZE,
23
+ step=8,
24
+ value=1024,
25
+ )
26
+
27
+ height = gr.Slider(
28
+ label="Height",
29
+ minimum=256,
30
+ maximum=MAX_IMAGE_SIZE,
31
+ step=32,
32
+ value=1024,
33
+ )
34
+
35
+ guidance_scale = gr.Slider(
36
+ label="Guidance Scale",
37
+ minimum=1,
38
+ maximum=15,
39
+ step=0.1,
40
+ value=3.5,
41
+ )
42
+
43
+ num_inference_steps = gr.Slider(
44
+ label="Number of Inference Steps",
45
+ minimum=1,
46
+ maximum=50,
47
+ step=1,
48
+ value=28,
49
+ )
50
+
51
+ submit = gr.Button("Submit")
52
+ submit.click(
53
+ fn=Banner.TextImage,
54
+ inputs=[textInput, width, height, guidance_scale, num_inference_steps],
55
+ outputs=gr.Image()
56
+ )
57
+
58
+ with gr.TabItem("Edit your Banner"):
59
+ img = gr.ImageMask(sources=["upload"], layers=False, transforms=[], format="png", label="base image",
60
+ show_label=True)
61
+ prompt = gr.Textbox(label="Enter the text to get a good start")
62
+ out_img=gr.Image()
63
+ btn = gr.Button()
64
+ btn.click(Banner.Image2Image, [prompt,img], out_img)
65
+
66
+ with gr.TabItem("Upgrade your Banner"):
67
+ img = gr.Image()
68
+ prompt = gr.Textbox(label="Enter the text to get a good start")
69
+ btn = gr.Button()
70
+ size = gr.Slider(label="Size", minimum=256, maximum=MAX_IMAGE_SIZE, step=8, value=1024)
71
+ out_img = gr.Image()
72
+ btn.click(Banner.Image2Image_2, [prompt, img,size,num_inference_steps], out_img)
73
+
74
+
75
+ with gr.Tab("Video"):
76
+ gr.Markdown("# Create your own Video")
77
+ img=gr.Image()
78
+ btn = gr.Button()
79
+ video=gr.Video()
80
+ btn.click(Video.Video, img, video)
81
+
82
+ demo.launch()
options/Banner.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from options.Banner_Model.Text2Banner import T2I
3
+ from options.Banner_Model.Image2Image import I2I
4
+ from options.Banner_Model.Image2Image_2 import I2I_2
5
+
6
+ def TextImage(prompt, width=1024, height=1024, guidance_scale=3.5,
7
+ num_inference_steps=28):
8
+ img = T2I(prompt, width, height, guidance_scale, num_inference_steps)
9
+ return img
10
+
11
+ def Image2Image(prompt,image, width=1024, height=1024, guidance_scale=3.5,num_inference_steps=28,strength=0.99):
12
+ return I2I(prompt, image, width, height, guidance_scale, num_inference_steps, strength)
13
+
14
+ def Image2Image_2(prompt,image,size,num_inference_steps=30):
15
+ return I2I_2(image, prompt,size,num_inference_steps)
options/Banner_Model/Image2Image.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import imageio
2
+ import numpy as np
3
+ from PIL import Image
4
+ from diffusers import AutoPipelineForInpainting
5
+ import torch
6
+
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ print(f"Using device for I2I: {device}")
9
+
10
+ # Load the inpainting pipeline
11
+ pipe = AutoPipelineForInpainting.from_pretrained(
12
+ "diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
13
+ torch_dtype=torch.float16, variant="fp16").to(device)
14
+
15
+
16
+ def resize_image(image, height, width):
17
+ """Resize image tensor to the desired height and width."""
18
+ return torch.nn.functional.interpolate(image, size=(height, width), mode='nearest')
19
+
20
+
21
+ def dummy(img):
22
+ """Save the composite image and generate a mask from the alpha channel."""
23
+ imageio.imwrite("output_image.png", img["composite"])
24
+
25
+ # Extract alpha channel from the first layer to create the mask
26
+ alpha_channel = img["layers"][0][:, :, 3]
27
+ mask = np.where(alpha_channel == 0, 0, 255).astype(np.uint8)
28
+
29
+ return img["background"], mask
30
+
31
+
32
+ def I2I(prompt, image, width=1024, height=1024, guidance_scale=8.0, num_inference_steps=20, strength=0.99):
33
+ img_url, mask = dummy(image)
34
+
35
+ # Resize image and mask to the target dimensions (height x width)
36
+ img_url = Image.fromarray(img_url, mode="RGB").resize((width, height))
37
+ mask_url = Image.fromarray(mask,mode="L").resize((width, height))
38
+
39
+ # Make sure both image and mask are converted into correct tensors
40
+ generator = torch.Generator(device=device).manual_seed(0)
41
+
42
+ # Generate the inpainted image
43
+ output = pipe(
44
+ prompt=prompt,
45
+ image=img_url,
46
+ mask_image=mask_url,
47
+ guidance_scale=guidance_scale,
48
+ num_inference_steps=num_inference_steps, # steps between 15 and 30 work well for us
49
+ strength=strength, # make sure to use `strength` below 1.0
50
+ generator=generator,
51
+ )
52
+
53
+ return output.images[0]
options/Banner_Model/Image2Image_2.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from controlnet_aux import LineartDetector
3
+ from diffusers import ControlNetModel,UniPCMultistepScheduler,FluxPipeline
4
+
5
+ device= "cuda" if torch.cuda.is_available() else "cpu"
6
+ print("Using device for I2I_2:", device)
7
+ processor = LineartDetector.from_pretrained("lllyasviel/Annotators")
8
+
9
+ checkpoint = "ControlNet-1-1-preview/control_v11p_sd15_lineart"
10
+ controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16).to(device)
11
+ pipe = FluxPipeline.from_pretrained(
12
+ "black-forest-labs/FLUX.1-dev", controlnet=controlnet, torch_dtype=torch.float16
13
+ ).to(device)
14
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
15
+ # pipe.enable_model_cpu_offload()
16
+
17
+ def I2I_2(image, prompt,size,num_inference_steps):
18
+ image.resize((size))
19
+ image=processor(image)
20
+ generator = torch.Generator(device=device).manual_seed(0)
21
+ image = pipe(prompt, num_inference_steps=num_inference_steps, generator=generator, image=image).images[0]
22
+ return image
23
+
options/Banner_Model/Text2Banner.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+
3
+
4
+ def T2I(prompt, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28):
5
+ # Initialize the model client
6
+ model = InferenceClient(model="black-forest-labs/FLUX.1-dev")
7
+
8
+ # Prepare the request parameters
9
+ payload = {
10
+ "prompt": prompt,
11
+ "width": width,
12
+ "height": height,
13
+ "guidance_scale": guidance_scale,
14
+ "num_inference_steps": num_inference_steps
15
+ }
16
+
17
+ # Remove None values to avoid sending unsupported arguments
18
+ payload = {k: v for k, v in payload.items() if v is not None}
19
+
20
+ # Make the request to generate an image
21
+ return model.text_to_image(**payload)
options/Banner_Model/__init__.py ADDED
File without changes
options/Banner_Model/__pycache__/Image2Image.cpython-310.pyc ADDED
Binary file (1.72 kB). View file
 
options/Banner_Model/__pycache__/Image2Image_2.cpython-310.pyc ADDED
Binary file (1.26 kB). View file
 
options/Banner_Model/__pycache__/Text2Banner.cpython-310.pyc ADDED
Binary file (726 Bytes). View file
 
options/Banner_Model/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (146 Bytes). View file
 
options/Banner_Model/__pycache__/controlnet_flux.cpython-310.pyc ADDED
Binary file (11.3 kB). View file
 
options/Banner_Model/__pycache__/pipeline_flux_controlnet_inpaint.cpython-310.pyc ADDED
Binary file (29.3 kB). View file
 
options/Banner_Model/__pycache__/transformer_flux.cpython-310.pyc ADDED
Binary file (13.9 kB). View file
 
options/Video.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from Video_model import Model
2
+
3
+ def Video(image):
4
+ return Model.Video(image)
options/Video_model/Model.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import StableVideoDiffusionPipeline
3
+ from diffusers.utils import load_image, export_to_video
4
+
5
+ device = "cuda" if torch.cuda.is_available() else "cpu"
6
+ print("Using device for video:", device)
7
+
8
+ pipeline = StableVideoDiffusionPipeline.from_pretrained(
9
+ "stabilityai/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16"
10
+ ).to(device)
11
+ # pipeline.enable_model_cpu_offload()
12
+ def Video(image):
13
+ image = load_image(image)
14
+ image = image.resize((1024, 576))
15
+
16
+ generator = torch.Generator(device=device).manual_seed(42)
17
+ frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]
18
+ # export_to_video(frames, "generated.mp4", fps=7)
19
+ return frames
options/Video_model/__init__.py ADDED
File without changes
options/__init__.py ADDED
File without changes
options/__pycache__/Banner.cpython-310.pyc ADDED
Binary file (920 Bytes). View file
 
options/__pycache__/Banner.cpython-312.pyc ADDED
Binary file (572 Bytes). View file
 
options/__pycache__/Video.cpython-310.pyc ADDED
Binary file (130 Bytes). View file
 
options/__pycache__/Video.cpython-312.pyc ADDED
Binary file (134 Bytes). View file
 
options/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (133 Bytes). View file
 
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ imageio
3
+ diffusers
4
+ controlnet_aux