Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
access_token = "TOKEN"
|
15 |
+
# Go to hugging face, your profile "SETTINGS" options, click on "Access tokens", and then generate ur token from there. Paste it over the top
|
16 |
+
|
17 |
+
# load the pipeline
|
18 |
+
device = "cuda"
|
19 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
20 |
+
"CompVis/stable-diffusion-v1-4",
|
21 |
+
revision="fp16",
|
22 |
+
torch_dtype=torch.float16,
|
23 |
+
use_auth_token=access_token
|
24 |
+
).to(device)
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
def generate(img, strength, seed, prompt):
|
32 |
+
# Convert the seed to an integer
|
33 |
+
seed = int(seed)
|
34 |
+
|
35 |
+
img1 = np.asarray(img)
|
36 |
+
img2 = Image.fromarray(img1)
|
37 |
+
|
38 |
+
# Check that the input image is a valid image
|
39 |
+
if not isinstance(img2, Image.Image):
|
40 |
+
raise ValueError("Invalid input image")
|
41 |
+
|
42 |
+
# Resize the image
|
43 |
+
init_image = img2.resize((768, 512))
|
44 |
+
|
45 |
+
# Create a list to store the 4 output images
|
46 |
+
images = []
|
47 |
+
|
48 |
+
# Use the GPU if available, otherwise use the CPU
|
49 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
50 |
+
with autocast(device):
|
51 |
+
# Generate the 4 output images using the pipe function
|
52 |
+
for i in range(2):
|
53 |
+
# Initialize the generator with a random seed
|
54 |
+
generator = torch.Generator(device=device).manual_seed(seed*i)
|
55 |
+
|
56 |
+
# Call the pipe function and store the output image
|
57 |
+
output_image = pipe(prompt=prompt, init_image=init_image, strength=strength, guidance_scale=7.5, generator=generator, batch_size=128).images[0]
|
58 |
+
|
59 |
+
# Check that the output image is a valid image
|
60 |
+
if not isinstance(output_image, Image.Image):
|
61 |
+
raise ValueError("Invalid output image")
|
62 |
+
|
63 |
+
images.append(output_image)
|
64 |
+
|
65 |
+
return [images[0], images[1]]
|
66 |
+
|
67 |
+
|
68 |
+
gr.Interface(
|
69 |
+
|
70 |
+
generate,
|
71 |
+
title = 'Image to Image using Diffusers',
|
72 |
+
inputs=[
|
73 |
+
gr.Image(elem_id = "input-image"),
|
74 |
+
gr.Slider(0, 1, value=0.05, label ="Strength (keep close to 0 for minimal changes)"),
|
75 |
+
gr.Slider(50, 700, value=75, label ="Seed"),
|
76 |
+
gr.Textbox(label="Prompt (leave blank if you want minimal changes)"),
|
77 |
+
],
|
78 |
+
outputs = [
|
79 |
+
gr.Image(elem_id="output-image"),
|
80 |
+
gr.Image(elem_id="output-image"),
|
81 |
+
|
82 |
+
], css = "#output-image, #input-image, #image-preview {border-radius: 40px !important; background-color : gray !important;} "
|
83 |
+
).launch(share=True, debug=True)
|