rikdas commited on
Commit
5b49f7f
1 Parent(s): 996c5b5

running space

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -5,34 +5,50 @@ from PIL import Image
5
  from io import BytesIO
6
  from diffusers import StableDiffusionImg2ImgPipeline
7
 
8
- device = "cpu"
9
- #model_path = "weights"
10
- #model_id_or_path = "runwayml/stable-diffusion-v1-5"
11
 
12
- model_id = "pwc-india/tartan_weights"
13
- pipe = StableDiffusionPipeline.from_pretrained(model_id).to(device)
14
-
15
- pipe2 = StableDiffusionImg2ImgPipeline.from_pretrained(model_id).to(device)
16
 
17
  import gradio as gr
18
 
19
- def generate_txt2img(prompt):
20
- return pipe(prompt, num_inference_steps=25, guidance_scale=7.5).images[0]
21
- def generate_img2img(img, prompt):
22
- image = Image.fromarray(img)
23
- return pipe2(prompt=prompt, image=image, strength=0.75, guidance_scale=7.5).images[0]
 
24
 
25
  with gr.Blocks() as demo:
26
  with gr.Tab("Text2Image"):
27
- inp_txt = gr.Text(showlabel=False, placeholder="Enter your prompt here...")
28
- btn = gr.Button("Generate")
29
- out_img = gr.Image()
30
- btn.click(fn=generate_txt2img, inputs=[inp_txt], outputs=[out_img])
 
 
 
 
 
 
 
 
 
 
 
31
  with gr.Tab("Image2Image"):
32
- inp_img = gr.Image()
33
- inp_txt2 = gr.Text(showlabel=False,placeholder="Enter your prompt here...")
34
- btn2 = gr.Button("Generate")
35
- out_img2 = gr.Image()
36
- btn2.click(fn=generate_img2img, inputs=[inp_img, inp_txt2], outputs=[out_img2])
37
-
38
- demo.launch(debug=True)
 
 
 
 
 
 
 
 
5
  from io import BytesIO
6
  from diffusers import StableDiffusionImg2ImgPipeline
7
 
8
+ device = "cuda"
9
+ model_id = "krishi/tartan2"
10
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to(device)
11
 
12
+ pipe2 = StableDiffusionImg2ImgPipeline(**pipe.components).to(device)
 
 
 
13
 
14
  import gradio as gr
15
 
16
+ def generate_txt2img(inp_txt, inp_neg, num_inf_steps, width, height, g_scale, num_imgs):
17
+ return pipe(prompt=inp_txt, negative_prompt=inp_neg, num_inference_steps=num_inf_steps, width=width, height=height, guidance_scale=g_scale, num_images_per_prompt=num_imgs).images
18
+ def generate_img2img(inp_img, inp_txt, inp_neg, num_inf_steps, g_scale, num_imgs, strength):
19
+ image = Image.fromarray(inp_img)
20
+ image = image.resize((512, 512))
21
+ return pipe2(prompt=inp_txt, negative_prompt=inp_neg, num_inference_steps=num_inf_steps, image=image, strength=strength, guidance_scale=g_scale, num_images_per_prompt=num_imgs).images
22
 
23
  with gr.Blocks() as demo:
24
  with gr.Tab("Text2Image"):
25
+ with gr.Group():
26
+ inp_txt = gr.Text(show_label=False, placeholder="Enter your prompt here...")
27
+ inp_neg = gr.Text(show_label=False, placeholder="Enter your negative prompt here...")
28
+ with gr.Accordion("Extra parameters", open=False):
29
+ num_inf_steps = gr.Slider(label="Number of inference steps", minimum=20, maximum=100, value=50, step=1)
30
+ with gr.Row():
31
+ with gr.Column():
32
+ width = gr.Slider(label="Width(pixels)", minimum=256, maximum=1024, value=512, step=1)
33
+ with gr.Column():
34
+ height = gr.Slider(label="Height(pixels)", minimum=256, maximum=1024, value=512, step=1)
35
+ g_scale = gr.Slider(label="Guidance scale", minimum=1, maximum=10, value=7.5, step=0.5)
36
+ num_imgs = gr.Slider(label="Number of images", minimum=1, maximum=10, value=1, step=1)
37
+ btn = gr.Button("Generate")
38
+ out_img = gr.Gallery(preview=True)
39
+ btn.click(fn=generate_txt2img, inputs=[inp_txt, inp_neg, num_inf_steps, width, height, g_scale, num_imgs], outputs=[out_img])
40
  with gr.Tab("Image2Image"):
41
+ with gr.Group():
42
+ inp_img = gr.Image()
43
+ inp_txt2 = gr.Text(show_label=False, placeholder="Enter your prompt here...")
44
+ inp_neg2 = gr.Text(show_label=False, placeholder="Enter your negative prompt here...")
45
+ with gr.Accordion("Extra parameters", open=False):
46
+ num_inf_steps2 = gr.Slider(label="Number of inference steps", minimum=20, maximum=100, value=50, step=1)
47
+ g_scale2 = gr.Slider(label="Guidance scale", minimum=1, maximum=10, value=7.5, step=0.5)
48
+ num_imgs2 = gr.Slider(label="Number of images", minimum=1, maximum=10, value=1, step=1)
49
+ strength = gr.Slider(label="Strength", minimum=0, maximum=1, value=0.8, step=0.1)
50
+ btn2 = gr.Button("Generate")
51
+ out_img2 = gr.Gallery(preview=True)
52
+ btn2.click(fn=generate_img2img, inputs=[inp_img, inp_txt2, inp_neg2, num_inf_steps2, g_scale2, num_imgs2, strength], outputs=[out_img2])
53
+
54
+ demo.launch(debug=True)