celinah HF Staff commited on
Commit
3e6cc30
·
1 Parent(s): b9ef8fe
Files changed (3) hide show
  1. README.md +4 -7
  2. app.py +112 -7
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,16 +1,13 @@
1
  ---
2
- title: FLUX.1 Dev
3
- emoji: 🌖
4
- colorFrom: red
5
- colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 5.31.0
8
  app_file: app.py
9
  pinned: false
10
  short_description: FLUX.1-dev with fal.ai ⚡
11
- hf_oauth: true
12
- hf_oauth_scopes:
13
- - inference-api
14
  ---
15
 
16
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: FLUX.1 Dev with fal.ai through HF Inference Providers
3
+ emoji:
4
+ colorFrom: yellow
5
+ colorTo: pink
6
  sdk: gradio
7
  sdk_version: 5.31.0
8
  app_file: app.py
9
  pinned: false
10
  short_description: FLUX.1-dev with fal.ai ⚡
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,10 +1,115 @@
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- with gr.Blocks(fill_height=True) as demo:
4
- with gr.Sidebar():
5
- gr.Markdown("# Inference Provider")
6
- gr.Markdown("This Space showcases the black-forest-labs/FLUX.1-dev model, served by the fal-ai API. Sign in with your Hugging Face account to use this API.")
7
- button = gr.LoginButton("Sign in")
8
- gr.load("models/black-forest-labs/FLUX.1-dev", accept_token=button, provider="fal-ai")
 
 
9
 
10
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+
4
  import gradio as gr
5
+ import numpy as np
6
+ from huggingface_hub import InferenceClient, login
7
+
8
+ MAX_SEED = np.iinfo(np.int32).max
9
+ MAX_IMAGE_SIZE = 2048
10
+
11
+ hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_API_KEY")
12
+ login(token=hf_token)
13
+
14
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
15
+ client = InferenceClient(provider="fal-ai")
16
+ image = client.text_to_image(
17
+ prompt=prompt,
18
+ width=width,
19
+ height=height,
20
+ num_inference_steps=num_inference_steps,
21
+ seed=seed,
22
+ model="black-forest-labs/FLUX.1-dev"
23
+ )
24
+ return image, seed
25
+
26
+ examples = [
27
+ "a tiny astronaut hatching from an egg on the moon",
28
+ "a cat holding a sign that says hello world",
29
+ "an anime illustration of a wiener schnitzel",
30
+ ]
31
 
32
+ css="""
33
+ #col-container {
34
+ margin: 0 auto;
35
+ max-width: 520px;
36
+ }
37
+ """
38
+
39
+ with gr.Blocks(css=css) as demo:
40
 
41
+ with gr.Column(elem_id="col-container"):
42
+ gr.Markdown(f"""# FLUX.1 [schnell] with fal-ai through HF Inference Providers ⚡
43
+ learn more about HF Inference Providers [here](https://huggingface.co/docs/inference-providers/index)""")
44
+
45
+ with gr.Row():
46
+
47
+ prompt = gr.Text(
48
+ label="Prompt",
49
+ show_label=False,
50
+ max_lines=1,
51
+ placeholder="Enter your prompt",
52
+ container=False,
53
+ )
54
+
55
+ run_button = gr.Button("Run", scale=0)
56
+
57
+ result = gr.Image(label="Result", show_label=False, format="png")
58
+
59
+ with gr.Accordion("Advanced Settings", open=False):
60
+
61
+ seed = gr.Slider(
62
+ label="Seed",
63
+ minimum=0,
64
+ maximum=MAX_SEED,
65
+ step=1,
66
+ value=0,
67
+ )
68
+
69
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
70
+
71
+ with gr.Row():
72
+
73
+ width = gr.Slider(
74
+ label="Width",
75
+ minimum=256,
76
+ maximum=MAX_IMAGE_SIZE,
77
+ step=32,
78
+ value=1024,
79
+ )
80
+
81
+ height = gr.Slider(
82
+ label="Height",
83
+ minimum=256,
84
+ maximum=MAX_IMAGE_SIZE,
85
+ step=32,
86
+ value=1024,
87
+ )
88
+
89
+ with gr.Row():
90
+
91
+
92
+ num_inference_steps = gr.Slider(
93
+ label="Number of inference steps",
94
+ minimum=1,
95
+ maximum=50,
96
+ step=1,
97
+ value=4,
98
+ )
99
+
100
+ gr.Examples(
101
+ examples = examples,
102
+ fn = infer,
103
+ inputs = [prompt],
104
+ outputs = [result, seed],
105
+ cache_examples="lazy"
106
+ )
107
+
108
+ gr.on(
109
+ triggers=[run_button.click, prompt.submit],
110
+ fn = infer,
111
+ inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
112
+ outputs = [result, seed]
113
+ )
114
+
115
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ huggingface-hub
2
+ numpy