-
Notifications
You must be signed in to change notification settings - Fork 504
fix: resolve Gradio 502 startup error on WSL2 environments #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -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() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except: | |
| except OSError: | |
| # If /proc/version cannot be read, leave is_wsl as False and continue |
Copilot
AI
Dec 25, 2025
There was a problem hiding this comment.
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.
| # 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)) |
| 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" | ||||||||
|
|
@@ -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() | ||||||||
|
||||||||
| 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
AI
Dec 25, 2025
There was a problem hiding this comment.
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.
| except: | |
| except OSError: |
Copilot
AI
Dec 25, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.