Upload folder using huggingface_hub
Browse files- README.md +3 -9
- __pycache__/mini_sd.cpython-38.pyc +0 -0
- images/person1.jpg +0 -0
- images/person2.jpg +0 -0
- sd.py +79 -0
- server.py +63 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: red
|
5 |
-
colorTo: green
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: mtr-sd
|
3 |
+
app_file: server.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
+
sdk_version: 3.35.2
|
|
|
|
|
6 |
---
|
|
|
|
__pycache__/mini_sd.cpython-38.pyc
ADDED
Binary file (577 Bytes). View file
|
|
images/person1.jpg
ADDED
![]() |
images/person2.jpg
ADDED
![]() |
sd.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os, sys
|
3 |
+
|
4 |
+
import argparse
|
5 |
+
import copy
|
6 |
+
|
7 |
+
from IPython.display import display
|
8 |
+
from PIL import Image, ImageDraw, ImageFont
|
9 |
+
from torchvision.ops import box_convert
|
10 |
+
|
11 |
+
import supervision as sv
|
12 |
+
|
13 |
+
# segment anything
|
14 |
+
from segment_anything import build_sam, SamPredictor
|
15 |
+
import cv2
|
16 |
+
import numpy as np
|
17 |
+
import matplotlib.pyplot as plt
|
18 |
+
|
19 |
+
# diffusers
|
20 |
+
import PIL
|
21 |
+
import requests
|
22 |
+
import torch
|
23 |
+
from io import BytesIO
|
24 |
+
from diffusers import StableDiffusionInpaintPipeline
|
25 |
+
|
26 |
+
from huggingface_hub import hf_hub_download
|
27 |
+
|
28 |
+
# load models
|
29 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
30 |
+
|
31 |
+
# stable diffusion (inpainting)
|
32 |
+
sd_pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
33 |
+
"stabilityai/stable-diffusion-2-inpainting",
|
34 |
+
torch_dtype=torch.float16,
|
35 |
+
).to(device)
|
36 |
+
|
37 |
+
def generate_image(image, mask, prompt, negative_prompt, pipe, seed):
|
38 |
+
# resize for inpainting
|
39 |
+
w, h = image.size
|
40 |
+
in_image = image.resize((512, 512))
|
41 |
+
in_mask = mask.resize((512, 512))
|
42 |
+
|
43 |
+
generator = torch.Generator(device).manual_seed(seed)
|
44 |
+
|
45 |
+
result = pipe(image=in_image, mask_image=in_mask, prompt=prompt, negative_prompt=negative_prompt, generator=generator)
|
46 |
+
result = result.images[0]
|
47 |
+
|
48 |
+
return result.resize((w, h))
|
49 |
+
|
50 |
+
prompt="perfect skin"
|
51 |
+
negative_prompt=""
|
52 |
+
seed = 7 # for reproducibility
|
53 |
+
def predict(inputs):
|
54 |
+
# load image
|
55 |
+
image, mask = inputs["image"], inputs["mask"]
|
56 |
+
|
57 |
+
# convert
|
58 |
+
image_source_pil = Image.fromarray(image)
|
59 |
+
image_mask_pil = Image.fromarray(mask)
|
60 |
+
|
61 |
+
# inference
|
62 |
+
generated_image = generate_image(image=image_source_pil, mask=image_mask_pil, prompt=prompt, negative_prompt=negative_prompt, pipe=sd_pipe, seed=seed)
|
63 |
+
return generated_image
|
64 |
+
|
65 |
+
# gradio interface
|
66 |
+
demo = gr.Interface(fn=predict,
|
67 |
+
inputs=gr.Image(source="upload",
|
68 |
+
# interactive=True,
|
69 |
+
height=512,
|
70 |
+
tool="sketch",
|
71 |
+
type="numpy"),
|
72 |
+
outputs=gr.Image(),
|
73 |
+
title="Perfect Skin",
|
74 |
+
article="<p style='text-align: center'>Perfect Skin | Demo</p>",
|
75 |
+
allow_flagging="never",
|
76 |
+
)
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
demo.launch(server_name="0.0.0.0") #share=True
|
server.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os, sys
|
3 |
+
|
4 |
+
import argparse
|
5 |
+
import copy
|
6 |
+
|
7 |
+
from IPython.display import display
|
8 |
+
from PIL import Image, ImageDraw, ImageFont
|
9 |
+
from torchvision.ops import box_convert
|
10 |
+
|
11 |
+
import supervision as sv
|
12 |
+
|
13 |
+
# segment anything
|
14 |
+
from segment_anything import build_sam, SamPredictor
|
15 |
+
import cv2
|
16 |
+
import numpy as np
|
17 |
+
import matplotlib.pyplot as plt
|
18 |
+
|
19 |
+
# diffusers
|
20 |
+
import PIL
|
21 |
+
import requests
|
22 |
+
import torch
|
23 |
+
from io import BytesIO
|
24 |
+
from diffusers import StableDiffusionInpaintPipeline
|
25 |
+
|
26 |
+
from huggingface_hub import hf_hub_download
|
27 |
+
|
28 |
+
# load models
|
29 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
30 |
+
|
31 |
+
# stable diffusion (inpainting)
|
32 |
+
sd_pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
33 |
+
"stabilityai/stable-diffusion-2-inpainting",
|
34 |
+
torch_dtype=torch.float16,
|
35 |
+
).to(device)
|
36 |
+
|
37 |
+
def generate_image(image, mask, prompt, negative_prompt, pipe, seed):
|
38 |
+
# resize for inpainting
|
39 |
+
w, h = image.size
|
40 |
+
in_image = image.resize((512, 512))
|
41 |
+
in_mask = mask.resize((512, 512))
|
42 |
+
|
43 |
+
generator = torch.Generator(device).manual_seed(seed)
|
44 |
+
|
45 |
+
result = pipe(image=in_image, mask_image=in_mask, prompt=prompt, negative_prompt=negative_prompt, generator=generator)
|
46 |
+
result = result.images[0]
|
47 |
+
|
48 |
+
return result.resize((w, h))
|
49 |
+
|
50 |
+
prompt="perfect skin"
|
51 |
+
negative_prompt=""
|
52 |
+
seed = 7 # for reproducibility
|
53 |
+
def predict(image, mask):
|
54 |
+
# convert
|
55 |
+
image_source_pil = Image.fromarray(image)
|
56 |
+
image_mask_pil = Image.fromarray(mask)
|
57 |
+
|
58 |
+
# inference
|
59 |
+
generated_image = generate_image(image=image_source_pil, mask=image_mask_pil, prompt=prompt, negative_prompt=negative_prompt, pipe=sd_pipe, seed=seed)
|
60 |
+
return generated_image
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
io = gr.Interface(predict, ["image", "image"], "image").launch(server_name="0.0.0.0", share=True)
|