Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gradio as gr
import argparse

import os
os.environ['OPENCV_IO_ENABLE_OPENEXR'] = '1'
Expand Down Expand Up @@ -616,6 +617,36 @@ def extract_glb(

# Launch the Gradio app
if __name__ == "__main__":
# Parse command line arguments for server configuration
parser = argparse.ArgumentParser(description='TRELLIS.2 Image to 3D Demo')
parser.add_argument('--server-name', type=str, default=None,
help='Server name/IP to bind to. Use "0.0.0.0" for WSL2 or Docker environments.')
parser.add_argument('--server-port', type=int, default=7860,
help='Port to run the server on (default: 7860)')
parser.add_argument('--share', action='store_true',
help='Create a public Gradio share link')
args = parser.parse_args()

# Auto-detect WSL2 environment and set appropriate server_name
server_name = args.server_name
if server_name is None:
# Check if running in WSL2
is_wsl = False
try:
with open('/proc/version', 'r') as f:
is_wsl = 'microsoft' in f.read().lower() or 'wsl' in f.read().lower()
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is being read twice in this line. The first f.read().lower() reads the entire file content, and then the second f.read().lower() is called on an empty file buffer since the file pointer is already at the end. This means the 'wsl' check will never match. Store the result of f.read().lower() in a variable first and then check both conditions against that variable.

Suggested change
is_wsl = 'microsoft' in f.read().lower() or 'wsl' in f.read().lower()
version_info = f.read().lower()
is_wsl = 'microsoft' in version_info or 'wsl' in version_info

Copilot uses AI. Check for mistakes.
except:
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bare except clause without specifying an exception type is considered bad practice. It catches all exceptions including system-exiting exceptions like KeyboardInterrupt and SystemExit, which should typically not be caught. Specify the expected exception types (e.g., FileNotFoundError, IOError, OSError) to make the error handling more explicit and avoid masking unexpected errors.

Suggested change
except:
except OSError:
# If /proc/version cannot be read, leave is_wsl as False and continue

Copilot uses AI. Check for mistakes.
pass

# Also check WSL_DISTRO_NAME environment variable
if os.environ.get('WSL_DISTRO_NAME') or os.environ.get('WSL_INTEROP'):
is_wsl = True

if is_wsl:
server_name = "0.0.0.0"
print("WSL2 environment detected. Using server_name='0.0.0.0' for better compatibility.")
print("Access the app at http://localhost:{} from your Windows browser.".format(args.server_port))

Comment on lines +630 to +649
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WSL2 detection logic (lines 630-648) is duplicated between app.py and app_texturing.py. Consider extracting this into a shared utility function to improve maintainability and ensure consistency. This would make it easier to fix bugs (like the double file read issue) or add improvements in one place rather than maintaining the same code in multiple locations.

Suggested change
# Auto-detect WSL2 environment and set appropriate server_name
server_name = args.server_name
if server_name is None:
# Check if running in WSL2
is_wsl = False
try:
with open('/proc/version', 'r') as f:
is_wsl = 'microsoft' in f.read().lower() or 'wsl' in f.read().lower()
except:
pass
# Also check WSL_DISTRO_NAME environment variable
if os.environ.get('WSL_DISTRO_NAME') or os.environ.get('WSL_INTEROP'):
is_wsl = True
if is_wsl:
server_name = "0.0.0.0"
print("WSL2 environment detected. Using server_name='0.0.0.0' for better compatibility.")
print("Access the app at http://localhost:{} from your Windows browser.".format(args.server_port))
def _is_wsl2() -> bool:
"""
Detect whether the current environment is WSL2.
Checks both /proc/version content and common WSL environment variables.
"""
# Check if running in WSL2 via /proc/version
try:
with open('/proc/version', 'r') as f:
version_info = f.read().lower()
if 'microsoft' in version_info or 'wsl' in version_info:
return True
except Exception:
# If /proc/version cannot be read, fall back to environment variables
pass
# Also check WSL-related environment variables
if os.environ.get('WSL_DISTRO_NAME') or os.environ.get('WSL_INTEROP'):
return True
return False
# Auto-detect WSL2 environment and set appropriate server_name
server_name = args.server_name
if server_name is None and _is_wsl2():
server_name = "0.0.0.0"
print("WSL2 environment detected. Using server_name='0.0.0.0' for better compatibility.")
print("Access the app at http://localhost:{} from your Windows browser.".format(args.server_port))

Copilot uses AI. Check for mistakes.
os.makedirs(TMP_DIR, exist_ok=True)

# Construct ui components
Expand All @@ -642,4 +673,10 @@ def extract_glb(
)),
}

demo.launch(css=css, head=head)
demo.launch(
css=css,
head=head,
server_name=server_name,
server_port=args.server_port,
share=args.share,
)
37 changes: 36 additions & 1 deletion app_texturing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gradio as gr
import argparse

import os
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
Expand Down Expand Up @@ -143,9 +144,43 @@ def shapeimage_to_tex(

# Launch the Gradio app
if __name__ == "__main__":
# Parse command line arguments for server configuration
parser = argparse.ArgumentParser(description='TRELLIS.2 Texturing Demo')
parser.add_argument('--server-name', type=str, default=None,
help='Server name/IP to bind to. Use "0.0.0.0" for WSL2 or Docker environments.')
parser.add_argument('--server-port', type=int, default=7860,
help='Port to run the server on (default: 7860)')
parser.add_argument('--share', action='store_true',
help='Create a public Gradio share link')
args = parser.parse_args()

# Auto-detect WSL2 environment and set appropriate server_name
server_name = args.server_name
if server_name is None:
# Check if running in WSL2
is_wsl = False
try:
with open('/proc/version', 'r') as f:
is_wsl = 'microsoft' in f.read().lower() or 'wsl' in f.read().lower()
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is being read twice in this line. The first f.read().lower() reads the entire file content, and then the second f.read().lower() is called on an empty file buffer since the file pointer is already at the end. This means the 'wsl' check will never match. Store the result of f.read().lower() in a variable first and then check both conditions against that variable.

Suggested change
is_wsl = 'microsoft' in f.read().lower() or 'wsl' in f.read().lower()
version_info = f.read().lower()
is_wsl = 'microsoft' in version_info or 'wsl' in version_info

Copilot uses AI. Check for mistakes.
except:
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bare except clause without specifying an exception type is considered bad practice. It catches all exceptions including system-exiting exceptions like KeyboardInterrupt and SystemExit, which should typically not be caught. Specify the expected exception types (e.g., FileNotFoundError, IOError, OSError) to make the error handling more explicit and avoid masking unexpected errors.

Suggested change
except:
except OSError:

Copilot uses AI. Check for mistakes.
pass

# Also check WSL_DISTRO_NAME environment variable
if os.environ.get('WSL_DISTRO_NAME') or os.environ.get('WSL_INTEROP'):
is_wsl = True

if is_wsl:
server_name = "0.0.0.0"
print("WSL2 environment detected. Using server_name='0.0.0.0' for better compatibility.")
print("Access the app at http://localhost:{} from your Windows browser.".format(args.server_port))
Comment on lines +157 to +175
Copy link

Copilot AI Dec 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WSL2 detection logic (lines 157-175) is duplicated between app.py and app_texturing.py. Consider extracting this into a shared utility function to improve maintainability and ensure consistency. This would make it easier to fix bugs (like the double file read issue) or add improvements in one place rather than maintaining the same code in multiple locations.

Copilot uses AI. Check for mistakes.

os.makedirs(TMP_DIR, exist_ok=True)

pipeline = Trellis2TexturingPipeline.from_pretrained('microsoft/TRELLIS.2-4B', config_file="texturing_pipeline.json")
pipeline.cuda()

demo.launch()
demo.launch(
server_name=server_name,
server_port=args.server_port,
share=args.share,
)