Upload ai_studio_code (100).py
Browse files- ai_studio_code (100).py +130 -0
ai_studio_code (100).py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# dreamo_helpers.py (CORRIGIDO)
|
2 |
+
# Módulo de serviço para o DreamO, com gestão de memória e aceitando uma lista dinâmica de referências.
|
3 |
+
|
4 |
+
import os
|
5 |
+
import cv2
|
6 |
+
import torch
|
7 |
+
import numpy as np
|
8 |
+
from PIL import Image
|
9 |
+
import huggingface_hub
|
10 |
+
import gc
|
11 |
+
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
12 |
+
from torchvision.transforms.functional import normalize
|
13 |
+
from dreamo.dreamo_pipeline import DreamOPipeline
|
14 |
+
from dreamo.utils import img2tensor, tensor2img
|
15 |
+
from tools import BEN2
|
16 |
+
|
17 |
+
class Generator:
|
18 |
+
def __init__(self):
|
19 |
+
self.cpu_device = torch.device('cpu')
|
20 |
+
self.gpu_device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
21 |
+
|
22 |
+
print("Carregando modelos DreamO para a CPU...")
|
23 |
+
model_root = 'black-forest-labs/FLUX.1-dev'
|
24 |
+
self.dreamo_pipeline = DreamOPipeline.from_pretrained(model_root, torch_dtype=torch.bfloat16)
|
25 |
+
self.dreamo_pipeline.load_dreamo_model(self.cpu_device, use_turbo=True)
|
26 |
+
|
27 |
+
self.bg_rm_model = BEN2.BEN_Base().to(self.cpu_device).eval()
|
28 |
+
huggingface_hub.hf_hub_download(repo_id='PramaLLC/BEN2', filename='BEN2_Base.pth', local_dir='models')
|
29 |
+
self.bg_rm_model.loadcheckpoints('models/BEN2_Base.pth')
|
30 |
+
|
31 |
+
self.face_helper = FaceRestoreHelper(
|
32 |
+
upscale_factor=1, face_size=512, crop_ratio=(1, 1),
|
33 |
+
det_model='retinaface_resnet50', save_ext='png', device=self.cpu_device,
|
34 |
+
)
|
35 |
+
print("Modelos DreamO prontos (na CPU).")
|
36 |
+
|
37 |
+
def to_gpu(self):
|
38 |
+
if self.gpu_device.type == 'cpu': return
|
39 |
+
print("Movendo modelos DreamO para a GPU...")
|
40 |
+
self.dreamo_pipeline.to(self.gpu_device)
|
41 |
+
self.bg_rm_model.to(self.gpu_device)
|
42 |
+
self.face_helper.device = self.gpu_device
|
43 |
+
self.dreamo_pipeline.t5_embedding.to(self.gpu_device)
|
44 |
+
self.dreamo_pipeline.task_embedding.to(self.gpu_device)
|
45 |
+
self.dreamo_pipeline.idx_embedding.to(self.gpu_device)
|
46 |
+
if hasattr(self.face_helper, 'face_parse'): self.face_helper.face_parse.to(self.gpu_device)
|
47 |
+
if hasattr(self.face_helper, 'face_det'): self.face_helper.face_det.to(self.gpu_device)
|
48 |
+
print("Modelos DreamO na GPU.")
|
49 |
+
|
50 |
+
def to_cpu(self):
|
51 |
+
if self.gpu_device.type == 'cpu': return
|
52 |
+
print("Descarregando modelos DreamO da GPU...")
|
53 |
+
self.dreamo_pipeline.to(self.cpu_device)
|
54 |
+
self.bg_rm_model.to(self.cpu_device)
|
55 |
+
self.face_helper.device = self.cpu_device
|
56 |
+
self.dreamo_pipeline.t5_embedding.to(self.cpu_device)
|
57 |
+
self.dreamo_pipeline.task_embedding.to(self.cpu_device)
|
58 |
+
self.dreamo_pipeline.idx_embedding.to(self.cpu_device)
|
59 |
+
if hasattr(self.face_helper, 'face_det'): self.face_helper.face_det.to(self.cpu_device)
|
60 |
+
if hasattr(self.face_helper, 'face_parse'): self.face_helper.face_parse.to(self.cpu_device)
|
61 |
+
gc.collect()
|
62 |
+
if torch.cuda.is_available(): torch.cuda.empty_cache()
|
63 |
+
|
64 |
+
@torch.inference_mode()
|
65 |
+
def generate_image_with_gpu_management(self, reference_items, prompt, width, height):
|
66 |
+
try:
|
67 |
+
self.to_gpu()
|
68 |
+
|
69 |
+
ref_conds = []
|
70 |
+
|
71 |
+
for idx, item in enumerate(reference_items):
|
72 |
+
ref_image_np = item.get('image_np')
|
73 |
+
ref_task = item.get('task')
|
74 |
+
|
75 |
+
if ref_image_np is not None:
|
76 |
+
if ref_task == "id":
|
77 |
+
ref_image = self.get_align_face(ref_image_np)
|
78 |
+
elif ref_task != "style":
|
79 |
+
ref_image = self.bg_rm_model.inference(Image.fromarray(ref_image_np))
|
80 |
+
else:
|
81 |
+
ref_image = ref_image_np
|
82 |
+
|
83 |
+
ref_image_tensor = img2tensor(np.array(ref_image), bgr2rgb=False).unsqueeze(0) / 255.0
|
84 |
+
ref_image_tensor = (2 * ref_image_tensor - 1.0).to(self.gpu_device, dtype=torch.bfloat16)
|
85 |
+
|
86 |
+
ref_conds.append({'img': ref_image_tensor, 'task': ref_task, 'idx': idx + 1})
|
87 |
+
|
88 |
+
# <<< CORREÇÃO APLICADA AQUI >>>
|
89 |
+
image = self.dreamo_pipeline(
|
90 |
+
prompt=prompt,
|
91 |
+
width=width,
|
92 |
+
height=height,
|
93 |
+
num_inference_steps=12,
|
94 |
+
guidance_scale=4.5,
|
95 |
+
ref_conds=ref_conds,
|
96 |
+
generator=torch.Generator(device=self.gpu_device).manual_seed(42) # Usar o dispositivo GPU
|
97 |
+
).images[0]
|
98 |
+
|
99 |
+
return image
|
100 |
+
|
101 |
+
finally:
|
102 |
+
self.to_cpu()
|
103 |
+
|
104 |
+
|
105 |
+
@torch.no_grad()
|
106 |
+
def get_align_face(self, img):
|
107 |
+
self.face_helper.clean_all()
|
108 |
+
image_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
109 |
+
self.face_helper.read_image(image_bgr)
|
110 |
+
self.face_helper.get_face_landmarks_5(only_center_face=True)
|
111 |
+
self.face_helper.align_warp_face()
|
112 |
+
if len(self.face_helper.cropped_faces) == 0: return None
|
113 |
+
align_face = self.face_helper.cropped_faces[0]
|
114 |
+
input_tensor = img2tensor(align_face, bgr2rgb=True).unsqueeze(0) / 255.0
|
115 |
+
input_tensor = input_tensor.to(self.gpu_device) # Necessário para o face_parse
|
116 |
+
parsing_out = self.face_helper.face_parse(normalize(input_tensor, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]))[0]
|
117 |
+
parsing_out = parsing_out.argmax(dim=1, keepdim=True)
|
118 |
+
bg_label = [0, 16, 18, 7, 8, 9, 14, 15]
|
119 |
+
bg = sum(parsing_out == i for i in bg_label).bool()
|
120 |
+
white_image = torch.ones_like(input_tensor)
|
121 |
+
face_features_image = torch.where(bg, white_image, input_tensor)
|
122 |
+
return tensor2img(face_features_image, rgb2bgr=False)
|
123 |
+
|
124 |
+
# --- Instância Singleton ---
|
125 |
+
# A inicialização permanece a mesma, pois é condicional dentro do app.py principal
|
126 |
+
print("Inicializando o Pintor de Cenas (DreamO Helper)...")
|
127 |
+
hf_token = os.getenv('HF_TOKEN')
|
128 |
+
if hf_token: huggingface_hub.login(token=hf_token)
|
129 |
+
dreamo_generator_singleton = Generator()
|
130 |
+
print("Pintor de Cenas (DreamO Helper) pronto.")
|