Spaces:
Sleeping
Sleeping
# Startup script for FormatReview | |
# This script starts the Streamlit service and exposes it via Tailscale Serve | |
# Exit on error | |
set -e | |
# --- Configuration for Streamlit App --- | |
STREAMLIT_APP_FILE="app.py" | |
STREAMLIT_PORT="8504" | |
STREAMLIT_PID_FILE="formatreview.pid" | |
STREAMLIT_LOG_FILE="formatreview.log" | |
# --- End Configuration --- | |
# Check if UV is installed | |
if ! command -v uv &> /dev/null; then | |
echo "Error: UV is not installed. Please install UV first." | |
echo "You can install UV with: pip install uv" | |
exit 1 | |
fi | |
# Recreate virtual environment | |
echo "Recreating virtual environment..." | |
rm -rf .venv | |
uv venv | |
# Activate virtual environment | |
echo "Activating virtual environment..." | |
source .venv/bin/activate | |
# Install dependencies | |
echo "Installing dependencies..." | |
uv pip install -r requirements.txt --no-cache | |
# Kill any existing instances of the Streamlit app | |
echo "Stopping any existing instances of the Streamlit app..." | |
if [ -f "$STREAMLIT_PID_FILE" ]; then | |
OLD_STREAMLIT_PID=$(cat $STREAMLIT_PID_FILE) | |
if ps -p $OLD_STREAMLIT_PID > /dev/null; then | |
kill $OLD_STREAMLIT_PID | |
echo "Killed existing Streamlit app process with PID $OLD_STREAMLIT_PID" | |
sleep 1 # Give it time to shut down | |
else | |
echo "No running Streamlit app process found with PID $OLD_STREAMLIT_PID" | |
fi | |
fi | |
# Also try to kill any other streamlit processes for this specific app file and port | |
pkill -f "streamlit run $STREAMLIT_APP_FILE --server.port $STREAMLIT_PORT" || true | |
sleep 1 | |
# Start the Streamlit app | |
echo "Starting Streamlit app on port $STREAMLIT_PORT..." | |
nohup .venv/bin/python3 -m streamlit run $STREAMLIT_APP_FILE --server.port $STREAMLIT_PORT --server.headless true > $STREAMLIT_LOG_FILE 2>&1 & | |
echo $! > $STREAMLIT_PID_FILE | |
# Check if the Streamlit service started successfully | |
sleep 3 # Give Streamlit a bit more time to start | |
if ! nc -z localhost $STREAMLIT_PORT; then | |
echo "Error: Failed to start Streamlit app on port $STREAMLIT_PORT." | |
cat $STREAMLIT_LOG_FILE # Output log file for debugging | |
exit 1 | |
else | |
echo "Streamlit app started successfully on port $STREAMLIT_PORT." | |
fi | |
# Check if Tailscale is installed | |
if ! command -v tailscale &> /dev/null; then | |
echo "Warning: Tailscale is not installed. The app will only be available locally." | |
echo "Install Tailscale to expose the service over your tailnet." | |
else | |
# Expose the service via Tailscale Serve | |
echo "Exposing Streamlit app via Tailscale Serve on port $STREAMLIT_PORT..." | |
echo "Setting up Funnel on port 8443..." | |
tailscale funnel --https=8443 --bg localhost:$STREAMLIT_PORT | |
# Get the Tailscale hostname | |
HOSTNAME=$(tailscale status --json | jq -r '.Self.DNSName') | |
if [ -n "$HOSTNAME" ]; then | |
echo "App may be available at a Tailscale URL. Check 'tailscale status' for details." | |
echo "If using a funnel, it might be https://$HOSTNAME/" | |
else | |
echo "App is exposed via Tailscale Serve, but couldn't determine the primary hostname." | |
echo "Check 'tailscale status' for details." | |
fi | |
fi | |
echo "FormatReview is now running!" | |
echo "Local URL: http://localhost:$STREAMLIT_PORT" | |
echo "Log file: $STREAMLIT_LOG_FILE" | |
echo "PID file: $STREAMLIT_PID_FILE" | |
echo "" | |
echo "If Tailscale is active, the app should be accessible via a Tailscale funnel URL." | |