yash commited on
Commit
1c61c14
1 Parent(s): ee84362

first commit

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from diffusers import StableDiffusionImg2ImgPipeline
4
+ from diffusers import DDIMScheduler,EulerDiscreteScheduler,EulerAncestralDiscreteScheduler,UniPCMultistepScheduler
5
+ from diffusers import KDPM2DiscreteScheduler,KDPM2AncestralDiscreteScheduler,PNDMScheduler
6
+ from diffusers import DPMSolverMultistepScheduler
7
+ import random
8
+
9
+
10
+
11
+
12
+ def set_pipeline(model_id_repo,scheduler):
13
+
14
+ model_ids_dict = {
15
+ "dreamshaper": "Lykon/DreamShaper",
16
+ "deliberate": "soren127/Deliberate",
17
+ "runwayml": "runwayml/stable-diffusion-v1-5",
18
+ "Realistic_Vision_V5_1_noVAE":"SG161222/Realistic_Vision_V5.1_noVAE"
19
+ }
20
+ model_id = model_id_repo
21
+ model_repo = model_ids_dict.get(model_id)
22
+ print("model_repo :",model_repo)
23
+
24
+
25
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
26
+ model_repo,
27
+ # torch_dtype=torch.float16, # to run on cpu
28
+ use_safetensors=True,
29
+ ).to("cpu")
30
+
31
+ # pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
32
+ # model_repo,
33
+ # torch_dtype=torch.float16, # to run on cpu
34
+ # use_safetensors=True,
35
+ # ).to("cuda")
36
+
37
+
38
+ scheduler_classes = {
39
+ "DDIM": DDIMScheduler,
40
+ "Euler": EulerDiscreteScheduler,
41
+ "Euler a": EulerAncestralDiscreteScheduler,
42
+ "UniPC": UniPCMultistepScheduler,
43
+ "DPM2 Karras": KDPM2DiscreteScheduler,
44
+ "DPM2 a Karras": KDPM2AncestralDiscreteScheduler,
45
+ "PNDM": PNDMScheduler,
46
+ "DPM++ 2M Karras": DPMSolverMultistepScheduler,
47
+ "DPM++ 2M SDE Karras": DPMSolverMultistepScheduler,
48
+ }
49
+
50
+ sampler_name = scheduler # Example sampler name, replace with the actual value
51
+ scheduler_class = scheduler_classes.get(sampler_name)
52
+
53
+ if scheduler_class is not None:
54
+ print("sampler_name:",sampler_name)
55
+ pipe.scheduler = scheduler_class.from_config(pipe.scheduler.config)
56
+ else:
57
+ pass
58
+
59
+ return pipe
60
+
61
+
62
+ def img_args(
63
+ prompt,
64
+ negative_prompt,
65
+ init_img,
66
+ model_id_repo = "Realistic_Vision_V5_1_noVAE",
67
+ scheduler= "Euler a",
68
+ height=896,
69
+ width=896,
70
+ num_inference_steps = 30,
71
+ guidance_scale = 7.5,
72
+ num_images_per_prompt = 1,
73
+ seed = 0,
74
+ strength = 0.5,
75
+ ):
76
+
77
+ pipe = set_pipeline(model_id_repo,scheduler)
78
+
79
+ if seed == 0:
80
+ seed = random.randint(0,25647981548564)
81
+ print(f"random seed :{seed}")
82
+ generator = torch.manual_seed(seed)
83
+ else:
84
+ generator = torch.manual_seed(seed)
85
+ print(f"manual seed :{seed}")
86
+
87
+ init_img = init_img.resize((width,height))
88
+ print(init_img.size)
89
+ image = pipe(
90
+ image=init_img,
91
+ prompt=prompt,
92
+ negative_prompt = negative_prompt,
93
+ height = height,
94
+ width = width,
95
+ num_inference_steps = num_inference_steps,
96
+ guidance_scale = guidance_scale,
97
+ num_images_per_prompt = num_images_per_prompt, # default 1
98
+ generator = generator,
99
+ strength=strength
100
+ ).images
101
+ return image
102
+
103
+
104
+ block = gr.Blocks().queue()
105
+ block.title = "Inpaint Anything"
106
+ with block as image_gen:
107
+ with gr.Column():
108
+ with gr.Row():
109
+ gr.Markdown("## Image Generation")
110
+ with gr.Row():
111
+ with gr.Column():
112
+ # with gr.Row():
113
+ input_img = gr.Image(type="pil",label="Output")
114
+ prompt = gr.Textbox(placeholder="what you want to generate",label="Positive Prompt")
115
+ negative_prompt = gr.Textbox(placeholder="what you don't want to generate",label="Negative prompt")
116
+ run_btn = gr.Button("image generation", elem_id="select_btn", variant="primary")
117
+ with gr.Accordion(label="Advance Options",open=False):
118
+ model_selection = gr.Dropdown(choices=["dreamshaper","deliberate","runwayml","Realistic_Vision_V5_1_noVAE"],value="Realistic_Vision_V5_1_noVAE",label="Models")
119
+ schduler_selection = gr.Dropdown(choices=["DDIM","Euler","Euler a","UniPC","DPM2 Karras","DPM2 a Karras","PNDM","DPM++ 2M Karras","DPM++ 2M SDE Karras"],value="Euler a",label="Scheduler")
120
+ strength_slider = gr.Slider(label="strength", minimum=0, maximum=1, value=0.8, step=0.05)
121
+ guidance_scale_slider = gr.Slider(label="guidance_scale", minimum=0, maximum=15, value=7.5, step=0.5)
122
+ num_images_per_prompt_slider = gr.Slider(label="num_images_per_prompt", minimum=0, maximum=5, value=1, step=1)
123
+ width_slider = gr.Slider(label="width", minimum=0, maximum=2048, value=896, step=1)
124
+ height_slider = gr.Slider(label="height", minimum=0, maximum=2048, value=896, step=1)
125
+ num_inference_steps_slider = gr.Slider(label="num_inference_steps", minimum=0, maximum=150, value=30, step=1)
126
+ seed_slider = gr.Slider(label="Seed Slider", minimum=0, maximum=256479815, value=0, step=1)
127
+ with gr.Column():
128
+ out_img = gr.Gallery(label='Output', show_label=False, elem_id="gallery", preview=True)
129
+
130
+
131
+ run_btn.click(fn=img_args,inputs=[prompt,negative_prompt,input_img,model_selection,schduler_selection,height_slider,width_slider,num_inference_steps_slider,guidance_scale_slider,num_images_per_prompt_slider,seed_slider,strength_slider],outputs=[out_img])
132
+ image_gen.launch()