Spaces:
Sleeping
Sleeping
File size: 3,403 Bytes
1a8caac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
#!/bin/bash
# Install Multi-Model Chatbot as systemd service
# This script must be run with sudo privileges
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Error: This script must be run as root (use sudo)${NC}"
exit 1
fi
# Get the actual user (not root)
ACTUAL_USER=${SUDO_USER:-$USER}
if [ "$ACTUAL_USER" = "root" ]; then
echo -e "${RED}Error: Please run with sudo as a regular user, not as root${NC}"
exit 1
fi
# Get current directory
CURRENT_DIR=$(pwd)
SERVICE_FILE="chatbot.service"
echo -e "${GREEN}=== Multi-Model Chatbot Service Installer ===${NC}"
echo ""
# Check if service file exists
if [ ! -f "$SERVICE_FILE" ]; then
echo -e "${RED}Error: $SERVICE_FILE not found in current directory${NC}"
exit 1
fi
# Check if venv exists
if [ ! -d "venv" ]; then
echo -e "${YELLOW}Warning: venv directory not found${NC}"
echo -e "${YELLOW}Please create virtual environment first:${NC}"
echo " python -m venv venv"
echo " source venv/bin/activate"
echo " pip install -r requirements-local.txt"
exit 1
fi
# Check if app.py exists
if [ ! -f "app.py" ]; then
echo -e "${RED}Error: app.py not found in current directory${NC}"
exit 1
fi
# Create customized service file
TEMP_SERVICE="/tmp/chatbot-temp.service"
cp "$SERVICE_FILE" "$TEMP_SERVICE"
# Replace placeholders
sed -i "s|YOUR_USERNAME|$ACTUAL_USER|g" "$TEMP_SERVICE"
sed -i "s|/path/to/simple-chatbot-gradio|$CURRENT_DIR|g" "$TEMP_SERVICE"
echo -e "${GREEN}β Service file configured:${NC}"
echo " User: $ACTUAL_USER"
echo " Directory: $CURRENT_DIR"
echo ""
# Create log directory if not exists
LOG_DIR="/var/log"
if [ ! -d "$LOG_DIR" ]; then
mkdir -p "$LOG_DIR"
fi
# Set log file permissions
touch /var/log/chatbot.log
touch /var/log/chatbot-error.log
chown $ACTUAL_USER:$ACTUAL_USER /var/log/chatbot.log
chown $ACTUAL_USER:$ACTUAL_USER /var/log/chatbot-error.log
chmod 644 /var/log/chatbot.log
chmod 644 /var/log/chatbot-error.log
echo -e "${GREEN}β Log files created${NC}"
# Copy service file to systemd directory
cp "$TEMP_SERVICE" /etc/systemd/system/chatbot.service
rm "$TEMP_SERVICE"
echo -e "${GREEN}β Service file installed to /etc/systemd/system/chatbot.service${NC}"
# Reload systemd daemon
systemctl daemon-reload
echo -e "${GREEN}β Systemd daemon reloaded${NC}"
# Enable service
systemctl enable chatbot.service
echo -e "${GREEN}β Service enabled (will start on boot)${NC}"
# Ask if user wants to start now
echo ""
read -p "Do you want to start the service now? (y/n) " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
systemctl start chatbot.service
sleep 2
systemctl status chatbot.service
echo ""
echo -e "${GREEN}β Service started successfully${NC}"
echo -e "${GREEN}β Access chatbot at: http://0.0.0.0:7860${NC}"
fi
echo ""
echo -e "${GREEN}=== Installation Complete ===${NC}"
echo ""
echo "Useful commands:"
echo " sudo systemctl start chatbot # Start service"
echo " sudo systemctl stop chatbot # Stop service"
echo " sudo systemctl restart chatbot # Restart service"
echo " sudo systemctl status chatbot # Check status"
echo " sudo journalctl -u chatbot -f # View live logs"
echo " tail -f /var/log/chatbot.log # View application logs"
echo ""
|