Manjushri commited on
Commit
ace5fb6
·
1 Parent(s): d70ea12

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ from diffusers import StableDiffusionInstructPix2PixPipeline
6
+
7
+ model_id = "timbrooks/instruct-pix2pix"
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, revision="fp16") if torch.cuda.is_available() else StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id)
10
+ pipe = pipe.to(device)
11
+
12
+ def resize(value,img):
13
+ img = Image.open(img)
14
+ img = img.resize((value,value))
15
+ return img
16
+
17
+ def infer(source_img, instructions, guide, steps, seed, Strength):
18
+ generator = torch.Generator(device).manual_seed(seed)
19
+ source_image = resize(512, source_img)
20
+ source_image.save('source.png')
21
+ image = pipe(instructions, image=source_image,
22
+ guidance_scale=guide, image_guidance_scale=Strength,
23
+ num_inference_steps=steps, generator=generator,).images[0]
24
+ return image
25
+
26
+ gr.Interface(fn=infer, inputs=[gr.Image(source="upload", type="filepath", label="Raw Image. Must Be .png"),
27
+ gr.Textbox(label = 'Prompt Input Text. 77 Token (Keyword or Symbol) Maximum'),
28
+ gr.Slider(2, 15, value = 7, label = 'Guidance Scale'),
29
+ gr.Slider(1, 20, value = 5, step = 1, label = 'Number of Iterations'),
30
+ gr.Slider(label = "Seed", minimum = 0, maximum = 987654321987654321, step = 1, randomize = True),
31
+ gr.Slider(label='Strength', minimum = .1, maximum = 2, step = .05, value = .5)],
32
+ outputs='image',
33
+ description = "MUST Be .PNG and 512x512 or 768x768</b>) enter a Prompt, or let it just do its Thing, then click submit. 10 Iterations takes about ~900-1200 seconds currently. For more informationon about Stable Diffusion or Suggestions for prompts, keywords, artists or styles see https://github.com/Maks-s/sd-akashic",
34
+ article = "Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>").queue(max_size=5).launch(debug=True)