Duibonduil commited on
Commit
34b5cbd
·
verified ·
1 Parent(s): 14c5b3b

Upload 3 files

Browse files
aworld/cmd/web/__init__.sh ADDED
File without changes
aworld/cmd/web/api_server.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ from fastapi import FastAPI
3
+ import uvicorn
4
+
5
+
6
+ logger = logging.getLogger(__name__)
7
+
8
+ app = FastAPI()
9
+
10
+ from .routers import chats,workspaces,sessions
11
+
12
+ app.include_router(chats.router, prefix=chats.prefix)
13
+ app.include_router(workspaces.router, prefix=workspaces.prefix)
14
+ app.include_router(sessions.router, prefix=sessions.prefix)
15
+
16
+ def run_server(port, args=None, **kwargs):
17
+ logger.info(f"Running API server on port {port}")
18
+ uvicorn.run(
19
+ app,
20
+ host="0.0.0.0",
21
+ port=port,
22
+ )
aworld/cmd/web/web_server.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import logging
3
+ from fastapi import FastAPI, Request, Response
4
+ from fastapi.responses import RedirectResponse
5
+ import uvicorn
6
+ import os
7
+ import subprocess
8
+ from fastapi.staticfiles import StaticFiles
9
+ from starlette.middleware.base import BaseHTTPMiddleware
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+ app = FastAPI()
14
+
15
+
16
+ @app.get("/")
17
+ async def root():
18
+ return RedirectResponse("/index.html")
19
+
20
+
21
+ def get_user_id_from_jwt(request: Request) -> str:
22
+ return "test_user_1"
23
+
24
+
25
+ from .routers import chats, workspaces, sessions, traces # noqa
26
+
27
+ app.include_router(chats.router, prefix=chats.prefix)
28
+ app.include_router(workspaces.router, prefix=workspaces.prefix)
29
+ app.include_router(sessions.router, prefix=sessions.prefix)
30
+ app.include_router(traces.router, prefix=traces.prefix)
31
+
32
+
33
+ def build_webui(force_rebuild: bool = False) -> str:
34
+ webui_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "webui")
35
+ static_path = os.path.join(webui_path, "dist")
36
+
37
+ if (not os.path.exists(static_path)) or force_rebuild:
38
+ logger.warning(f"Build WebUI at {webui_path}")
39
+
40
+ p = subprocess.Popen(
41
+ ["sh", "-c", "npm install && npm run build"],
42
+ cwd=webui_path,
43
+ )
44
+ p.wait()
45
+ if p.returncode != 0:
46
+ raise Exception(f"Failed to build WebUI, error code: {p.returncode}")
47
+ else:
48
+ logger.info("WebUI build successfully")
49
+
50
+ return static_path
51
+
52
+
53
+ static_path = build_webui(force_rebuild=True)
54
+ logger.info(f"Mounting static files from {static_path}")
55
+ app.mount("/", StaticFiles(directory=static_path, html=True), name="static")
56
+
57
+
58
+ class TimeoutMiddleware(BaseHTTPMiddleware):
59
+ def __init__(self, app, timeout: int = 300):
60
+ super().__init__(app)
61
+ self.timeout = timeout
62
+
63
+ async def dispatch(self, request: Request, call_next):
64
+ try:
65
+ return await asyncio.wait_for(call_next(request), timeout=self.timeout)
66
+ except asyncio.TimeoutError:
67
+ return Response("Request timeout", status_code=408)
68
+
69
+
70
+ app.add_middleware(TimeoutMiddleware, timeout=300)
71
+
72
+
73
+ def run_server(port, args=None, **kwargs):
74
+ logger.info(f"Running Web server on port {port}")
75
+ uvicorn.run(
76
+ app,
77
+ host="0.0.0.0",
78
+ port=port,
79
+ )