File size: 1,924 Bytes
87c3140
c824976
87c3140
b8abf64
524a99c
 
c824976
524a99c
c824976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8abf64
87c3140
 
 
 
 
 
 
 
 
ae215ea
c824976
 
 
 
 
 
 
 
 
ae215ea
c824976
 
 
 
 
 
 
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
import streamlit.web.cli as stcli
import os, sys, socket

# pip install protobuf==3.20.0
# pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117 nope
# pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu118
# pip install protobuf==3.20.0

def find_available_port(start_port, max_attempts=1000):
    port = start_port
    attempts = 0
    while attempts < max_attempts:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            try:
                s.bind(("127.0.0.1", port))
                # If successful, return the current port
                return port
            except socket.error:
                # If the port is in use, increment the port number and try again
                port += 1
                attempts += 1
    # Optional: Return None or raise an exception if no port is found within the attempts limit
    raise ValueError(f"Could not find an available port within {max_attempts} attempts starting from port {start_port}.")


def resolve_path(path):
    resolved_path = os.path.abspath(os.path.join(os.getcwd(), path))
    return resolved_path


if __name__ == "__main__":
    dir_home = os.path.dirname(__file__)

    start_port = 8528
    try:
        free_port = find_available_port(start_port)
        sys.argv = [
            "streamlit",
            "run",
            resolve_path(os.path.join(os.path.dirname(__file__),"app.py")),
            # resolve_path(os.path.join(dir_home,"vouchervision", "VoucherVision_GUI.py")),
            "--global.developmentMode=false",
            # "--server.port=8545",
            f"--server.port={free_port}",
            # Toggle below for HF vs Local
            # "--is_hf=1",
            # "--is_hf=0",
        ]
        sys.exit(stcli.main())
    except ValueError as e:
        print(e)