Upload 2 files
Browse files- app.py +147 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import requests
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
|
9 |
+
MAX_SEED = np.iinfo(np.int32).max
|
10 |
+
MAX_IMAGE_SIZE = 2048
|
11 |
+
|
12 |
+
class APIClient:
|
13 |
+
def __init__(self, api_key=os.getenv("API_KEY"), base_url="inference.prodia.com"):
|
14 |
+
self.headers = {
|
15 |
+
"Content-Type": "application/json",
|
16 |
+
"Accept": "image/jpeg",
|
17 |
+
"Authorization": f"Bearer {api_key}"
|
18 |
+
}
|
19 |
+
self.base_url = f"https://{base_url}"
|
20 |
+
|
21 |
+
def _post(self, url, json=None):
|
22 |
+
r = requests.post(url, headers=self.headers, json=json)
|
23 |
+
r.raise_for_status()
|
24 |
+
|
25 |
+
return Image.open(BytesIO(r.content)).convert("RGBA")
|
26 |
+
|
27 |
+
def job(self, config):
|
28 |
+
body = {"type": "inference.flux.dev.txt2img.v1", "config": config}
|
29 |
+
return self._post(f"{self.base_url}/v2/job", json=body)
|
30 |
+
|
31 |
+
|
32 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
33 |
+
if randomize_seed:
|
34 |
+
seed = random.randint(0, MAX_SEED)
|
35 |
+
image = generative_api.job({
|
36 |
+
"prompt": prompt,
|
37 |
+
"width": width,
|
38 |
+
"height": height,
|
39 |
+
"seed": seed,
|
40 |
+
"num_inference_steps": num_inference_steps,
|
41 |
+
"guidance_scale": guidance_scale
|
42 |
+
})
|
43 |
+
return image, seed
|
44 |
+
|
45 |
+
generative_api = APIClient()
|
46 |
+
|
47 |
+
examples = [
|
48 |
+
"a tiny astronaut hatching from an egg on the moon",
|
49 |
+
"a cat holding a sign that says hello world",
|
50 |
+
"an anime illustration of a wiener schnitzel",
|
51 |
+
]
|
52 |
+
|
53 |
+
css="""
|
54 |
+
#col-container {
|
55 |
+
margin: 0 auto;
|
56 |
+
max-width: 520px;
|
57 |
+
}
|
58 |
+
"""
|
59 |
+
|
60 |
+
with gr.Blocks(css=css) as demo:
|
61 |
+
|
62 |
+
with gr.Column(elem_id="col-container"):
|
63 |
+
gr.Markdown(f"""# FLUX.1 [dev]
|
64 |
+
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
65 |
+
[[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)]
|
66 |
+
""")
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
|
70 |
+
prompt = gr.Text(
|
71 |
+
label="Prompt",
|
72 |
+
show_label=False,
|
73 |
+
max_lines=1,
|
74 |
+
placeholder="Enter your prompt",
|
75 |
+
container=False,
|
76 |
+
)
|
77 |
+
|
78 |
+
run_button = gr.Button("Run", scale=0)
|
79 |
+
|
80 |
+
result = gr.Image(label="Result", show_label=False)
|
81 |
+
|
82 |
+
with gr.Accordion("Advanced Settings", open=False):
|
83 |
+
|
84 |
+
seed = gr.Slider(
|
85 |
+
label="Seed",
|
86 |
+
minimum=0,
|
87 |
+
maximum=MAX_SEED,
|
88 |
+
step=1,
|
89 |
+
value=0,
|
90 |
+
)
|
91 |
+
|
92 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
93 |
+
|
94 |
+
with gr.Row():
|
95 |
+
|
96 |
+
width = gr.Slider(
|
97 |
+
label="Width",
|
98 |
+
minimum=256,
|
99 |
+
maximum=MAX_IMAGE_SIZE,
|
100 |
+
step=32,
|
101 |
+
value=1024,
|
102 |
+
interactive=False
|
103 |
+
)
|
104 |
+
|
105 |
+
height = gr.Slider(
|
106 |
+
label="Height",
|
107 |
+
minimum=256,
|
108 |
+
maximum=MAX_IMAGE_SIZE,
|
109 |
+
step=32,
|
110 |
+
value=1024,
|
111 |
+
interactive=False
|
112 |
+
)
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
|
116 |
+
guidance_scale = gr.Slider(
|
117 |
+
label="Guidance Scale",
|
118 |
+
minimum=1,
|
119 |
+
maximum=15,
|
120 |
+
step=0.1,
|
121 |
+
value=3.5,
|
122 |
+
)
|
123 |
+
|
124 |
+
num_inference_steps = gr.Slider(
|
125 |
+
label="Number of inference steps",
|
126 |
+
minimum=1,
|
127 |
+
maximum=50,
|
128 |
+
step=1,
|
129 |
+
value=28,
|
130 |
+
)
|
131 |
+
|
132 |
+
gr.Examples(
|
133 |
+
examples = examples,
|
134 |
+
fn = infer,
|
135 |
+
inputs = [prompt],
|
136 |
+
outputs = [result, seed],
|
137 |
+
cache_examples="lazy"
|
138 |
+
)
|
139 |
+
|
140 |
+
gr.on(
|
141 |
+
triggers=[run_button.click, prompt.submit],
|
142 |
+
fn = infer,
|
143 |
+
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
144 |
+
outputs = [result, seed]
|
145 |
+
)
|
146 |
+
|
147 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
requests~=2.32.3
|
2 |
+
numpy~=1.26.4
|