MohamedRashad commited on
Commit
38f03cc
·
1 Parent(s): df48381

Refactor image generation in app.py to streamline processing and enhance performance

Browse files
Files changed (1) hide show
  1. app.py +12 -29
app.py CHANGED
@@ -1,19 +1,11 @@
1
- import spaces
2
- from diffusers import FluxPipeline, AutoencoderKL
3
- from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
4
- import torch
5
-
6
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
- pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
8
- good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to(device)
9
- pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
10
-
11
  import gradio as gr
 
12
  from gradio_litmodel3d import LitModel3D
13
 
14
  import os
15
  os.environ['SPCONV_ALGO'] = 'native'
16
  from typing import *
 
17
  import numpy as np
18
  import imageio
19
  import uuid
@@ -23,9 +15,13 @@ from trellis.pipelines import TrellisImageTo3DPipeline
23
  from trellis.representations import Gaussian, MeshExtractResult
24
  from trellis.utils import render_utils, postprocessing_utils
25
  from gradio_client import Client
 
26
 
27
  llm_client = Client("Qwen/Qwen2.5-72B-Instruct")
28
 
 
 
 
29
  def generate_t2i_prompt(item_name):
30
  llm_prompt_template = """You are tasked with creating a concise yet highly detailed description of an item to be used for generating an image in a game development pipeline. The image should show the **entire item** with no parts cropped or hidden. The background should always be plain and monocolor, with no focus on it.
31
 
@@ -55,32 +51,19 @@ Focus on the item itself, ensuring it is fully described, and specify a plain, w
55
 
56
  return object_t2i_prompt
57
 
58
- @spaces.GPU(duration=75)
59
  def generate_item_image(object_t2i_prompt):
60
- trial_id = ""
61
- for image in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
62
- prompt=object_t2i_prompt,
63
- guidance_scale=3.5,
64
- num_inference_steps=28,
65
- width=1024,
66
- height=1024,
67
- generator=torch.Generator("cpu").manual_seed(0),
68
- output_type="pil",
69
- good_vae=good_vae,
70
- ):
71
- yield trial_id, image
72
-
73
- trial_id, processed_image = preprocess_image(image)
74
  return trial_id, processed_image
75
 
76
-
77
  MAX_SEED = np.iinfo(np.int32).max
78
  TMP_DIR = "/tmp/Trellis-demo"
79
 
80
  os.makedirs(TMP_DIR, exist_ok=True)
81
 
82
 
83
- def preprocess_image(image: Image.Image) -> Tuple[str, Image.Image]:
84
  """
85
  Preprocess the input image.
86
 
@@ -265,7 +248,7 @@ with gr.Blocks(title="Game Items Generator") as demo:
265
  for image in os.listdir("assets/example_image")
266
  ],
267
  inputs=[image_prompt],
268
- fn=preprocess_image,
269
  outputs=[trial_id, image_prompt],
270
  run_on_click=True,
271
  examples_per_page=64,
@@ -283,7 +266,7 @@ with gr.Blocks(title="Game Items Generator") as demo:
283
  outputs=[trial_id, image_prompt],
284
  )
285
  image_prompt.upload(
286
- preprocess_image,
287
  inputs=[image_prompt],
288
  outputs=[trial_id, image_prompt],
289
  )
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import spaces
3
  from gradio_litmodel3d import LitModel3D
4
 
5
  import os
6
  os.environ['SPCONV_ALGO'] = 'native'
7
  from typing import *
8
+ import torch
9
  import numpy as np
10
  import imageio
11
  import uuid
 
15
  from trellis.representations import Gaussian, MeshExtractResult
16
  from trellis.utils import render_utils, postprocessing_utils
17
  from gradio_client import Client
18
+ from diffusers import FluxPipeline
19
 
20
  llm_client = Client("Qwen/Qwen2.5-72B-Instruct")
21
 
22
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
23
+ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
24
+
25
  def generate_t2i_prompt(item_name):
26
  llm_prompt_template = """You are tasked with creating a concise yet highly detailed description of an item to be used for generating an image in a game development pipeline. The image should show the **entire item** with no parts cropped or hidden. The background should always be plain and monocolor, with no focus on it.
27
 
 
51
 
52
  return object_t2i_prompt
53
 
54
+ @spaces.GPU(duration=100)
55
  def generate_item_image(object_t2i_prompt):
56
+ image = pipe(prompt=object_t2i_prompt, guidance_scale=3.5, num_inference_steps=28, width=1024, height=1024, generator=torch.Generator("cpu").manual_seed(0), output_type="pil").images[0]
57
+ trial_id, processed_image = preprocess_pil_image(image)
 
 
 
 
 
 
 
 
 
 
 
 
58
  return trial_id, processed_image
59
 
 
60
  MAX_SEED = np.iinfo(np.int32).max
61
  TMP_DIR = "/tmp/Trellis-demo"
62
 
63
  os.makedirs(TMP_DIR, exist_ok=True)
64
 
65
 
66
+ def preprocess_pil_image(image: Image.Image) -> Tuple[str, Image.Image]:
67
  """
68
  Preprocess the input image.
69
 
 
248
  for image in os.listdir("assets/example_image")
249
  ],
250
  inputs=[image_prompt],
251
+ fn=preprocess_pil_image,
252
  outputs=[trial_id, image_prompt],
253
  run_on_click=True,
254
  examples_per_page=64,
 
266
  outputs=[trial_id, image_prompt],
267
  )
268
  image_prompt.upload(
269
+ preprocess_pil_image,
270
  inputs=[image_prompt],
271
  outputs=[trial_id, image_prompt],
272
  )