Spaces:
Running
Running
Code improvement
Browse files
run.py
CHANGED
@@ -1,48 +1,58 @@
|
|
1 |
-
import subprocess
|
2 |
-
import os
|
3 |
-
from server.app import app
|
4 |
-
from server.website import Website
|
5 |
-
from server.backend import Backend_Api
|
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 |
-
app
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import os
|
3 |
+
from server.app import app
|
4 |
+
from server.website import Website
|
5 |
+
from server.backend import Backend_Api
|
6 |
+
from json import load
|
7 |
+
|
8 |
+
|
9 |
+
def start_gpt4api():
|
10 |
+
# Get the GPT-4 API directory path
|
11 |
+
api_directory = os.path.join(os.path.dirname(
|
12 |
+
os.path.abspath(__file__)), "apiGPT4")
|
13 |
+
|
14 |
+
# Install the GPT-4 dependencies using yarn
|
15 |
+
install_dependencies = subprocess.Popen(
|
16 |
+
["yarn"], cwd=api_directory, shell=True)
|
17 |
+
install_dependencies.wait()
|
18 |
+
|
19 |
+
# Start the GPT-4 process using yarn start
|
20 |
+
api_process = subprocess.Popen(
|
21 |
+
["yarn", "start"], cwd=api_directory, shell=True)
|
22 |
+
|
23 |
+
return api_process
|
24 |
+
|
25 |
+
|
26 |
+
if __name__ == '__main__':
|
27 |
+
# Run GPT-4 API
|
28 |
+
gpt4api_process = start_gpt4api()
|
29 |
+
|
30 |
+
# Load configuration from config.json
|
31 |
+
config = load(open('config.json', 'r'))
|
32 |
+
site_config = config['site_config']
|
33 |
+
|
34 |
+
# Set up the website routes
|
35 |
+
site = Website(app)
|
36 |
+
for route in site.routes:
|
37 |
+
app.add_url_rule(
|
38 |
+
route,
|
39 |
+
view_func=site.routes[route]['function'],
|
40 |
+
methods=site.routes[route]['methods'],
|
41 |
+
)
|
42 |
+
|
43 |
+
# Set up the backend API routes
|
44 |
+
backend_api = Backend_Api(app, config)
|
45 |
+
for route in backend_api.routes:
|
46 |
+
app.add_url_rule(
|
47 |
+
route,
|
48 |
+
view_func=backend_api.routes[route]['function'],
|
49 |
+
methods=backend_api.routes[route]['methods'],
|
50 |
+
)
|
51 |
+
|
52 |
+
# Run the Flask server
|
53 |
+
print(f"Running on port {site_config['port']}")
|
54 |
+
app.run(**site_config)
|
55 |
+
print(f"Closing port {site_config['port']}")
|
56 |
+
|
57 |
+
# Terminate the GPT-4 API process when the Flask server is closed
|
58 |
+
gpt4api_process.terminate()
|