MegaTronX commited on
Commit
70b4c23
·
verified ·
1 Parent(s): 1f91a8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +129 -53
app.py CHANGED
@@ -1,64 +1,140 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
27
 
28
- response = ""
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- response += token
40
- yield response
 
 
 
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
 
 
 
 
 
61
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
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, AutoencoderTiny, AutoencoderKL
7
+ from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
8
+ from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
9
 
10
+ dtype = torch.bfloat16
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
14
+ good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
15
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
16
+ pipe.load_lora_weights("MegaTronX/MetartLoRA", weight_name="MetartLoRA.safetensors")
17
+ torch.cuda.empty_cache()
18
 
19
+ MAX_SEED = np.iinfo(np.int32).max
20
+ MAX_IMAGE_SIZE = 2048
21
 
22
+ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
23
 
24
+ @spaces.GPU(duration=75)
25
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
26
+ if randomize_seed:
27
+ seed = random.randint(0, MAX_SEED)
28
+ generator = torch.Generator().manual_seed(seed)
29
+
30
+ for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
31
+ prompt=prompt,
32
+ guidance_scale=guidance_scale,
33
+ num_inference_steps=num_inference_steps,
34
+ width=width,
35
+ height=height,
36
+ generator=generator,
37
+ output_type="pil",
38
+ good_vae=good_vae,
39
+ ):
40
+ yield img, seed
41
+
42
+ examples = [
43
+ "a tiny astronaut hatching from an egg on the moon",
44
+ "a cat holding a sign that says hello world",
45
+ "an anime illustration of a wiener schnitzel",
46
+ ]
47
 
48
+ css="""
49
+ #col-container {
50
+ margin: 0 auto;
51
+ max-width: 520px;
52
+ }
53
+ """
54
 
55
+ with gr.Blocks(css=css) as demo:
56
+
57
+ with gr.Column(elem_id="col-container"):
58
+ gr.Markdown(f"""# FLUX.1 [dev]
59
+ 12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
60
+ [[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)]
61
+ """)
62
+
63
+ with gr.Row():
64
+
65
+ prompt = gr.Text(
66
+ label="Prompt",
67
+ show_label=False,
68
+ max_lines=1,
69
+ placeholder="Enter your prompt",
70
+ container=False,
71
+ )
72
+
73
+ run_button = gr.Button("Run", scale=0)
74
+
75
+ result = gr.Image(label="Result", show_label=False)
76
+
77
+ with gr.Accordion("Advanced Settings", open=False):
78
+
79
+ seed = gr.Slider(
80
+ label="Seed",
81
+ minimum=0,
82
+ maximum=MAX_SEED,
83
+ step=1,
84
+ value=0,
85
+ )
86
+
87
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
88
+
89
+ with gr.Row():
90
+
91
+ width = gr.Slider(
92
+ label="Width",
93
+ minimum=256,
94
+ maximum=MAX_IMAGE_SIZE,
95
+ step=32,
96
+ value=1024,
97
+ )
98
+
99
+ height = gr.Slider(
100
+ label="Height",
101
+ minimum=256,
102
+ maximum=MAX_IMAGE_SIZE,
103
+ step=32,
104
+ value=1024,
105
+ )
106
+
107
+ with gr.Row():
108
 
109
+ guidance_scale = gr.Slider(
110
+ label="Guidance Scale",
111
+ minimum=1,
112
+ maximum=15,
113
+ step=0.1,
114
+ value=3.5,
115
+ )
116
+
117
+ num_inference_steps = gr.Slider(
118
+ label="Number of inference steps",
119
+ minimum=1,
120
+ maximum=50,
121
+ step=1,
122
+ value=28,
123
+ )
124
+
125
+ gr.Examples(
126
+ examples = examples,
127
+ fn = infer,
128
+ inputs = [prompt],
129
+ outputs = [result, seed],
130
+ cache_examples="lazy"
131
+ )
132
 
133
+ gr.on(
134
+ triggers=[run_button.click, prompt.submit],
135
+ fn = infer,
136
+ inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
137
+ outputs = [result, seed]
138
+ )
139
 
140
+ demo.launch()