fffiloni commited on
Commit
f413f1e
·
verified ·
1 Parent(s): 1de55b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -2
app.py CHANGED
@@ -21,6 +21,46 @@ model = AutoModelForCausalLM.from_pretrained(model_path, use_auth_token=hf_token
21
 
22
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  @spaces.GPU
25
  def llama_gen_fragrance(scene):
26
 
@@ -247,8 +287,9 @@ def infer(image_input):
247
  image_desc = extract_field(parsed, "Image Description")
248
 
249
  print(image_desc)
 
250
 
251
- return result, parsed
252
 
253
  css="""
254
  #col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
@@ -271,7 +312,8 @@ with gr.Blocks(css=css) as demo:
271
  #caption = gr.Textbox(label="Generated Caption")
272
  fragrance = gr.Textbox(label="generated Fragrance", elem_id="fragrance")
273
  json_res = gr.JSON(label="JSON")
 
274
 
275
- submit_btn.click(fn=infer, inputs=[image_in], outputs=[fragrance, json_res])
276
 
277
  demo.queue(max_size=12).launch(ssr_mode=False, mcp_server=True)
 
21
 
22
  clipi_client = Client("https://fffiloni-clip-interrogator-2.hf.space/")
23
 
24
+ # FLUX
25
+
26
+ import numpy as np
27
+ import random
28
+ import torch
29
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL
30
+ from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
31
+ from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
32
+
33
+ dtype = torch.bfloat16
34
+ device = "cuda" if torch.cuda.is_available() else "cpu"
35
+
36
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
37
+ good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
38
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)
39
+ torch.cuda.empty_cache()
40
+
41
+ MAX_SEED = np.iinfo(np.int32).max
42
+ MAX_IMAGE_SIZE = 2048
43
+
44
+ pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
45
+
46
+ @spaces.GPU
47
+ def infer_flux(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
48
+ if randomize_seed:
49
+ seed = random.randint(0, MAX_SEED)
50
+ generator = torch.Generator().manual_seed(seed)
51
+
52
+ for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
53
+ prompt=prompt,
54
+ guidance_scale=guidance_scale,
55
+ num_inference_steps=num_inference_steps,
56
+ width=width,
57
+ height=height,
58
+ generator=generator,
59
+ output_type="pil",
60
+ good_vae=good_vae,
61
+ ):
62
+ yield img
63
+
64
  @spaces.GPU
65
  def llama_gen_fragrance(scene):
66
 
 
287
  image_desc = extract_field(parsed, "Image Description")
288
 
289
  print(image_desc)
290
+ gen_bottle = infer_flux(image_desc)
291
 
292
+ return result, parsed, gen_bottle
293
 
294
  css="""
295
  #col-container {max-width: 910px; margin-left: auto; margin-right: auto;}
 
312
  #caption = gr.Textbox(label="Generated Caption")
313
  fragrance = gr.Textbox(label="generated Fragrance", elem_id="fragrance")
314
  json_res = gr.JSON(label="JSON")
315
+ bottle_res = gr.Image(label="Flacon")
316
 
317
+ submit_btn.click(fn=infer, inputs=[image_in], outputs=[fragrance, json_res, bottle_res])
318
 
319
  demo.queue(max_size=12).launch(ssr_mode=False, mcp_server=True)