Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
import torch
|
5 |
+
import os
|
6 |
+
from diffusers import DiffusionPipeline, DDIMScheduler
|
7 |
+
MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
|
8 |
+
has_cuda = torch.cuda.is_available()
|
9 |
+
device = torch.device('cpu' if not has_cuda else 'cuda')
|
10 |
+
|
11 |
+
pipe = DiffusionPipeline.from_pretrained(
|
12 |
+
"CompVis/stable-diffusion-v1-4",
|
13 |
+
safety_checker=None,
|
14 |
+
use_auth_token=MY_SECRET_TOKEN,
|
15 |
+
custom_pipeline="imagic_stable_diffusion",
|
16 |
+
scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=False, set_alpha_to_one=False)
|
17 |
+
).to(device)
|
18 |
+
|
19 |
+
generator = th.Generator("cuda").manual_seed(0)
|
20 |
+
|
21 |
+
def infer(prompt, init_image):
|
22 |
+
res = pipe.train(
|
23 |
+
prompt,
|
24 |
+
init_image,
|
25 |
+
guidance_scale=7.5,
|
26 |
+
num_inference_steps=50,
|
27 |
+
generator=generator)
|
28 |
+
|
29 |
+
res = pipe(alpha=1)
|
30 |
+
|
31 |
+
return res.images[0]
|
32 |
+
|
33 |
+
|
34 |
+
prompt_input = gr.Textbox()
|
35 |
+
image_init = gr.Image(source="upload", type="filepath")
|
36 |
+
image_output = gr.Image()
|
37 |
+
|
38 |
+
demo = gr.Interface(fn=infer, inputs=[prompt_input, image_init], outputs=image_output)
|
39 |
+
demo.launch()
|