Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import inspect
|
3 |
+
import warnings
|
4 |
+
import numpy as np
|
5 |
+
from typing import List, Optional, Union
|
6 |
+
import requests
|
7 |
+
from io import BytesIO
|
8 |
+
from PIL import Image
|
9 |
+
import torch
|
10 |
+
from torch import autocast
|
11 |
+
from tqdm.auto import tqdm
|
12 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
13 |
+
|
14 |
+
from huggingface_hub import notebook_login
|
15 |
+
notebook_login()
|
16 |
+
|
17 |
+
device = "cuda"
|
18 |
+
model_path = "CompVis/stable-diffusion-v1-4"
|
19 |
+
access_token = "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"
|
20 |
+
|
21 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
22 |
+
model_path,
|
23 |
+
revision="fp16",
|
24 |
+
torch_dtype=torch.float16,
|
25 |
+
use_auth_token=access_token
|
26 |
+
)
|
27 |
+
pipe = pipe.to(device)
|
28 |
+
|
29 |
+
def predict(img, strength, seed, prompt):
|
30 |
+
seed = int(seed)
|
31 |
+
img1 = np.asarray(img)
|
32 |
+
img2 = Image.fromarray(img1)
|
33 |
+
|
34 |
+
init_image = img2.resize((768, 512))
|
35 |
+
|
36 |
+
|
37 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
38 |
+
with autocast("cuda"):
|
39 |
+
image = pipe(prompt=prompt, init_image=init_image, strength=strength, guidance_scale=5, generator=generator).images[0]
|
40 |
+
|
41 |
+
return image
|
42 |
+
|
43 |
+
gr.Interface(
|
44 |
+
predict,
|
45 |
+
title = 'Image to Image using Diffusers',
|
46 |
+
inputs=[
|
47 |
+
gr.Image(),
|
48 |
+
gr.Slider(0, 1, value=0.05, label ="strength (keep it close to 0 to make minimal changes to image (such as 0.1, 0.2, 0.3)"),
|
49 |
+
gr.Number(label = "seed (any number, generally 1024. But it's totally random. Change it and see different outputs)"),
|
50 |
+
gr.Textbox(label="Prompt, empty by default")
|
51 |
+
],
|
52 |
+
outputs = [
|
53 |
+
gr.Image()
|
54 |
+
]
|
55 |
+
).launch()
|