Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tqdm.auto import trange
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from source import Model, Block
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def cvtImg(img):
|
| 10 |
+
img = img - img.min()
|
| 11 |
+
img = (img / img.max())
|
| 12 |
+
return img.numpy().astype(np.float32)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def load_DM():
|
| 16 |
+
PATH = "./Diffusion-cuda"
|
| 17 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 18 |
+
st.write("Devise used:", device)
|
| 19 |
+
model = Model().to(device)
|
| 20 |
+
st.write("Diffusion Model's architecture created.")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Load the entire model object
|
| 24 |
+
model = torch.load(PATH, map_location=device)
|
| 25 |
+
st.write("Diffusion Model loaded.")
|
| 26 |
+
except FileNotFoundError:
|
| 27 |
+
st.error("Model file not found at specified path.")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"Error occurred while loading model: {e}")
|
| 30 |
+
|
| 31 |
+
return model, device
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def predict_step_new(model, device):
|
| 35 |
+
xs = []
|
| 36 |
+
IMG_SIZE = 32
|
| 37 |
+
timesteps = 16
|
| 38 |
+
|
| 39 |
+
progress_text = "Denoising the image."
|
| 40 |
+
my_bar = st.progress(0, text=progress_text)
|
| 41 |
+
x = torch.randn(size=(1, 3, IMG_SIZE, IMG_SIZE), device=device)
|
| 42 |
+
placeholder = st.empty()
|
| 43 |
+
li = []
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
for i in trange(timesteps):
|
| 46 |
+
my_bar.progress(i*100//15, text=progress_text)
|
| 47 |
+
t = i
|
| 48 |
+
x = model(x, torch.full([8, 1], t, dtype=torch.float, device=device))
|
| 49 |
+
xs = x[0].permute(1, 2, 0)
|
| 50 |
+
xs = torch.clip(xs, -1, 1)
|
| 51 |
+
xs = cvtImg(xs.cpu())
|
| 52 |
+
images = np.transpose(xs, (2, 0, 1))
|
| 53 |
+
images = np.transpose(images, (1, 2, 0))
|
| 54 |
+
placeholder.image(images, caption=f"Time step {t}", width=500)
|
| 55 |
+
li.append(images)
|
| 56 |
+
st.markdown("Brand new **car** generated!")
|
| 57 |
+
return li
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def main():
|
| 61 |
+
st.set_page_config(page_title="Diffusion Model App Demo")
|
| 62 |
+
st.header("Diffusion Model App 🖼️ 🚕", divider="rainbow")
|
| 63 |
+
st.markdown("""Made by:
|
| 64 |
+
Marijan$^\dag$, Thomas, Mauritz, Amanda and Baris for **COGITO** Diffusion Model""")
|
| 65 |
+
|
| 66 |
+
if "model" not in st.session_state:
|
| 67 |
+
st.session_state.model = []
|
| 68 |
+
|
| 69 |
+
if "generation" not in st.session_state:
|
| 70 |
+
st.session_state.generation = []
|
| 71 |
+
|
| 72 |
+
if "device" not in st.session_state:
|
| 73 |
+
st.session_state.device = None
|
| 74 |
+
|
| 75 |
+
placeholder_1 = st.empty()
|
| 76 |
+
isclick = placeholder_1.button('Import Diffusion Model')
|
| 77 |
+
if isclick:
|
| 78 |
+
st.session_state.model, st.session_state.device = load_DM()
|
| 79 |
+
placeholder_1.empty()
|
| 80 |
+
|
| 81 |
+
if st.button('Generate!'):
|
| 82 |
+
li = predict_step_new(st.session_state.model, st.session_state.device)
|
| 83 |
+
st.session_state.generation.append(li)
|
| 84 |
+
for num, gen in enumerate(st.session_state.generation):
|
| 85 |
+
st.write("Generation number", num+1)
|
| 86 |
+
st.image(gen, width=40)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
main()
|