|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import spaces |
|
import argparse |
|
import os |
|
import shutil |
|
import cv2 |
|
import gradio as gr |
|
import numpy as np |
|
import torch |
|
import requests |
|
from facexlib.utils.face_restoration_helper import FaceRestoreHelper |
|
import huggingface_hub |
|
from huggingface_hub import hf_hub_download |
|
from PIL import Image |
|
from torchvision.transforms.functional import normalize |
|
|
|
from dreamo.dreamo_pipeline import DreamOPipeline |
|
from dreamo.utils import img2tensor, resize_numpy_image_area, tensor2img, resize_numpy_image_long |
|
from tools import BEN2 |
|
|
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('--port', type=int, default=8080) |
|
parser.add_argument('--version', type=str, default='v1.1') |
|
parser.add_argument('--no_turbo', action='store_true') |
|
args = parser.parse_args() |
|
|
|
huggingface_hub.login(os.getenv('HF_TOKEN')) |
|
|
|
try: |
|
shutil.rmtree('gradio_cached_examples') |
|
except FileNotFoundError: |
|
print("cache folder not exist") |
|
|
|
class Generator: |
|
def __init__(self): |
|
device = torch.device('cuda') |
|
|
|
|
|
self.bg_rm_model = BEN2.BEN_Base().to(device).eval() |
|
hf_hub_download(repo_id='PramaLLC/BEN2', filename='BEN2_Base.pth', local_dir='models') |
|
self.bg_rm_model.loadcheckpoints('models/BEN2_Base.pth') |
|
|
|
self.face_helper = FaceRestoreHelper( |
|
upscale_factor=1, |
|
face_size=512, |
|
crop_ratio=(1, 1), |
|
det_model='retinaface_resnet50', |
|
save_ext='png', |
|
device=device, |
|
) |
|
|
|
|
|
model_root = 'black-forest-labs/FLUX.1-dev' |
|
dreamo_pipeline = DreamOPipeline.from_pretrained(model_root, torch_dtype=torch.bfloat16) |
|
dreamo_pipeline.load_dreamo_model(device, use_turbo=not args.no_turbo) |
|
self.dreamo_pipeline = dreamo_pipeline.to(device) |
|
|
|
@torch.no_grad() |
|
def get_align_face(self, img): |
|
|
|
self.face_helper.clean_all() |
|
image_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) |
|
self.face_helper.read_image(image_bgr) |
|
self.face_helper.get_face_landmarks_5(only_center_face=True) |
|
self.face_helper.align_warp_face() |
|
if len(self.face_helper.cropped_faces) == 0: |
|
return None |
|
align_face = self.face_helper.cropped_faces[0] |
|
|
|
input = img2tensor(align_face, bgr2rgb=True).unsqueeze(0) / 255.0 |
|
input = input.to(torch.device("cuda")) |
|
parsing_out = self.face_helper.face_parse(normalize(input, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]))[0] |
|
parsing_out = parsing_out.argmax(dim=1, keepdim=True) |
|
bg_label = [0, 16, 18, 7, 8, 9, 14, 15] |
|
bg = sum(parsing_out == i for i in bg_label).bool() |
|
white_image = torch.ones_like(input) |
|
|
|
face_features_image = torch.where(bg, white_image, input) |
|
face_features_image = tensor2img(face_features_image, rgb2bgr=False) |
|
|
|
return face_features_image |
|
|
|
|
|
generator = Generator() |
|
|
|
|
|
@spaces.GPU |
|
def translate_albanian_to_english(text): |
|
"""Translate Albanian to English using sepioo-facebook-translation API.""" |
|
if not text.strip(): |
|
return "" |
|
for attempt in range(2): |
|
try: |
|
response = requests.post( |
|
"https://hal1993-mdftranslation1234567890abcdef1234567890-fc073a6.hf.space/v1/translate", |
|
json={"from_language": "sq", "to_language": "en", "input_text": text}, |
|
headers={"accept": "application/json", "Content-Type": "application/json"}, |
|
timeout=5 |
|
) |
|
response.raise_for_status() |
|
translated = response.json().get("translate", "") |
|
print(f"Translation response: {translated}") |
|
return translated |
|
except Exception as e: |
|
print(f"Translation error (attempt {attempt + 1}): {e}") |
|
if attempt == 1: |
|
return f"Përkthimi dështoi: {str(e)}" |
|
return f"Përkthimi dështoi" |
|
|
|
|
|
@spaces.GPU |
|
@torch.inference_mode() |
|
def generate_image( |
|
ref_image1, |
|
ref_image2, |
|
ref_task1, |
|
ref_task2, |
|
prompt_albanian, |
|
seed, |
|
width=1024, |
|
height=1024, |
|
ref_res=512, |
|
num_steps=12, |
|
guidance=3.5, |
|
true_cfg=1, |
|
cfg_start_step=0, |
|
cfg_end_step=0, |
|
neg_prompt='', |
|
neg_guidance=3.5, |
|
first_step_guidance=0, |
|
): |
|
if not prompt_albanian.strip(): |
|
return None |
|
|
|
|
|
prompt = translate_albanian_to_english(prompt_albanian) |
|
if prompt.startswith("Përkthimi dështoi"): |
|
return None |
|
|
|
print(prompt) |
|
ref_conds = [] |
|
debug_images = [] |
|
|
|
ref_images = [ref_image1, ref_image2] |
|
ref_tasks = [ref_task1, ref_task2] |
|
|
|
for idx, (ref_image, ref_task) in enumerate(zip(ref_images, ref_tasks)): |
|
if ref_image is not None: |
|
if ref_task == "id": |
|
ref_image = resize_numpy_image_long(ref_image, 1024) |
|
ref_image = generator.get_align_face(ref_image) |
|
elif ref_task != "style": |
|
ref_image = generator.bg_rm_model.inference(Image.fromarray(ref_image)) |
|
if ref_task != "id": |
|
ref_image = resize_numpy_image_area(np.array(ref_image), ref_res * ref_res) |
|
debug_images.append(ref_image) |
|
ref_image = img2tensor(ref_image, bgr2rgb=False).unsqueeze(0) / 255.0 |
|
ref_image = 2 * ref_image - 1.0 |
|
ref_conds.append( |
|
{ |
|
'img': ref_image, |
|
'task': ref_task, |
|
'idx': idx + 1, |
|
} |
|
) |
|
|
|
seed = int(seed) |
|
if seed == -1: |
|
seed = torch.Generator(device="cpu").seed() |
|
|
|
image = generator.dreamo_pipeline( |
|
prompt=prompt, |
|
width=width, |
|
height=height, |
|
num_inference_steps=num_steps, |
|
guidance_scale=guidance, |
|
ref_conds=ref_conds, |
|
generator=torch.Generator(device="cpu").manual_seed(seed), |
|
true_cfg_scale=true_cfg, |
|
true_cfg_start_step=cfg_start_step, |
|
true_cfg_end_step=cfg_end_step, |
|
negative_prompt=neg_prompt, |
|
neg_guidance_scale=neg_guidance, |
|
first_step_guidance_scale=first_step_guidance if first_step_guidance > 0 else guidance, |
|
).images[0] |
|
|
|
return image |
|
|
|
|
|
_HEADER_ = ''' |
|
<div style="text-align: center; max-width: 650px; margin: 0 auto;"> |
|
<h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem; display: contents;">DreamO v1.1</h1> |
|
<p style="font-size: 1rem; margin-bottom: 1.5rem;">Paper: <a href='https://arxiv.org/abs/2504.16915' target='_blank'>DreamO: A Unified Framework for Image Customization</a> | Codes: <a href='https://github.com/bytedance/DreamO' target='_blank'>GitHub</a></p> |
|
</div> |
|
🚩 Update Notes: |
|
- 2025.06.24: Updated to version 1.1 with significant improvements in image quality, reduced likelihood of body composition errors, and enhanced aesthetics. <a href='https://github.com/bytedance/DreamO/blob/main/dreamo_v1.1.md' target='_blank'>Learn more about this model</a> |
|
- 2025.05.11: We have updated the model to mitigate over-saturation and plastic-face issues. The new version shows consistent improvements over the previous release. |
|
❗️❗️❗️**User Guide:** |
|
- The most important thing to do first is to try the examples provided below the demo, which will help you better understand the capabilities of the DreamO model and the types of tasks it currently supports |
|
- For each input, please select the appropriate task type. For general objects, characters, or clothing, choose IP — we will remove the background from the input image. If you select ID, we will extract the face region from the input image (similar to PuLID). If you select Style, the background will be preserved, and you must prepend the prompt with the instruction: 'generate a same style image.' to activate the style task. |
|
- To accelerate inference, we adopt FLUX-turbo LoRA, which reduces the sampling steps from 25 to 12 compared to FLUX-dev. Additionally, we distill a CFG LoRA, achieving nearly a twofold reduction in steps by eliminating the need for true CFG |
|
''' |
|
|
|
_CITE_ = r""" |
|
If DreamO is helpful, please help to ⭐ the <a href='https://github.com/bytedance/DreamO' target='_blank'> Github Repo</a>. Thanks! |
|
--- |
|
📧 **Contact** |
|
If you have any questions or feedbacks, feel free to open a discussion or contact <b>[email protected]</b> and <b>[email protected]</b> |
|
""" |
|
|
|
|
|
def create_demo(): |
|
with gr.Blocks() as app: |
|
gr.HTML(""" |
|
<style> |
|
body::before { |
|
content: ""; |
|
display: block; |
|
height: 320px; |
|
background-color: var(--body-background-fill); |
|
} |
|
button[aria-label="Fullscreen"], button[aria-label="Fullscreen"]:hover { |
|
display: none !important; |
|
visibility: hidden !important; |
|
opacity: 0 !important; |
|
pointer-events: none !important; |
|
} |
|
button[aria-label="Share"], button[aria-label="Share"]:hover { |
|
display: none !important; |
|
} |
|
button[aria-label="Download"] { |
|
transform: scale(3); |
|
transform-origin: top right; |
|
margin: 0 !important; |
|
padding: 6px !important; |
|
} |
|
</style> |
|
""") |
|
|
|
gr.Markdown("") |
|
gr.Markdown("") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
with gr.Row(): |
|
ref_image1 = gr.Image(label="Imazhi Referencës 1", type="numpy", height=256) |
|
ref_image2 = gr.Image(label="Imazhi Referencës 2", type="numpy", height=256) |
|
with gr.Row(): |
|
prompt_albanian = gr.Textbox(label="Përshkrimi", value="") |
|
aspect_ratio = gr.Dropdown( |
|
choices=["16:9", "1:1", "9:16"], |
|
value="1:1", |
|
label="Permasat e fotos" |
|
) |
|
with gr.Row(): |
|
ref_task1 = gr.Dropdown(choices=["ip", "id", "style"], value="id", visible=False) |
|
ref_task2 = gr.Dropdown(choices=["ip", "id", "style"], value="ip", visible=False) |
|
num_steps = gr.Slider(8, 30, 12, step=1, visible=False) |
|
guidance = gr.Slider(1.0, 10.0, 2.0, step=0.1, visible=False) |
|
seed = gr.Textbox(value="-1", visible=False) |
|
ref_res = gr.Slider(512, 1024, 1024, step=16, visible=False) |
|
neg_prompt = gr.Textbox(value="", visible=False) |
|
neg_guidance = gr.Slider(1.0, 10.0, 3.5, step=0.1, visible=False) |
|
true_cfg = gr.Slider(1, 5, 1, step=0.1, visible=False) |
|
cfg_start_step = gr.Slider(0, 30, 0, step=1, visible=False) |
|
cfg_end_step = gr.Slider(0, 30, 0, step=1, visible=False) |
|
first_step_guidance = gr.Slider(0, 10, 0, step=0.1, visible=False) |
|
width = gr.Slider(768, 1024, 1024, step=16, visible=False) |
|
height = gr.Slider(768, 1024, 1024, step=16, visible=False) |
|
generate_btn = gr.Button("Gjenero") |
|
|
|
with gr.Column(): |
|
output_image = gr.Image(label="Imazhi i Gjeneruar", format='png') |
|
|
|
def update_resolution(aspect_ratio): |
|
if aspect_ratio == "16:9": |
|
return 1024, 576 |
|
elif aspect_ratio == "9:16": |
|
return 576, 1024 |
|
else: |
|
return 1024, 1024 |
|
|
|
aspect_ratio.change( |
|
fn=update_resolution, |
|
inputs=[aspect_ratio], |
|
outputs=[width, height] |
|
) |
|
|
|
generate_btn.click( |
|
fn=generate_image, |
|
inputs=[ |
|
ref_image1, |
|
ref_image2, |
|
ref_task1, |
|
ref_task2, |
|
prompt_albanian, |
|
seed, |
|
width, |
|
height, |
|
ref_res, |
|
num_steps, |
|
guidance, |
|
true_cfg, |
|
cfg_start_step, |
|
cfg_end_step, |
|
neg_prompt, |
|
neg_guidance, |
|
first_step_guidance, |
|
], |
|
outputs=[output_image] |
|
) |
|
|
|
return app |
|
|
|
if __name__ == "__main__": |
|
app = create_demo() |
|
app.launch(share=True) |