-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
63 lines (52 loc) · 2.12 KB
/
Copy pathrun.py
File metadata and controls
63 lines (52 loc) · 2.12 KB
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
import subprocess
import webbrowser
import time
import os
import sys
def cleanup_port(port):
"""
Kills any process running on the specified port.
"""
if os.name == 'nt': # Windows
try:
# Find PID using netstat
output = subprocess.check_output(f"netstat -ano | findstr :{port}", shell=True).decode()
for line in output.splitlines():
if "LISTENING" in line:
pid = line.strip().split()[-1]
print(f"🧹 Killing existing process on port {port} (PID: {pid})...")
subprocess.run(f"taskkill /F /PID {pid}", shell=True, capture_output=True)
except subprocess.CalledProcessError:
# No process found on that port
pass
def main():
print("🚀 Starting DocuScan AI...")
# 1. Ensure port 8000 is free
cleanup_port(8000)
# 1. Install dependencies if needed (optional, for convenience)
# subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
# 2. Get absolute path of backend
backend_path = os.path.join(os.getcwd(), "backend", "main.py")
# 3. Start Backend as a separate process
print("📡 Launching Backend (FastAPI)...")
backend_proc = subprocess.Popen([sys.executable, backend_path])
# 4. Wait for backend to warm up (Models can be slow to load)
print("⏳ Warming up AI models (PaddleOCR)... This might take 10-30 seconds on first run.")
time.sleep(5)
# 5. Open Frontend
from pathlib import Path
frontend_path = os.path.join(os.getcwd(), "frontend", "index.html")
frontend_uri = Path(frontend_path).as_uri()
print(f"🎨 Opening Frontend: {frontend_uri}")
webbrowser.open(frontend_uri)
print("\n✅ System Running!")
print("----------------------------------------")
print("Press Ctrl+C to stop the backend.")
print("----------------------------------------")
try:
backend_proc.wait()
except KeyboardInterrupt:
print("\n🛑 Stopping system...")
backend_proc.terminate()
if __name__ == "__main__":
main()