This guide explains how to deploy the Text2Trait frontend application using Gunicorn and the provided WSGI entry point.
-
Python 3.12 or higher
-
Install dependencies from
text2trait_forntend_app/src/pyproject.toml: (Note: The directory name contains a typo 'forntend' - this is the actual name in the repository)cd text2trait_forntend_app/src pip install -r pyproject.toml # or with poetry: poetry install
-
Install Gunicorn:
pip install gunicorn
The wsgi_t2tfe.py file at the root of the repository provides a Gunicorn-compatible WSGI entry point for the Text2Trait frontend Dash application.
For local development and testing:
python wsgi_t2tfe.pyThe application will be available at http://localhost:8051/
For production deployment, use Gunicorn:
gunicorn wsgi_t2tfe:server --bind 0.0.0.0:8051 --workers 4gunicorn wsgi_t2tfe:server \
--bind 127.0.0.1:8051 \
--workers 4 \
--timeout 120 \
--access-logfile /var/log/text2trait/access.log \
--error-logfile /var/log/text2trait/error.log \
--log-level info--bind 127.0.0.1:8051- Bind only to localhost on port 8051--workers 4- Use 4 worker processes (adjust based on CPU cores: 2-4 × CPU cores)--timeout 120- Worker timeout in seconds (adjust based on your needs)--access-logfile- Path for access logs--error-logfile- Path for error logs--log-level- Logging level (debug, info, warning, error, critical)
Create a systemd service file /etc/systemd/system/text2trait-frontend.service:
[Unit]
Description=Text2Trait Frontend Application
After=network.target
[Service]
Type=simple
User=harvest
Group=harvest
WorkingDirectory=/path/to/Text2Trait
Environment="PATH=/path/to/venv/bin"
ExecStart=/path/to/venv/bin/gunicorn wsgi_t2tfe:server \
--bind 127.0.0.1:8051 \
--workers 4 \
--timeout 120 \
--access-logfile /var/log/text2trait/access.log \
--error-logfile /var/log/text2trait/error.log
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.targetEnable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable text2trait-frontend
sudo systemctl start text2trait-frontend
sudo systemctl status text2trait-frontendCreate an Nginx configuration /etc/nginx/sites-available/text2trait:
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:8051;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket support (if needed by Dash)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Increase timeouts for long-running requests
proxy_connect_timeout 120s;
proxy_send_timeout 120s;
proxy_read_timeout 120s;
}
}Enable the site:
sudo ln -s /etc/nginx/sites-available/text2trait /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxIf your application requires environment variables, set them before running:
export DATA_PATH=/path/to/data
export LOG_LEVEL=INFO
gunicorn wsgi_t2tfe:server --bind 127.0.0.1:8051 --workers 4Monitor the application:
# View logs
tail -f /var/log/text2trait/access.log
tail -f /var/log/text2trait/error.log
# Check service status
sudo systemctl status text2trait-frontend
# View systemd logs
sudo journalctl -u text2trait-frontend -fThe optimal number of workers depends on your server:
- CPU-bound applications:
(2 × CPU cores) + 1 - I/O-bound applications:
(4 × CPU cores) + 1
For the Dash application (mostly I/O-bound):
# For a 4-core server:
gunicorn wsgi_t2tfe:server --bind 127.0.0.1:8051 --workers 17For better async support, use the gevent worker class:
pip install gevent
gunicorn wsgi_t2tfe:server \
--bind 127.0.0.1:8051 \
--workers 4 \
--worker-class gevent \
--worker-connections 1000-
Check Python path and dependencies:
python -c "from wsgi_t2tfe import server; print('OK')" -
Check Gunicorn syntax:
gunicorn --check-config wsgi_t2tfe:server
-
Check logs for errors:
tail -f /var/log/text2trait/error.log
Reduce the number of workers:
gunicorn wsgi_t2tfe:server --bind 127.0.0.1:8051 --workers 2Increase the timeout:
gunicorn wsgi_t2tfe:server --bind 127.0.0.1:8051 --timeout 300- Never expose Gunicorn directly to the internet - Always use a reverse proxy like Nginx
- Use HTTPS - Configure SSL/TLS in Nginx
- Firewall rules - Restrict access to port 8051 to localhost only
- Keep dependencies updated - Regularly update Python packages
- Use a dedicated user - Don't run as root