@@ -18,42 +18,6 @@ def get_number_of_workers(workers=None):
1818 return workers
1919
2020
21- def replace_port (static_files_dir , host , port ):
22- # Load index.html from frontend directory
23- # In it there is a script tag that sets the base url
24- # like so setItem("port", "http://localhost:7860")
25- # localhost could be anything so we need to verify for string
26- # we need to set the base url to the port that the server is running on
27- # so that the frontend can make requests to the backend
28- # This is a hacky way to do it, but it works
29-
30- # Check if the host is http or https
31- logger .info (f"host: { host } " )
32- logger .info (f"port: { port } " )
33- url = f"{ host } :{ port } " if "http" in host else f"http://{ host } :{ port } "
34- logger .info (f"url: { url } " )
35- new_string = f'setItem("port","{ url } ")'
36-
37- with open (static_files_dir / "index.html" , "r" ) as f :
38- index_html = f .read ()
39- # using regex to replace the port
40- index_html = re .sub (
41- r"setItem\(\"port\",.*\)" ,
42- new_string ,
43- index_html ,
44- )
45- with open (static_files_dir / "index.html" , "w" ) as f :
46- f .write (index_html )
47- # Verify that the port was replaced
48- with open (static_files_dir / "index.html" , "r" ) as f :
49- index_html = f .read ()
50- if new_string not in index_html :
51- raise ValueError (
52- "The port was not replaced in index.html. "
53- "Please check the regex in main.py"
54- )
55-
56-
5721def serve (
5822 host : str = "127.0.0.1" , workers : int = 1 , timeout : int = 60 , port : int = 7860
5923):
@@ -73,9 +37,6 @@ def serve(
7337 "timeout" : timeout ,
7438 }
7539
76- # Replace the port in index.html
77- replace_port (static_files_dir , host , port )
78-
7940 if platform .system () in ["Darwin" , "Windows" ]:
8041 # Run using uvicorn on MacOS and Windows
8142 # Windows doesn't support gunicorn
0 commit comments