Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
#import torch
|
3 |
+
#from torch import autocast // only for GPU
|
4 |
+
|
5 |
+
from PIL import Image
|
6 |
+
import numpy as np
|
7 |
+
from io import BytesIO
|
8 |
+
import os
|
9 |
+
MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
|
10 |
+
|
11 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
12 |
+
|
13 |
+
print("hello sylvain")
|
14 |
+
|
15 |
+
YOUR_TOKEN=MY_SECRET_TOKEN
|
16 |
+
|
17 |
+
device="cpu"
|
18 |
+
|
19 |
+
#prompt_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
|
20 |
+
#prompt_pipe.to(device)
|
21 |
+
|
22 |
+
img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
|
23 |
+
img_pipe.to(device)
|
24 |
+
|
25 |
+
source_img = gr.Image(source="upload", type="filepath", label="init_img | 512*512 px")
|
26 |
+
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
|
27 |
+
|
28 |
+
def resize(value,img):
|
29 |
+
#baseheight = value
|
30 |
+
img = Image.open(img)
|
31 |
+
#hpercent = (baseheight/float(img.size[1]))
|
32 |
+
#wsize = int((float(img.size[0])*float(hpercent)))
|
33 |
+
#img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
|
34 |
+
img = img.resize((value,value), Image.Resampling.LANCZOS)
|
35 |
+
return img
|
36 |
+
|
37 |
+
|
38 |
+
def infer(prompt, source_img):
|
39 |
+
|
40 |
+
source_image = resize(512, source_img)
|
41 |
+
source_image.save('source.png')
|
42 |
+
images_list = img_pipe([prompt] * 2, init_image=source_image, strength=0.75)
|
43 |
+
images = []
|
44 |
+
safe_image = Image.open(r"unsafe.png")
|
45 |
+
for i, image in enumerate(images_list["sample"]):
|
46 |
+
if(images_list["nsfw_content_detected"][i]):
|
47 |
+
images.append(safe_image)
|
48 |
+
else:
|
49 |
+
images.append(image)
|
50 |
+
return images
|
51 |
+
|
52 |
+
print("Great sylvain ! Everything is working fine !")
|
53 |
+
|
54 |
+
title="Img2Img Stable Diffusion CPU"
|
55 |
+
description="Img2Img Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled.</b>"
|
56 |
+
|
57 |
+
gr.Interface(fn=infer, inputs=["text", source_img], outputs=gallery,title=title,description=description).queue(max_size=100).launch(enable_queue=True)
|
58 |
+
#from torch import autocast
|
59 |
+
#import requests
|
60 |
+
#import torch
|
61 |
+
#from PIL import Image
|
62 |
+
#from io import BytesIO
|
63 |
+
#import os
|
64 |
+
#MY_SECRET_TOKEN = os.environ.get('HF_TOKEN_SD')
|
65 |
+
|
66 |
+
#from diffusers import StableDiffusionImg2ImgPipeline
|
67 |
+
|
68 |
+
#YOUR_TOKEN = MY_SECRET_TOKEN
|
69 |
+
# load the pipeline
|
70 |
+
#device = "cuda"
|
71 |
+
#model_id_or_path = "CompVis/stable-diffusion-v1-4"
|
72 |
+
|
73 |
+
# pipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token = YOUR_TOKEN)
|
74 |
+
|
75 |
+
#pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
76 |
+
# model_id_or_path,
|
77 |
+
# revision="fp16",
|
78 |
+
# torch_dtype=torch.float16,
|
79 |
+
# use_auth_token=YOUR_TOKEN
|
80 |
+
#)
|
81 |
+
# or download via git clone https://huggingface.co/CompVis/stable-diffusion-v1-4
|
82 |
+
# and pass `model_id_or_path="./stable-diffusion-v1-4"` without having to use `use_auth_token=True`.
|
83 |
+
#pipe = pipe.to(device)
|
84 |
+
|
85 |
+
# let's download an initial image
|
86 |
+
#url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
|
87 |
+
|
88 |
+
#response = requests.get(url)
|
89 |
+
#init_image = Image.open(BytesIO(response.content)).convert("RGB")
|
90 |
+
#init_image = init_image.resize((768, 512))
|
91 |
+
|
92 |
+
#prompt = "Lively, illustration of a [[[<king::4>]]], portrait, fantasy, intricate, Scenic, hyperdetailed, hyper realistic <king-hearthstone>, unreal engine, 4k, smooth, sharp focus, intricate, cinematic lighting, highly detailed, octane, digital painting, artstation, concept art, vibrant colors, Cinema4D, WLOP, 3d render, in the style of hearthstone::5 art by Artgerm and greg rutkowski and magali villeneuve, martina jackova, Giger"
|
93 |
+
|
94 |
+
#with autocast("cuda"):
|
95 |
+
# images = pipe(prompt=prompt, init_image=init_image, strength=0.75, guidance_scale=7.5).images
|
96 |
+
|
97 |
+
#images[0].save("fantasy_landscape.png")
|