JavierGon12 commited on
Commit
757b45e
1 Parent(s): 18f9a4f

Generate images code

Browse files
Files changed (2) hide show
  1. app.py +33 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,4 +1,35 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- x = st.slider("Select a value")
4
- st.write('Test number:', x)
 
1
  import streamlit as st
2
+ from diffusers import DDPMScheduler, UNet2DModel
3
+ from PIL import Image
4
+ import torch
5
+ import numpy as np
6
+
7
+ def generate_image():
8
+ scheduler = DDPMScheduler.from_pretrained("google/ddpm-cat-256")
9
+ model = UNet2DModel.from_pretrained("google/ddpm-cat-256").to("cuda")
10
+ scheduler.set_timesteps(50)
11
+
12
+ sample_size = model.config.sample_size
13
+ noise = torch.randn((1, 3, sample_size, sample_size)).to("cuda")
14
+ input = noise
15
+
16
+ for t in scheduler.timesteps:
17
+ with torch.no_grad():
18
+ noisy_residual = model(input, t).sample()
19
+ prev_noisy_sample = scheduler.step(noisy_residual, t, input).prev_sample
20
+ input = prev_noisy_sample
21
+
22
+ image = (input / 2 + 0.5).clamp(0, 1)
23
+ image = image.cpu().permute(0, 2, 3, 1).numpy()[0]
24
+ image = Image.fromarray((image * 255).round().astype("uint8"))
25
+ return image
26
+
27
+ # Streamlit app
28
+ st.title("DDPM Image Generation")
29
+ st.write("Generating and displaying an image using DDPM.")
30
+
31
+ # Generate and display the image
32
+ generated_image = generate_image()
33
+ st.image(generated_image, caption="Generated Image", use_column_width=True)
34
+
35
 
 
 
requirements.txt CHANGED
@@ -2,4 +2,5 @@ transformers
2
  streamlit
3
  diffusers
4
  pandas
5
- plotly
 
 
2
  streamlit
3
  diffusers
4
  pandas
5
+ plotly
6
+ torch