hansyan commited on
Commit
3aaaaf0
1 Parent(s): f993adf

Upload 4 files

Browse files
Files changed (4) hide show
  1. LICENSE +21 -0
  2. README.md +5 -8
  3. app.py +130 -0
  4. requirements.txt +16 -0
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Tripo AI & Stability AI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,13 +1,10 @@
1
  ---
2
- title: PeRFlow SDXL
3
- emoji: 😻
4
- colorFrom: green
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.27.0
 
8
  app_file: app.py
9
  pinned: false
10
- license: cc-by-nc-4.0
11
  ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: PeRFlow-SDXL
3
+ colorFrom: gray
4
+ colorTo: gray
 
5
  sdk: gradio
6
+ sdk_version: 4.20.1
7
+ python_version: 3.10.12
8
  app_file: app.py
9
  pinned: false
 
10
  ---
 
 
app.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import spaces
2
+ import random
3
+ import gradio as gr
4
+ import numpy as np
5
+ import torch
6
+ from PIL import Image
7
+
8
+ def setup_seed(seed):
9
+ random.seed(seed)
10
+ np.random.seed(seed)
11
+ torch.manual_seed(seed)
12
+ torch.cuda.manual_seed_all(seed)
13
+ torch.backends.cudnn.deterministic = True
14
+
15
+ if torch.cuda.is_available():
16
+ device = "cuda:0"
17
+ else:
18
+ device = "cpu"
19
+
20
+ ### PeRFlow-T2I
21
+ from diffusers import StableDiffusionXLPipeline
22
+ pipe = StableDiffusionXLPipeline.from_pretrained("hansyan/perflow-sdxl-dreamshaper", torch_dtype=torch.float16, use_safetensors=True, variant="v0-fix")
23
+ from src.scheduler_perflow import PeRFlowScheduler
24
+ pipe.scheduler = PeRFlowScheduler.from_config(pipe.scheduler.config, prediction_type="ddim_eps", num_time_windows=4)
25
+ pipe.to("cuda:0", torch.float16)
26
+ # pipe_t2i = None
27
+
28
+
29
+ ### gradio
30
+ # @spaces.GPU
31
+ def generate(text, num_inference_steps, cfg_scale, seed):
32
+ setup_seed(int(seed))
33
+ num_inference_steps = int(num_inference_steps)
34
+ cfg_scale = float(cfg_scale)
35
+
36
+ prompt_prefix = "photorealistic, uhd, high resolution, high quality, highly detailed; "
37
+ neg_prompt = "distorted, blur, low-quality, haze, out of focus"
38
+ text = prompt_prefix + text
39
+ samples = pipe(
40
+ prompt = [text],
41
+ negative_prompt = [neg_prompt],
42
+ height = 1024,
43
+ width = 1024,
44
+ num_inference_steps = num_inference_steps,
45
+ guidance_scale = cfg_scale,
46
+ output_type = 'pt',
47
+ ).images
48
+ samples = samples.squeeze(0).permute(1, 2, 0).cpu().numpy()*255.
49
+ samples = samples.astype(np.uint8)
50
+ samples = Image.fromarray(samples[:, :, :3])
51
+ return samples
52
+
53
+
54
+ # layout
55
+ css = """
56
+ h1 {
57
+ text-align: center;
58
+ display:block;
59
+ }
60
+ h2 {
61
+ text-align: center;
62
+ display:block;
63
+ }
64
+ h3 {
65
+ text-align: center;
66
+ display:block;
67
+ }
68
+ .gradio-container {
69
+ max-width: 768px !important;
70
+ }
71
+ """
72
+ with gr.Blocks(title="PeRFlow-SDXL", css=css) as interface:
73
+ gr.Markdown(
74
+ """
75
+ # PeRFlow-SDXL
76
+
77
+ GitHub: [https://github.com/magic-research/piecewise-rectified-flow](https://github.com/magic-research/piecewise-rectified-flow) <br/>
78
+ Models: [https://huggingface.co/hansyan/perflow-sdxl-dreamshaper](https://huggingface.co/hansyan/perflow-sdxl-dreamshaper)
79
+
80
+ <br/>
81
+ """
82
+ )
83
+
84
+ with gr.Column():
85
+ text = gr.Textbox(
86
+ label="Input Prompt",
87
+ value="masterpiece, A closeup face photo of girl, wearing a rain coat, in the street, heavy rain, bokeh"
88
+ )
89
+ with gr.Row():
90
+ num_inference_steps = gr.Dropdown(label='Num Inference Steps',choices=[4,5,6,7,8], value=6, interactive=True)
91
+ cfg_scale = gr.Dropdown(label='CFG scale',choices=[1.5, 2.0, 2.5], value=2.0, interactive=True)
92
+ seed = gr.Textbox(label="Random Seed", value=42)
93
+ submit = gr.Button(scale=1, variant='primary')
94
+
95
+ # with gr.Column():
96
+ # with gr.Row():
97
+ output_image = gr.Image(label='Generated Image')
98
+
99
+ gr.Markdown(
100
+ """
101
+ Here are some examples provided:
102
+ - “masterpiece, A closeup face photo of girl, wearing a rain coat, in the street, heavy rain, bokeh”
103
+ - “RAW photo, a handsome man, wearing a black coat, outside, closeup face”
104
+ - “RAW photo, a red luxury car, studio light”
105
+ - “masterpiece, A beautiful cat bask in the sun”
106
+ """
107
+ )
108
+
109
+ # activate
110
+ text.submit(
111
+ fn=generate,
112
+ inputs=[text, num_inference_steps, cfg_scale, seed],
113
+ outputs=[output_image],
114
+ )
115
+ seed.submit(
116
+ fn=generate,
117
+ inputs=[text, num_inference_steps, cfg_scale, seed],
118
+ outputs=[output_image],
119
+ )
120
+ submit.click(fn=generate,
121
+ inputs=[text, num_inference_steps, cfg_scale, seed],
122
+ outputs=[output_image],
123
+ )
124
+
125
+
126
+
127
+ if __name__ == '__main__':
128
+ interface.queue(max_size=10)
129
+ # interface.launch()
130
+ interface.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ diffusers==0.24.0
2
+ einops==0.7.0
3
+ gradio==4.20.1
4
+ huggingface_hub==0.21.4
5
+ imageio==2.27.0
6
+ numpy==1.24.3
7
+ omegaconf==2.3.0
8
+ packaging==23.2
9
+ Pillow==10.1.0
10
+ rembg==2.0.55
11
+ safetensors==0.3.2
12
+ torch==2.0.0
13
+ torchvision==0.15.1
14
+ tqdm==4.64.1
15
+ transformers==4.27.0
16
+ trimesh==4.0.5