ai-backend / run.py
arinsujal's picture
feat: clean project deployment without cache or binaries
a4a8767
Raw History Blame Contribute Delete
1.97 kB
"""
Unified launcher for AI Quiz System:
Runs FastAPI backend on port 8000 and Streamlit frontend on port 8501.
"""
import subprocess
import sys
import time
import socket
from pathlib import Path
def is_port_open(port: int, host: str = "127.0.0.1") -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.5)
return s.connect_ex((host, port)) == 0
def start_services():
# Ensure we use venv python if present and not already inside venv
python_exe = sys.executable
venv_python = Path(__file__).resolve().parent / "venv" / "Scripts" / "python.exe"
if venv_python.exists() and "venv" not in sys.executable.lower():
python_exe = str(venv_python)
print(f"Using Python: {python_exe}")
print("Starting FastAPI backend on http://127.0.0.1:8000...")
backend_proc = subprocess.Popen(
[python_exe, "-m", "uvicorn", "backend.main:app", "--host", "127.0.0.1", "--port", "8000", "--reload", "--reload-dir", "backend"]
)
print("Waiting for FastAPI backend to initialize embedding models...")
for _ in range(30):
if is_port_open(8000):
print("FastAPI backend is ready!")
break
time.sleep(1)
print("Starting Streamlit frontend on http://127.0.0.1:8501...")
frontend_proc = subprocess.Popen(
[python_exe, "-m", "streamlit", "run", "frontend/app.py", "--server.port", "8501", "--server.headless", "true", "--server.runOnSave", "true"]
)
print("\nAI Quiz System is running!")
print("- FastAPI API Docs: http://127.0.0.1:8000/docs")
print("- Streamlit UI: http://127.0.0.1:8501")
print("\nPress Ctrl+C to terminate both services.\n")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping services...")
backend_proc.terminate()
frontend_proc.terminate()
print("Done.")
if __name__ == "__main__":
start_services()