Maaz66 commited on
Commit
92eeb58
·
1 Parent(s): 9cb90a8
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !pip install diffusers==0.3.0 transformers ftfy
2
+ !pip install -qq "ipywidgets>=7,<8"
3
+ ! pip install gradio
4
+ import gradio as gr
5
+ from huggingface_hub import notebook_login
6
+ import inspect
7
+ import warnings
8
+ from typing import List, Optional, Union
9
+
10
+ import torch
11
+ from torch import autocast
12
+ from tqdm.auto import tqdm
13
+
14
+ from diffusers import StableDiffusionImg2ImgPipeline
15
+
16
+
17
+ device = "cuda"
18
+ model_path = "CompVis/stable-diffusion-v1-4"
19
+
20
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
21
+ model_path,
22
+ revision="fp16",
23
+ torch_dtype=torch.float16,
24
+ use_auth_token=True
25
+ )
26
+ pipe = pipe.to(device)
27
+
28
+ def predict(image_url, strength, seed):
29
+ seed= int(seed)
30
+
31
+ response = requests.get(image_url)
32
+ init_img = Image.open(BytesIO(response.content)).convert("RGB")
33
+ init_img = init_img.resize((768, 512))
34
+
35
+
36
+ generator = torch.Generator(device=device).manual_seed(seed)
37
+ with autocast("cuda"):
38
+ image = pipe(prompt="", init_image=init_img, strength=strength, guidance_scale=5, generator=generator).images[0]
39
+
40
+ return image
41
+
42
+
43
+
44
+ gr.Interface(
45
+ predict,
46
+ title = 'Image to Image using Diffusers',
47
+ inputs=[
48
+ gr.Textbox(label="image_url"),
49
+
50
+ gr.Slider(0, 1, value=0.05, label ="strength"),
51
+ gr.Number(label = "seed")
52
+
53
+ ],
54
+ outputs = [
55
+ gr.Image()
56
+ ]
57
+ ).launch()
58
+