saicharan1234 commited on
Commit
6145eb5
1 Parent(s): 12d6a3e

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +186 -0
main.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Form
2
+ from fastapi.responses import StreamingResponse
3
+ import torch
4
+ from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler, DPMSolverSinglestepScheduler
5
+ from diffusers.pipelines import StableDiffusionInpaintPipeline, StableDiffusionXLInpaintPipeline
6
+ from huggingface_hub import hf_hub_download
7
+ import numpy as np
8
+ import random
9
+ from PIL import Image
10
+ import io
11
+
12
+ app = FastAPI()
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+
16
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
17
+
18
+ # Load pipelines
19
+ HF_TOKEN = "your_huggingface_token" # Replace with your actual token
20
+ pipe_xl_final = StableDiffusionXLPipeline.from_single_file(
21
+ hf_hub_download(repo_id="fluently/Fluently-XL-Final", filename="FluentlyXL-Final.safetensors", token=HF_TOKEN),
22
+ torch_dtype=torch.float16,
23
+ use_safetensors=True,
24
+ )
25
+ pipe_xl_final.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_xl_final.scheduler.config)
26
+ pipe_xl_final.to(device)
27
+
28
+ pipe_anime = StableDiffusionPipeline.from_pretrained(
29
+ "fluently/Fluently-anime",
30
+ torch_dtype=torch.float16,
31
+ use_safetensors=True,
32
+ )
33
+ pipe_anime.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_anime.scheduler.config)
34
+ pipe_anime.to(device)
35
+
36
+ pipe_epic = StableDiffusionPipeline.from_pretrained(
37
+ "fluently/Fluently-epic",
38
+ torch_dtype=torch.float16,
39
+ use_safetensors=True,
40
+ )
41
+ pipe_epic.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_epic.scheduler.config)
42
+ pipe_epic.to(device)
43
+
44
+ pipe_xl_inpaint = StableDiffusionXLInpaintPipeline.from_single_file(
45
+ "https://huggingface.co/fluently/Fluently-XL-v3-inpainting/blob/main/FluentlyXL-v3-inpainting.safetensors",
46
+ torch_dtype=torch.float16,
47
+ use_safetensors=True,
48
+ )
49
+ pipe_xl_inpaint.to(device)
50
+
51
+ pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(
52
+ "fluently/Fluently-v4-inpainting",
53
+ torch_dtype=torch.float16,
54
+ use_safetensors=True,
55
+ )
56
+ pipe_inpaint.to(device)
57
+
58
+ pipe_xl = StableDiffusionXLPipeline.from_pretrained(
59
+ "fluently/Fluently-XL-v4",
60
+ torch_dtype=torch.float16,
61
+ use_safetensors=True,
62
+ )
63
+ pipe_xl.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe_xl.scheduler.config)
64
+ pipe_xl.to(device)
65
+
66
+ pipe_xl_lightning = StableDiffusionXLPipeline.from_pretrained(
67
+ "fluently/Fluently-XL-v3-lightning",
68
+ torch_dtype=torch.float16,
69
+ use_safetensors=True,
70
+ )
71
+ pipe_xl_lightning.scheduler = DPMSolverSinglestepScheduler.from_config(pipe_xl_lightning.scheduler.config, use_karras_sigmas=False, timestep_spacing="trailing", lower_order_final=True)
72
+ pipe_xl_lightning.to(device)
73
+
74
+
75
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
76
+ if randomize_seed:
77
+ seed = random.randint(0, MAX_SEED)
78
+ return seed
79
+
80
+
81
+ @app.post("/generate")
82
+ async def generate(
83
+ model: str = Form(...),
84
+ prompt: str = Form(...),
85
+ negative_prompt: str = Form(""),
86
+ use_negative_prompt: bool = Form(False),
87
+ seed: int = Form(0),
88
+ width: int = Form(1024),
89
+ height: int = Form(1024),
90
+ guidance_scale: float = Form(3),
91
+ randomize_seed: bool = Form(False),
92
+ inpaint_image: UploadFile = File(None),
93
+ mask_image: UploadFile = File(None),
94
+ blur_factor: float = Form(1.0),
95
+ strength: float = Form(0.75)
96
+ ):
97
+ seed = int(randomize_seed_fn(seed, randomize_seed))
98
+
99
+ if not use_negative_prompt:
100
+ negative_prompt = ""
101
+
102
+ inpaint_image_pil = Image.open(io.BytesIO(await inpaint_image.read())) if inpaint_image else None
103
+ mask_image_pil = Image.open(io.BytesIO(await mask_image.read())) if mask_image else None
104
+
105
+ if model == "Fluently XL Final":
106
+ images = pipe_xl_final(
107
+ prompt=prompt,
108
+ negative_prompt=negative_prompt,
109
+ width=width,
110
+ height=height,
111
+ guidance_scale=guidance_scale,
112
+ num_inference_steps=25,
113
+ num_images_per_prompt=1,
114
+ output_type="pil",
115
+ ).images
116
+ elif model == "Fluently Anime":
117
+ images = pipe_anime(
118
+ prompt=prompt,
119
+ negative_prompt=negative_prompt,
120
+ width=width,
121
+ height=height,
122
+ guidance_scale=guidance_scale,
123
+ num_inference_steps=30,
124
+ num_images_per_prompt=1,
125
+ output_type="pil",
126
+ ).images
127
+ elif model == "Fluently Epic":
128
+ images = pipe_epic(
129
+ prompt=prompt,
130
+ negative_prompt=negative_prompt,
131
+ width=width,
132
+ height=height,
133
+ guidance_scale=guidance_scale,
134
+ num_inference_steps=30,
135
+ num_images_per_prompt=1,
136
+ output_type="pil",
137
+ ).images
138
+ elif model == "Fluently XL v4":
139
+ images = pipe_xl(
140
+ prompt=prompt,
141
+ negative_prompt=negative_prompt,
142
+ width=width,
143
+ height=height,
144
+ guidance_scale=guidance_scale,
145
+ num_inference_steps=25,
146
+ num_images_per_prompt=1,
147
+ output_type="pil",
148
+ ).images
149
+ elif model == "Fluently XL v3 Lightning":
150
+ images = pipe_xl_lightning(
151
+ prompt=prompt,
152
+ negative_prompt=negative_prompt,
153
+ width=width,
154
+ height=height,
155
+ guidance_scale=2,
156
+ num_inference_steps=5,
157
+ num_images_per_prompt=1,
158
+ output_type="pil",
159
+ ).images
160
+ elif model == "Fluently v4 inpaint" or model == "Fluently XL v3 inpaint":
161
+ blurred_mask = pipe_inpaint.mask_processor.blur(mask_image_pil, blur_factor=blur_factor)
162
+ images = pipe_inpaint(
163
+ prompt=prompt,
164
+ image=inpaint_image_pil,
165
+ mask_image=blurred_mask,
166
+ negative_prompt=negative_prompt,
167
+ width=width,
168
+ height=height,
169
+ guidance_scale=guidance_scale,
170
+ num_inference_steps=30,
171
+ strength=strength,
172
+ num_images_per_prompt=1,
173
+ output_type="pil",
174
+ ).images
175
+
176
+ img = images[0]
177
+ img_byte_arr = io.BytesIO()
178
+ img.save(img_byte_arr, format='PNG')
179
+ img_byte_arr.seek(0)
180
+
181
+ return StreamingResponse(img_byte_arr, media_type="image/png")
182
+
183
+
184
+ if __name__ == "__main__":
185
+ import uvicorn
186
+ uvicorn.run(app, host="0.0.0.0", port=7860)