UPIF-Demo / dev_tools /colab_setup.py
yashsecdev's picture
Refactor: Prepare for Vercel + Colab architecture
1b7ef07
import os
import subprocess
import sys
import time
import threading
def install_dependencies():
print("πŸš€ Installing dependencies...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyngrok", "nest_asyncio", "uvicorn"])
def run_server():
print("πŸ”₯ Starting FastAPI Server...")
# Run server.py using python command to ensure it runs as a script
subprocess.run([sys.executable, "server.py"], check=True)
def start_ngrok():
from pyngrok import ngrok
token = input("πŸ”‘ Enter your Ngrok Authtoken (from https://dashboard.ngrok.com/get-started/your-authtoken): ")
if not token:
print("❌ Error: Ngrok token is required.")
return
ngrok.set_auth_token(token)
# Start tunnel to port 8000
public_url = ngrok.connect(8000).public_url
print(f"\nβœ… \033[92mPublic Backend URL: {public_url}\033[0m")
print("πŸ‘‰ Copy this URL and paste it into the 'Backend Connection' box on your Vercel website.\n")
# Keep functionality alive
try:
# Run server in main thread or subprocess
run_server()
except KeyboardInterrupt:
print("Stopping...")
ngrok.kill()
if __name__ == "__main__":
if not os.path.exists("server.py"):
print("❌ Error: server.py not found. Make sure you are in the root of the cloned repository.")
else:
try:
install_dependencies()
print("βœ… Dependencies installed.")
start_ngrok()
except Exception as e:
print(f"❌ Error: {e}")