Spaces:
Runtime error
Runtime error
Afrinetwork7
commited on
Commit
•
4559e89
1
Parent(s):
feda0ab
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
-
import
|
|
|
2 |
import numpy as np
|
3 |
import random
|
4 |
import torch
|
5 |
from diffusers import DiffusionPipeline
|
6 |
-
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
7 |
import boto3
|
8 |
from io import BytesIO
|
9 |
import time
|
@@ -27,6 +27,17 @@ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_d
|
|
27 |
MAX_SEED = np.iinfo(np.int32).max
|
28 |
MAX_IMAGE_SIZE = 2048
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def save_image_to_s3(image):
|
31 |
img_byte_arr = BytesIO()
|
32 |
image.save(img_byte_arr, format='PNG')
|
@@ -42,107 +53,31 @@ def save_image_to_s3(image):
|
|
42 |
url = f"https://{S3_BUCKET}.s3.{S3_REGION}.amazonaws.com/{filename}"
|
43 |
return url
|
44 |
|
45 |
-
|
46 |
-
|
|
|
47 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
|
|
|
48 |
generator = torch.Generator().manual_seed(seed)
|
49 |
-
image = pipe(
|
50 |
-
prompt=prompt,
|
51 |
-
width=width,
|
52 |
-
height=height,
|
53 |
-
num_inference_steps=num_inference_steps,
|
54 |
-
generator=generator,
|
55 |
-
guidance_scale=guidance_scale
|
56 |
-
).images[0]
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
]
|
67 |
-
|
68 |
-
css="""
|
69 |
-
#col-container {
|
70 |
-
margin: 0 auto;
|
71 |
-
max-width: 520px;
|
72 |
-
}
|
73 |
-
"""
|
74 |
-
|
75 |
-
with gr.Blocks(css=css) as demo:
|
76 |
-
with gr.Column(elem_id="col-container"):
|
77 |
-
gr.Markdown(f"""# FLUX.1 [dev]
|
78 |
-
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
79 |
-
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
80 |
-
""")
|
81 |
-
|
82 |
-
with gr.Row():
|
83 |
-
prompt = gr.Text(
|
84 |
-
label="Prompt",
|
85 |
-
show_label=False,
|
86 |
-
max_lines=1,
|
87 |
-
placeholder="Enter your prompt",
|
88 |
-
container=False,
|
89 |
-
)
|
90 |
-
run_button = gr.Button("Run", scale=0)
|
91 |
-
|
92 |
-
result = gr.Text(label="Image URL", show_label=True)
|
93 |
|
94 |
-
|
95 |
-
seed = gr.Slider(
|
96 |
-
label="Seed",
|
97 |
-
minimum=0,
|
98 |
-
maximum=MAX_SEED,
|
99 |
-
step=1,
|
100 |
-
value=0,
|
101 |
-
)
|
102 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
103 |
-
with gr.Row():
|
104 |
-
width = gr.Slider(
|
105 |
-
label="Width",
|
106 |
-
minimum=256,
|
107 |
-
maximum=MAX_IMAGE_SIZE,
|
108 |
-
step=32,
|
109 |
-
value=1024,
|
110 |
-
)
|
111 |
-
height = gr.Slider(
|
112 |
-
label="Height",
|
113 |
-
minimum=256,
|
114 |
-
maximum=MAX_IMAGE_SIZE,
|
115 |
-
step=32,
|
116 |
-
value=1024,
|
117 |
-
)
|
118 |
-
with gr.Row():
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance Scale",
|
121 |
-
minimum=1,
|
122 |
-
maximum=15,
|
123 |
-
step=0.1,
|
124 |
-
value=3.5,
|
125 |
-
)
|
126 |
-
num_inference_steps = gr.Slider(
|
127 |
-
label="Number of inference steps",
|
128 |
-
minimum=1,
|
129 |
-
maximum=50,
|
130 |
-
step=1,
|
131 |
-
value=28,
|
132 |
-
)
|
133 |
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
inputs=[prompt],
|
138 |
-
outputs=[result, seed],
|
139 |
-
cache_examples="lazy"
|
140 |
-
)
|
141 |
-
gr.on(
|
142 |
-
triggers=[run_button.click, prompt.submit],
|
143 |
-
fn=infer,
|
144 |
-
inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
145 |
-
outputs=[result, seed]
|
146 |
-
)
|
147 |
|
148 |
-
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
import numpy as np
|
4 |
import random
|
5 |
import torch
|
6 |
from diffusers import DiffusionPipeline
|
|
|
7 |
import boto3
|
8 |
from io import BytesIO
|
9 |
import time
|
|
|
27 |
MAX_SEED = np.iinfo(np.int32).max
|
28 |
MAX_IMAGE_SIZE = 2048
|
29 |
|
30 |
+
app = FastAPI()
|
31 |
+
|
32 |
+
class InferenceRequest(BaseModel):
|
33 |
+
prompt: str
|
34 |
+
seed: int = 42
|
35 |
+
randomize_seed: bool = False
|
36 |
+
width: int = 1024
|
37 |
+
height: int = 1024
|
38 |
+
guidance_scale: float = 5.0
|
39 |
+
num_inference_steps: int = 28
|
40 |
+
|
41 |
def save_image_to_s3(image):
|
42 |
img_byte_arr = BytesIO()
|
43 |
image.save(img_byte_arr, format='PNG')
|
|
|
53 |
url = f"https://{S3_BUCKET}.s3.{S3_REGION}.amazonaws.com/{filename}"
|
54 |
return url
|
55 |
|
56 |
+
@app.post("/infer")
|
57 |
+
async def infer(request: InferenceRequest):
|
58 |
+
if request.randomize_seed:
|
59 |
seed = random.randint(0, MAX_SEED)
|
60 |
+
else:
|
61 |
+
seed = request.seed
|
62 |
+
|
63 |
generator = torch.Generator().manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
try:
|
66 |
+
image = pipe(
|
67 |
+
prompt=request.prompt,
|
68 |
+
width=request.width,
|
69 |
+
height=request.height,
|
70 |
+
num_inference_steps=request.num_inference_steps,
|
71 |
+
generator=generator,
|
72 |
+
guidance_scale=request.guidance_scale
|
73 |
+
).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
image_url = save_image_to_s3(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
return {"image_url": image_url, "seed": seed}
|
78 |
+
except Exception as e:
|
79 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
+
@app.get("/")
|
82 |
+
async def root():
|
83 |
+
return {"message": "Welcome to the IG API"}
|