File size: 1,522 Bytes
89e9047
 
 
 
 
 
793566f
89e9047
 
 
 
 
 
 
 
 
 
da2fadf
89e9047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
import inspect
import warnings
import numpy as np
from typing import List, Optional, Union
import requests

from io import BytesIO
from PIL import Image
import torch
from torch import autocast
from tqdm.auto import tqdm
from diffusers import StableDiffusionImg2ImgPipeline

from huggingface_hub import notebook_login
notebook_login()


device = "cuda"
model_path = "CompVis/stable-diffusion-v1-4"
access_token = "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"

pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    model_path,
    revision="fp16", 
    torch_dtype=torch.float16,
    use_auth_token=access_token
)
pipe = pipe.to(device)

def predict(img, strength, seed, prompt):
  seed =  int(seed)
  img1 = np.asarray(img)
  img2 = Image.fromarray(img1)

  init_image = img2.resize((768, 512))

  generator = torch.Generator(device=device).manual_seed(seed)
  with autocast("cuda"):
    image = pipe(prompt=prompt, init_image=init_image, strength=strength, guidance_scale=5, generator=generator).images[0]

  return image

gr.Interface(
    predict,
    title = 'Image to Image using Diffusers',
    inputs=[
        gr.Image(),
        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)"),
        gr.Number(label = "seed (any number, generally 1024. But it's totally random. Change it and see different outputs)"),
        gr.Textbox(label="Prompt, empty by default")
    ],
    outputs = [
        gr.Image()
        ]
).launch()