Fabrice-TIERCELIN commited on
Commit
3524cd4
β€’
1 Parent(s): 4eb7d6d

This PR upgrades this space to FLUX.1

Browse files

FLUX.1 is one of the most advanced free Text-to-Image AIs. This PR has a large interface and as parameters:
1. _Image format_,
1. _Prompt_,
1. _width_,
1. _height_,
1. _Guidance Scale_,
1. _Steps_,
1. _Seed_,
1. _Random seed_.

Files changed (3) hide show
  1. README.md +5 -4
  2. app.py +122 -36
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
- title: Text To Image
3
  emoji: 🐒
4
  colorFrom: green
5
  colorTo: indigo
6
- sdk: streamlit
7
- sdk_version: 1.31.1
8
  app_file: app.py
9
  pinned: false
10
  license: openrail
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: FLUX.1 [merged]
3
  emoji: 🐒
4
  colorFrom: green
5
  colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 4.41.0
8
  app_file: app.py
9
  pinned: false
10
  license: openrail
11
+ suggested_hardware: cpu-basic
12
  ---
13
 
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,36 +1,122 @@
1
- import streamlit as st
2
- import requests
3
- import io
4
- from PIL import Image
5
- import os
6
-
7
- hf_token = os.environ.get("hf_token")
8
- API_URL_KVI = "https://api-inference.huggingface.co/models/Kvikontent/kviimager2.0"
9
- API_URL_MJ = "https://api-inference.huggingface.co/models/Kvikontent/midjourney-v6"
10
- API_URL_DALLE = "https://api-inference.huggingface.co/models/ehristoforu/dalle-3-xl"
11
- headers = {"Authorization": f"Bearer {hf_token}"}
12
-
13
- def query(payload):
14
- response = requests.post(API_URL, headers=headers, json=payload)
15
- return response.content
16
-
17
- st.title("Text To Image models")
18
- st.write("Choose model and enter a prompt")
19
- model = st.selectbox(
20
- "Choose model",
21
- ("KVIImager2.0", "Midjourney V6", "Dalle 3")
22
- )
23
- prompt = st.text_input("Enter prompt")
24
-
25
- if prompt:
26
- if model == "KVIImager2.0":
27
- API_URL = API_URL_KVI
28
- elif model == "Midjourney V6":
29
- API_URL = API_URL_MJ
30
- elif model == "Dalle 3":
31
- API_URL = API_URL_DALLE
32
- image_bytes = query({
33
- "inputs": prompt,
34
- })
35
- st.image(image_bytes, caption="Generated Image")
36
- st.info("Image generated successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import spaces
5
+ import torch
6
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel, FluxPipeline
7
+ from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
8
+
9
+ dtype = torch.bfloat16
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
11
+
12
+ pipe = FluxPipeline.from_pretrained("sayakpaul/FLUX.1-merged", torch_dtype=torch.bfloat16).to(device)
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 2048
16
+
17
+
18
+ @spaces.GPU()
19
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=8, output_format="png", progress=gr.Progress(track_tqdm=True)):
20
+ if randomize_seed:
21
+ seed = random.randint(0, MAX_SEED)
22
+ generator = torch.Generator().manual_seed(seed)
23
+ image = pipe(
24
+ prompt = prompt,
25
+ width = width,
26
+ height = height,
27
+ num_inference_steps = num_inference_steps,
28
+ generator = generator,
29
+ guidance_scale=guidance_scale
30
+ ).images[0]
31
+ return gr.update(format = output_format, value = image), seed
32
+
33
+ examples = [
34
+ "a tiny astronaut hatching from an egg on the moon",
35
+ "a cat holding a sign that says hello world",
36
+ "an anime illustration of a wiener schnitzel",
37
+ ]
38
+
39
+ with gr.Blocks(delete_cache=(4000, 4000)) as demo:
40
+
41
+ with gr.Column(elem_id="col-container"):
42
+ gr.Markdown(f"""# [FLUX.1 [merged]](https://huggingface.co/sayakpaul/FLUX.1-merged)
43
+ Merge by [Sayak Paul](https://huggingface.co/sayakpaul) of 2 of the 12B param rectified flow transformers [FLUX.1 [dev]](https://huggingface.co/black-forest-labs/FLUX.1-dev) and [FLUX.1 [schnell]](https://huggingface.co/black-forest-labs/FLUX.1-schnell) by [Black Forest Labs](https://blackforestlabs.ai/)
44
+ """)
45
+
46
+ prompt = gr.Text(
47
+ label = "Prompt",
48
+ show_label = False,
49
+ lines = 2,
50
+ autofocus = True,
51
+ placeholder = "Enter your prompt",
52
+ container = False
53
+ )
54
+
55
+ output_format = gr.Radio([["*.png", "png"], ["*.webp", "webp"], ["*.jpeg", "jpeg"], ["*.gif", "gif"], ["*.bmp", "bmp"]], label="Image format for result", info="File extention", value="png", interactive=True)
56
+
57
+ with gr.Accordion("Advanced Settings", open=False):
58
+
59
+ with gr.Row():
60
+
61
+ width = gr.Slider(
62
+ label="Width",
63
+ minimum=256,
64
+ maximum=MAX_IMAGE_SIZE,
65
+ step=32,
66
+ value=1024,
67
+ )
68
+
69
+ height = gr.Slider(
70
+ label="Height",
71
+ minimum=256,
72
+ maximum=MAX_IMAGE_SIZE,
73
+ step=32,
74
+ value=1024,
75
+ )
76
+
77
+ num_inference_steps = gr.Slider(
78
+ label="Number of inference steps",
79
+ minimum=1,
80
+ maximum=50,
81
+ step=1,
82
+ value=4,
83
+ )
84
+
85
+ guidance_scale = gr.Slider(
86
+ label="Guidance Scale",
87
+ minimum=1,
88
+ maximum=15,
89
+ step=0.1,
90
+ value=3.5,
91
+ )
92
+
93
+ seed = gr.Slider(
94
+ label="Seed",
95
+ minimum=0,
96
+ maximum=MAX_SEED,
97
+ step=1,
98
+ value=0,
99
+ )
100
+
101
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
102
+
103
+ run_button = gr.Button(value = "πŸš€ Generate", variant="primary")
104
+
105
+ result = gr.Image(label="Result", show_label=False, format="png")
106
+
107
+ gr.Examples(
108
+ examples = examples,
109
+ fn = infer,
110
+ inputs = [prompt],
111
+ outputs = [result, seed],
112
+ cache_examples="lazy"
113
+ )
114
+
115
+ gr.on(
116
+ triggers=[run_button.click, prompt.submit],
117
+ fn = infer,
118
+ inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, output_format],
119
+ outputs = [result, seed]
120
+ )
121
+
122
+ demo.queue(default_concurrency_limit=2).launch(show_error=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ accelerate
2
+ git+https://github.com/huggingface/diffusers.git
3
+ torch
4
+ transformers==4.42.4
5
+ xformers
6
+ sentencepiece