MohamedRashad commited on
Commit
8961b93
·
1 Parent(s): dc2b440

Refactor app.py to optimize model loading and memory management; added AutoencoderTiny and adjusted GPU settings.

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -7,11 +7,8 @@ from gradio_litmodel3d import LitModel3D
7
 
8
  import os
9
  os.environ['SPCONV_ALGO'] = 'native'
10
- os.system("nvidia-smi")
11
- os.system("nvcc --version")
12
  from typing import *
13
  import torch
14
- print(f"Torch version {torch.__version__}")
15
  import numpy as np
16
  import imageio
17
  import uuid
@@ -21,15 +18,24 @@ from trellis.pipelines import TrellisImageTo3DPipeline
21
  from trellis.representations import Gaussian, MeshExtractResult
22
  from trellis.utils import render_utils, postprocessing_utils
23
  from gradio_client import Client
24
- from diffusers import FluxPipeline, AutoencoderKL
25
  from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
26
 
27
  llm_client = Client("Qwen/Qwen2.5-72B-Instruct")
 
 
 
 
 
 
28
 
29
- pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to("cuda")
30
- good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to("cuda")
31
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
32
 
 
 
 
 
 
33
  def generate_t2i_prompt(item_name):
34
  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.
35
 
@@ -75,7 +81,7 @@ def preprocess_pil_image(image: Image.Image) -> Tuple[str, Image.Image]:
75
  processed_image.save(f"{TMP_DIR}/{trial_id}.png")
76
  return trial_id, processed_image
77
 
78
- @spaces.GPU
79
  def generate_item_image(object_t2i_prompt):
80
  trial_id = ""
81
  for image in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
@@ -103,11 +109,6 @@ def generate_item_image(object_t2i_prompt):
103
  trial_id, processed_image = preprocess_pil_image(image)
104
  yield trial_id, processed_image
105
 
106
- MAX_SEED = np.iinfo(np.int32).max
107
- TMP_DIR = "/tmp/Trellis-demo"
108
-
109
- os.makedirs(TMP_DIR, exist_ok=True)
110
-
111
  def pack_state(gs: Gaussian, mesh: MeshExtractResult, trial_id: str) -> dict:
112
  return {
113
  'gaussian': {
 
7
 
8
  import os
9
  os.environ['SPCONV_ALGO'] = 'native'
 
 
10
  from typing import *
11
  import torch
 
12
  import numpy as np
13
  import imageio
14
  import uuid
 
18
  from trellis.representations import Gaussian, MeshExtractResult
19
  from trellis.utils import render_utils, postprocessing_utils
20
  from gradio_client import Client
21
+ from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
22
  from live_preview_helpers import flux_pipe_call_that_returns_an_iterable_of_images
23
 
24
  llm_client = Client("Qwen/Qwen2.5-72B-Instruct")
25
+ device = "cuda" if torch.cuda.is_available() else "cpu"
26
+
27
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=torch.bfloat16).to(device)
28
+ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
29
+ good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to(device)
30
+ torch.cuda.empty_cache()
31
 
 
 
32
  pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)
33
 
34
+ MAX_SEED = np.iinfo(np.int32).max
35
+ TMP_DIR = "/tmp/Trellis-demo"
36
+
37
+ os.makedirs(TMP_DIR, exist_ok=True)
38
+
39
  def generate_t2i_prompt(item_name):
40
  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.
41
 
 
81
  processed_image.save(f"{TMP_DIR}/{trial_id}.png")
82
  return trial_id, processed_image
83
 
84
+ @spaces.GPU(duration=75)
85
  def generate_item_image(object_t2i_prompt):
86
  trial_id = ""
87
  for image in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
 
109
  trial_id, processed_image = preprocess_pil_image(image)
110
  yield trial_id, processed_image
111
 
 
 
 
 
 
112
  def pack_state(gs: Gaussian, mesh: MeshExtractResult, trial_id: str) -> dict:
113
  return {
114
  'gaussian': {