Spaces:
Running
on
Zero
Running
on
Zero
Fixing Spaces Issues
Browse files- app.py +22 -20
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
import os
|
|
|
|
| 4 |
# Import constants
|
| 5 |
import numpy as np
|
| 6 |
import torch
|
|
@@ -18,7 +18,7 @@ import random
|
|
| 18 |
#import accelerate
|
| 19 |
from transformers import AutoTokenizer , DPTImageProcessor, DPTForDepthEstimation
|
| 20 |
from pathlib import Path
|
| 21 |
-
|
| 22 |
import logging
|
| 23 |
#logging.getLogger("transformers.modeling_utils").setLevel(logging.ERROR)
|
| 24 |
import gc
|
|
@@ -68,12 +68,7 @@ from utils.excluded_colors import (
|
|
| 68 |
# from utils.ai_generator import (
|
| 69 |
# generate_ai_image,
|
| 70 |
# )
|
| 71 |
-
|
| 72 |
-
versions_html,
|
| 73 |
-
#initialize_cuda,
|
| 74 |
-
#release_torch_resources,
|
| 75 |
-
#get_torch_info
|
| 76 |
-
)
|
| 77 |
from utils.lora_details import (
|
| 78 |
upd_prompt_notes,
|
| 79 |
split_prompt_precisely,
|
|
@@ -89,11 +84,13 @@ PIPELINE_CLASSES = {
|
|
| 89 |
}
|
| 90 |
|
| 91 |
import spaces
|
| 92 |
-
#-------------- ------------------------------------------------MODEL INITIALIZATION------------------------------------------------------------#
|
| 93 |
-
# Load models once during module import
|
| 94 |
-
image_processor = DPTImageProcessor.from_pretrained("Intel/dpt-large",)
|
| 95 |
-
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large", ignore_mismatched_sizes=True)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
input_image_palette = []
|
| 99 |
current_prerendered_image = gr.State("./images/images/Beeuty-1.png")
|
|
@@ -303,11 +300,11 @@ class Condition(object):
|
|
| 303 |
type_id = torch.ones_like(ids[:, :1]) * self.type_id
|
| 304 |
return tokens, ids, type_id
|
| 305 |
|
| 306 |
-
@spaces.GPU(duration=140, progress=gr.Progress(track_tqdm=True))
|
| 307 |
-
def generate_image(pipe, generate_params, progress=gr.Progress(track_tqdm=True)):
|
| 308 |
-
|
| 309 |
|
| 310 |
-
|
| 311 |
def generate_image_lowmem(
|
| 312 |
text,
|
| 313 |
neg_prompt=None,
|
|
@@ -509,7 +506,7 @@ def generate_image_lowmem(
|
|
| 509 |
generate_params = {k: v for k, v in generate_params.items() if v is not None}
|
| 510 |
print(f"generate_params: {generate_params}")
|
| 511 |
# Generate the image
|
| 512 |
-
result = generate_image(pipe,generate_params)
|
| 513 |
image = result.images[0]
|
| 514 |
# Clean up
|
| 515 |
del result
|
|
@@ -606,7 +603,7 @@ def generate_ai_image_local (
|
|
| 606 |
#gc.collect()
|
| 607 |
return None
|
| 608 |
|
| 609 |
-
#@spaces.GPU(duration=140)
|
| 610 |
def generate_input_image_click(map_option, prompt_textbox_value, negative_prompt_textbox_value, model_textbox_value, randomize_seed=True, seed=None, use_conditioned_image=False, strength=0.5, image_format="16:9", scale_factor=(8/3), progress=gr.Progress(track_tqdm=True)):
|
| 611 |
if randomize_seed:
|
| 612 |
seed = random.randint(0, constants.MAX_SEED)
|
|
@@ -661,7 +658,7 @@ def generate_input_image_click(map_option, prompt_textbox_value, negative_prompt
|
|
| 661 |
upscaled_image.save(tmp_upscaled.name, format="PNG")
|
| 662 |
constants.temp_files.append(tmp_upscaled.name)
|
| 663 |
print(f"Upscaled image saved to {tmp_upscaled.name}")
|
| 664 |
-
|
| 665 |
# Return the path of the upscaled image
|
| 666 |
return tmp_upscaled.name
|
| 667 |
|
|
@@ -698,6 +695,10 @@ def add_border(image, mask_width, mask_height, blank_color):
|
|
| 698 |
|
| 699 |
|
| 700 |
################################## DEPTH ESTIMATION ##################################
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
|
| 702 |
def create_3d_obj(rgb_image, raw_depth, image_path, depth=10, z_scale=200):
|
| 703 |
"""
|
|
@@ -713,6 +714,7 @@ def create_3d_obj(rgb_image, raw_depth, image_path, depth=10, z_scale=200):
|
|
| 713 |
Returns:
|
| 714 |
str: The file path to the saved GLTF model.
|
| 715 |
"""
|
|
|
|
| 716 |
# Normalize the depth image
|
| 717 |
depth_image = ((raw_depth - raw_depth.min()) / (raw_depth.max() - raw_depth.min()) * 255).astype("uint8")
|
| 718 |
depth_o3d = o3d.geometry.Image(depth_image)
|
|
@@ -787,7 +789,6 @@ def create_3d_obj(rgb_image, raw_depth, image_path, depth=10, z_scale=200):
|
|
| 787 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 788 |
return gltf_path
|
| 789 |
|
| 790 |
-
@spaces.GPU()
|
| 791 |
def depth_process_image(image_path, resized_width=800, z_scale=208):
|
| 792 |
"""
|
| 793 |
Processes the input image to generate a depth map and a 3D mesh reconstruction.
|
|
@@ -798,6 +799,7 @@ def depth_process_image(image_path, resized_width=800, z_scale=208):
|
|
| 798 |
Returns:
|
| 799 |
list: A list containing the depth image, 3D mesh reconstruction, and GLTF file path.
|
| 800 |
"""
|
|
|
|
| 801 |
image_path = Path(image_path)
|
| 802 |
if not image_path.exists():
|
| 803 |
raise ValueError("Image file not found")
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
+
|
| 4 |
# Import constants
|
| 5 |
import numpy as np
|
| 6 |
import torch
|
|
|
|
| 18 |
#import accelerate
|
| 19 |
from transformers import AutoTokenizer , DPTImageProcessor, DPTForDepthEstimation
|
| 20 |
from pathlib import Path
|
| 21 |
+
|
| 22 |
import logging
|
| 23 |
#logging.getLogger("transformers.modeling_utils").setLevel(logging.ERROR)
|
| 24 |
import gc
|
|
|
|
| 68 |
# from utils.ai_generator import (
|
| 69 |
# generate_ai_image,
|
| 70 |
# )
|
| 71 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
from utils.lora_details import (
|
| 73 |
upd_prompt_notes,
|
| 74 |
split_prompt_precisely,
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
import spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
from utils.version_info import (
|
| 89 |
+
versions_html,
|
| 90 |
+
#initialize_cuda,
|
| 91 |
+
#release_torch_resources,
|
| 92 |
+
#get_torch_info
|
| 93 |
+
)
|
| 94 |
|
| 95 |
input_image_palette = []
|
| 96 |
current_prerendered_image = gr.State("./images/images/Beeuty-1.png")
|
|
|
|
| 300 |
type_id = torch.ones_like(ids[:, :1]) * self.type_id
|
| 301 |
return tokens, ids, type_id
|
| 302 |
|
| 303 |
+
# @spaces.GPU(duration=140, progress=gr.Progress(track_tqdm=True))
|
| 304 |
+
# def generate_image(pipe, generate_params, progress=gr.Progress(track_tqdm=True)):
|
| 305 |
+
# return pipe(**generate_params)
|
| 306 |
|
| 307 |
+
#@spaces.GPU(duration=140, progress=gr.Progress(track_tqdm=True))
|
| 308 |
def generate_image_lowmem(
|
| 309 |
text,
|
| 310 |
neg_prompt=None,
|
|
|
|
| 506 |
generate_params = {k: v for k, v in generate_params.items() if v is not None}
|
| 507 |
print(f"generate_params: {generate_params}")
|
| 508 |
# Generate the image
|
| 509 |
+
result = pipe(**generate_params) #generate_image(pipe,generate_params)
|
| 510 |
image = result.images[0]
|
| 511 |
# Clean up
|
| 512 |
del result
|
|
|
|
| 603 |
#gc.collect()
|
| 604 |
return None
|
| 605 |
|
| 606 |
+
#@spaces.GPU(duration=140,progress=gr.Progress(track_tqdm=True))
|
| 607 |
def generate_input_image_click(map_option, prompt_textbox_value, negative_prompt_textbox_value, model_textbox_value, randomize_seed=True, seed=None, use_conditioned_image=False, strength=0.5, image_format="16:9", scale_factor=(8/3), progress=gr.Progress(track_tqdm=True)):
|
| 608 |
if randomize_seed:
|
| 609 |
seed = random.randint(0, constants.MAX_SEED)
|
|
|
|
| 658 |
upscaled_image.save(tmp_upscaled.name, format="PNG")
|
| 659 |
constants.temp_files.append(tmp_upscaled.name)
|
| 660 |
print(f"Upscaled image saved to {tmp_upscaled.name}")
|
| 661 |
+
gc.collect()
|
| 662 |
# Return the path of the upscaled image
|
| 663 |
return tmp_upscaled.name
|
| 664 |
|
|
|
|
| 695 |
|
| 696 |
|
| 697 |
################################## DEPTH ESTIMATION ##################################
|
| 698 |
+
#-------------- ------------------------------------------------MODEL INITIALIZATION------------------------------------------------------------#
|
| 699 |
+
# Load models once during module import
|
| 700 |
+
image_processor = DPTImageProcessor.from_pretrained("Intel/dpt-large",)
|
| 701 |
+
depth_model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large", ignore_mismatched_sizes=True)
|
| 702 |
|
| 703 |
def create_3d_obj(rgb_image, raw_depth, image_path, depth=10, z_scale=200):
|
| 704 |
"""
|
|
|
|
| 714 |
Returns:
|
| 715 |
str: The file path to the saved GLTF model.
|
| 716 |
"""
|
| 717 |
+
import open3d as o3d
|
| 718 |
# Normalize the depth image
|
| 719 |
depth_image = ((raw_depth - raw_depth.min()) / (raw_depth.max() - raw_depth.min()) * 255).astype("uint8")
|
| 720 |
depth_o3d = o3d.geometry.Image(depth_image)
|
|
|
|
| 789 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 790 |
return gltf_path
|
| 791 |
|
|
|
|
| 792 |
def depth_process_image(image_path, resized_width=800, z_scale=208):
|
| 793 |
"""
|
| 794 |
Processes the input image to generate a depth map and a 3D mesh reconstruction.
|
|
|
|
| 799 |
Returns:
|
| 800 |
list: A list containing the depth image, 3D mesh reconstruction, and GLTF file path.
|
| 801 |
"""
|
| 802 |
+
|
| 803 |
image_path = Path(image_path)
|
| 804 |
if not image_path.exists():
|
| 805 |
raise ValueError("Image file not found")
|
requirements.txt
CHANGED
|
@@ -15,7 +15,7 @@ invisible_watermark
|
|
| 15 |
# ==0.0.27.post2 --index-url https://download.pytorch.org/whl/cu118/xformers-0.0.27.post2%2Bcu118-cp310-cp310-manylinux2014_x86_64.whl#sha256=b3cdeeb9eae4547805ab8c3c645ac2fa9c6da85b46c039d9befa117e9f6f22fe
|
| 16 |
|
| 17 |
#generic Torch versions
|
| 18 |
-
|
| 19 |
torch
|
| 20 |
torchvision
|
| 21 |
#xformers #==0.0.29.post3
|
|
|
|
| 15 |
# ==0.0.27.post2 --index-url https://download.pytorch.org/whl/cu118/xformers-0.0.27.post2%2Bcu118-cp310-cp310-manylinux2014_x86_64.whl#sha256=b3cdeeb9eae4547805ab8c3c645ac2fa9c6da85b46c039d9befa117e9f6f22fe
|
| 16 |
|
| 17 |
#generic Torch versions
|
| 18 |
+
--extra-index-url https://download.pytorch.org/whl/cu124
|
| 19 |
torch
|
| 20 |
torchvision
|
| 21 |
#xformers #==0.0.29.post3
|