Prgckwb commited on
Commit
83bb2ae
1 Parent(s): e6271d2

:tada: init

Browse files
Files changed (3) hide show
  1. .gitignore +8 -0
  2. app.py +81 -0
  3. requirements.txt +4 -0
.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ ### Example user template template
2
+ ### Example user template
3
+
4
+ # IntelliJ project files
5
+ .idea
6
+ *.iml
7
+ out
8
+ gen
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import gradio as gr
4
+ import torch
5
+ from PIL import Image
6
+ from diffusers.image_processor import VaeImageProcessor
7
+ from diffusers.schedulers import (
8
+ DDPMScheduler,
9
+ DDIMScheduler,
10
+ EulerAncestralDiscreteScheduler,
11
+ DPMSolverMultistepScheduler,
12
+ FlowMatchEulerDiscreteScheduler,
13
+ )
14
+ from diffusers.utils.torch_utils import randn_tensor
15
+
16
+ SCHEDULERS = {
17
+ "DDPMScheduler": DDPMScheduler,
18
+ "DDIMScheduler": DDIMScheduler,
19
+ "EulerAncestralDiscreteScheduler": EulerAncestralDiscreteScheduler,
20
+ "DPMSolverMultistepScheduler": DPMSolverMultistepScheduler,
21
+ "FlowMatchEulerDiscreteScheduler": FlowMatchEulerDiscreteScheduler,
22
+ }
23
+
24
+
25
+ def inference(
26
+ image_pil: Image.Image,
27
+ scheduler_name: str,
28
+ per_step_time: int = 1,
29
+ n_total_steps: int = 1000,
30
+ ):
31
+ scheduler = SCHEDULERS[scheduler_name]()
32
+ scheduler.set_timesteps(num_inference_steps=n_total_steps)
33
+ timesteps = torch.flip(scheduler.timesteps, dims=[0])
34
+
35
+ image_processor = VaeImageProcessor()
36
+ image_tensor = image_processor.preprocess(image_pil)
37
+
38
+ # Fix seed
39
+ generator = torch.Generator().manual_seed(1117)
40
+ noise = randn_tensor(image_tensor.shape, generator)
41
+
42
+ for i, t in enumerate(timesteps):
43
+ noised_image_tensor = scheduler.add_noise(image_tensor, noise, timesteps=t)
44
+ noised_image_pil = image_processor.postprocess(noised_image_tensor)[0]
45
+ time.sleep(per_step_time)
46
+
47
+ # language=HTML
48
+ info_html = f"""
49
+ <div class="info-step">
50
+ <span class="step-number">Step {i + 1}</span> / {n_total_steps}
51
+ </div>
52
+ """
53
+
54
+ yield noised_image_pil, info_html
55
+
56
+
57
+ if __name__ == '__main__':
58
+ demo = gr.Interface(
59
+ title='Noisescope',
60
+ description='',
61
+ fn=inference,
62
+ inputs=[
63
+ gr.Image(type='pil', label='Input Image'),
64
+ gr.Dropdown(list(SCHEDULERS.keys()), value='DDPMScheduler', label='Scheduler'),
65
+ gr.Radio(choices=[0, 1, 2], value=0, label='Per-Step time'),
66
+ gr.Radio(choices=[10, 25, 50, 100, 1000], value=50, label='Total Steps'),
67
+ ],
68
+ outputs=[
69
+ gr.Image(type='pil', label='Noised Image'),
70
+ gr.HTML(label='Timestep Info'),
71
+ ],
72
+ # language=css
73
+ css="""
74
+ body { font-family: Arial, sans-serif; background-color: #f0f0f5; }
75
+ h1 { color: #3c3c3c; }
76
+ .gradio-container { max-width: 800px; margin: auto; padding: 20px; background: white; border-radius: 10px; box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1); }
77
+ .info-step { padding: 10px; background: #3c3c3c; color: white; border-radius: 5px; margin-bottom: 10px; }
78
+ .step-number { font-weight: bold; color: #FFD700; }
79
+ """
80
+ )
81
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ diffusers
2
+ transformers
3
+ accelerate
4
+ safetensors