Spaces:
Running
Running
Rename startup-hf.sh to start_hf.py
Browse files- start_hf.py +135 -0
- startup-hf.sh +0 -40
start_hf.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
HuggingFace Spaces Startup Script
|
| 4 |
+
Handles bot and server startup with proper port management
|
| 5 |
+
"""
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import time
|
| 9 |
+
import signal
|
| 10 |
+
import socket
|
| 11 |
+
import subprocess
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
PORT = int(os.environ.get("PORT", 7860))
|
| 15 |
+
|
| 16 |
+
def is_port_in_use(port):
|
| 17 |
+
"""Check if a port is in use"""
|
| 18 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 19 |
+
return s.connect_ex(('localhost', port)) == 0
|
| 20 |
+
|
| 21 |
+
def kill_port_process(port):
|
| 22 |
+
"""Kill process using a specific port"""
|
| 23 |
+
try:
|
| 24 |
+
# Try fuser first
|
| 25 |
+
result = subprocess.run(
|
| 26 |
+
f"fuser -k {port}/tcp",
|
| 27 |
+
shell=True,
|
| 28 |
+
capture_output=True,
|
| 29 |
+
timeout=5
|
| 30 |
+
)
|
| 31 |
+
if result.returncode == 0:
|
| 32 |
+
print(f"✅ Killed process on port {port} using fuser")
|
| 33 |
+
return True
|
| 34 |
+
except:
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
# Try lsof
|
| 39 |
+
result = subprocess.run(
|
| 40 |
+
f"lsof -t -i:{port}",
|
| 41 |
+
shell=True,
|
| 42 |
+
capture_output=True,
|
| 43 |
+
timeout=5
|
| 44 |
+
)
|
| 45 |
+
if result.returncode == 0:
|
| 46 |
+
pids = result.stdout.decode().strip().split('\n')
|
| 47 |
+
for pid in pids:
|
| 48 |
+
if pid:
|
| 49 |
+
os.kill(int(pid), signal.SIGKILL)
|
| 50 |
+
print(f"✅ Killed process(es) on port {port} using lsof")
|
| 51 |
+
return True
|
| 52 |
+
except:
|
| 53 |
+
pass
|
| 54 |
+
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
def start_bot():
|
| 58 |
+
"""Start the Ultroid bot in background"""
|
| 59 |
+
print("🤖 Starting Ultroid bot...")
|
| 60 |
+
|
| 61 |
+
if os.environ.get("SESSION1"):
|
| 62 |
+
bot_process = subprocess.Popen(
|
| 63 |
+
[sys.executable, "multi_client.py"],
|
| 64 |
+
stdout=subprocess.PIPE,
|
| 65 |
+
stderr=subprocess.PIPE
|
| 66 |
+
)
|
| 67 |
+
else:
|
| 68 |
+
bot_process = subprocess.Popen(
|
| 69 |
+
[sys.executable, "-m", "pyUltroid"],
|
| 70 |
+
stdout=subprocess.PIPE,
|
| 71 |
+
stderr=subprocess.PIPE
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
print(f"✅ Bot started with PID: {bot_process.pid}")
|
| 75 |
+
return bot_process
|
| 76 |
+
|
| 77 |
+
def start_server(port):
|
| 78 |
+
"""Start the FastAPI server"""
|
| 79 |
+
print(f"🚀 Starting API server on port {port}...")
|
| 80 |
+
|
| 81 |
+
# Import and run server
|
| 82 |
+
os.environ["PORT"] = str(port)
|
| 83 |
+
|
| 84 |
+
# Run server.py directly
|
| 85 |
+
subprocess.run([sys.executable, "server.py"])
|
| 86 |
+
|
| 87 |
+
def main():
|
| 88 |
+
print("=" * 50)
|
| 89 |
+
print("🚀 Ultroid HuggingFace Startup (Python)")
|
| 90 |
+
print("=" * 50)
|
| 91 |
+
|
| 92 |
+
# Change to repo directory
|
| 93 |
+
repo_dir = Path("/app/repo")
|
| 94 |
+
if repo_dir.exists():
|
| 95 |
+
os.chdir(repo_dir)
|
| 96 |
+
print(f"📂 Working directory: {repo_dir}")
|
| 97 |
+
else:
|
| 98 |
+
print(f"❌ Repo directory not found: {repo_dir}")
|
| 99 |
+
sys.exit(1)
|
| 100 |
+
|
| 101 |
+
# Check if port is in use
|
| 102 |
+
if is_port_in_use(PORT):
|
| 103 |
+
print(f"⚠️ Port {PORT} is already in use!")
|
| 104 |
+
print(f"🧹 Attempting to free port {PORT}...")
|
| 105 |
+
|
| 106 |
+
if kill_port_process(PORT):
|
| 107 |
+
time.sleep(2)
|
| 108 |
+
if is_port_in_use(PORT):
|
| 109 |
+
print(f"❌ Failed to free port {PORT}")
|
| 110 |
+
sys.exit(1)
|
| 111 |
+
else:
|
| 112 |
+
print(f"⚠️ Could not kill process on port {PORT}")
|
| 113 |
+
else:
|
| 114 |
+
print(f"✅ Port {PORT} is available")
|
| 115 |
+
|
| 116 |
+
# Start bot in background
|
| 117 |
+
bot_process = start_bot()
|
| 118 |
+
|
| 119 |
+
# Wait for bot to initialize
|
| 120 |
+
print("⏳ Waiting for bot to initialize...")
|
| 121 |
+
time.sleep(5)
|
| 122 |
+
|
| 123 |
+
# Check port one more time before starting server
|
| 124 |
+
if is_port_in_use(PORT):
|
| 125 |
+
print(f"❌ Port {PORT} was taken while bot was starting!")
|
| 126 |
+
kill_port_process(PORT)
|
| 127 |
+
time.sleep(2)
|
| 128 |
+
|
| 129 |
+
# Start server (blocks here)
|
| 130 |
+
print(f"📊 Dashboard will be available on port {PORT}")
|
| 131 |
+
print("=" * 50)
|
| 132 |
+
start_server(PORT)
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
main()
|
startup-hf.sh
DELETED
|
@@ -1,40 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env bash
|
| 2 |
-
# HuggingFace startup script for Ultroid with Dashboard
|
| 3 |
-
|
| 4 |
-
echo "
|
| 5 |
-
┏┳┓╋┏┓╋╋╋╋┏┓┏┓
|
| 6 |
-
┃┃┣┓┃┗┳┳┳━╋╋┛┃
|
| 7 |
-
┃┃┃┗┫┏┫┏┫╋┃┃╋┃
|
| 8 |
-
┗━┻━┻━┻┛┗━┻┻━┛
|
| 9 |
-
|
| 10 |
-
Visit @TheUltroid for updates!!
|
| 11 |
-
Dashboard enabled on port 7860
|
| 12 |
-
"
|
| 13 |
-
|
| 14 |
-
# Fix DNS for HuggingFace
|
| 15 |
-
if [ -w /etc/resolv.conf ]; then
|
| 16 |
-
echo "nameserver 1.1.1.1" > /etc/resolv.conf
|
| 17 |
-
echo "nameserver 1.0.0.1" >> /etc/resolv.conf
|
| 18 |
-
fi
|
| 19 |
-
|
| 20 |
-
# Load environment variables if .env exists
|
| 21 |
-
if [ -f .env ] ; then
|
| 22 |
-
set -o allexport
|
| 23 |
-
source .env
|
| 24 |
-
set +o allexport
|
| 25 |
-
fi
|
| 26 |
-
|
| 27 |
-
# Start the bot in background
|
| 28 |
-
echo "🤖 Starting Ultroid bot..."
|
| 29 |
-
if [ $SESSION1 ] ; then
|
| 30 |
-
python3 multi_client.py &
|
| 31 |
-
else
|
| 32 |
-
python3 -m pyUltroid &
|
| 33 |
-
fi
|
| 34 |
-
|
| 35 |
-
# Wait a bit for bot to initialize
|
| 36 |
-
sleep 5
|
| 37 |
-
|
| 38 |
-
# Start the API server (serves dashboard + API endpoints)
|
| 39 |
-
echo "🚀 Starting API server with dashboard on port ${PORT:-7860}..."
|
| 40 |
-
python3 server.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|