Spaces:
Running
Running
Update startup_hf.py
Browse files- startup_hf.py +17 -1
startup_hf.py
CHANGED
|
@@ -13,6 +13,9 @@ def setup_directories():
|
|
| 13 |
"""Setup required directories with proper permissions"""
|
| 14 |
print("Setting up directories for Hugging Face Spaces...")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
# Get the port from environment or default to 7860 for Hugging Face Spaces
|
| 17 |
port = int(os.environ.get('PORT', 7860))
|
| 18 |
os.environ['PORT'] = str(port)
|
|
@@ -94,6 +97,7 @@ def setup_directories():
|
|
| 94 |
|
| 95 |
def start_application():
|
| 96 |
"""Start the Flask application using gunicorn"""
|
|
|
|
| 97 |
port = os.environ.get('PORT', '7860')
|
| 98 |
|
| 99 |
# Use gunicorn to serve the Flask app
|
|
@@ -104,12 +108,24 @@ def start_application():
|
|
| 104 |
'--workers', '1',
|
| 105 |
'--timeout', '120',
|
| 106 |
'--log-level', 'info',
|
|
|
|
|
|
|
| 107 |
'app:app'
|
| 108 |
]
|
| 109 |
|
| 110 |
print(f"Starting application with command: {' '.join(cmd)}")
|
|
|
|
| 111 |
sys.stdout.flush() # Ensure output is visible in logs
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
if __name__ == '__main__':
|
| 115 |
if setup_directories():
|
|
|
|
| 13 |
"""Setup required directories with proper permissions"""
|
| 14 |
print("Setting up directories for Hugging Face Spaces...")
|
| 15 |
|
| 16 |
+
# Set containerized environment variable
|
| 17 |
+
os.environ['CONTAINERIZED'] = 'true'
|
| 18 |
+
|
| 19 |
# Get the port from environment or default to 7860 for Hugging Face Spaces
|
| 20 |
port = int(os.environ.get('PORT', 7860))
|
| 21 |
os.environ['PORT'] = str(port)
|
|
|
|
| 97 |
|
| 98 |
def start_application():
|
| 99 |
"""Start the Flask application using gunicorn"""
|
| 100 |
+
# Hugging Face Spaces typically uses PORT environment variable
|
| 101 |
port = os.environ.get('PORT', '7860')
|
| 102 |
|
| 103 |
# Use gunicorn to serve the Flask app
|
|
|
|
| 108 |
'--workers', '1',
|
| 109 |
'--timeout', '120',
|
| 110 |
'--log-level', 'info',
|
| 111 |
+
'--access-logfile', '-', # Log to stdout
|
| 112 |
+
'--error-logfile', '-', # Log to stdout
|
| 113 |
'app:app'
|
| 114 |
]
|
| 115 |
|
| 116 |
print(f"Starting application with command: {' '.join(cmd)}")
|
| 117 |
+
print(f"Application should be available at http://0.0.0.0:{port}")
|
| 118 |
sys.stdout.flush() # Ensure output is visible in logs
|
| 119 |
+
|
| 120 |
+
# Start the application
|
| 121 |
+
try:
|
| 122 |
+
subprocess.run(cmd, check=True)
|
| 123 |
+
except subprocess.CalledProcessError as e:
|
| 124 |
+
print(f"Error starting application: {e}")
|
| 125 |
+
sys.exit(1)
|
| 126 |
+
except KeyboardInterrupt:
|
| 127 |
+
print("Application stopped by user")
|
| 128 |
+
sys.exit(0)
|
| 129 |
|
| 130 |
if __name__ == '__main__':
|
| 131 |
if setup_directories():
|