Jiang commited on
Commit
81053f9
·
unverified ·
2 Parent(s): affa358 c79d28c

Merge pull request #1 from east-and-west-magic/downloader

Browse files
Files changed (4) hide show
  1. Dockerfile +18 -9
  2. main.py +32 -13
  3. page.py +23 -17
  4. utils.py +7 -0
Dockerfile CHANGED
@@ -1,16 +1,25 @@
1
- FROM python:3.11
 
2
 
3
- WORKDIR /code
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  COPY ./requirements.txt /code/requirements.txt
6
- RUN python3 -m pip install --no-cache-dir --upgrade pip
7
- RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
  COPY . .
10
 
11
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
12
-
13
- RUN mkdir /.cache
14
- RUN chmod 777 /.cache
15
- RUN mkdir .chroma
16
- RUN chmod 777 .chroma
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
 
4
+ FROM python:3.10.10
5
+
6
+ # Set up a new user named "user" with user ID 1000
7
+ RUN useradd -m -u 1000 user
8
+
9
+ # Switch to the "user" user
10
+ USER user
11
+
12
+ # Set home to the user's home directory
13
+ ENV HOME=/home/user \
14
+ PATH=/home/user/.local/bin:$PATH
15
+
16
+ # Set the working directory to the user's home directory
17
+ WORKDIR $HOME/code
18
 
19
  COPY ./requirements.txt /code/requirements.txt
20
+
21
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
22
 
23
  COPY . .
24
 
25
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
main.py CHANGED
@@ -1,24 +1,43 @@
1
- from fastapi import FastAPI
2
- from pydantic import BaseModel
3
  from panel.io.fastapi import add_applications
4
- from page import page
 
5
  import logging
6
 
7
- app = FastAPI()
8
- add_applications(page, app=app, title="Log Displayer")
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- class Log(BaseModel):
11
- message: str
12
 
13
  @app.post("/app")
14
- async def log_app(log: Log):
 
15
  logger = logging.getLogger("APP")
16
- logger.debug(log.message)
17
- return {"details": "Logged Successfully. Please check the root page."}
18
 
19
 
20
  @app.post("/game")
21
- async def log_game(log: Log):
 
22
  logger = logging.getLogger("Game")
23
- logger.debug(log.message)
24
- return {"details": "Logged Successfully. Please check the root page."}
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Body
2
+ from fastapi.middleware.cors import CORSMiddleware
3
  from panel.io.fastapi import add_applications
4
+ import page
5
+ from utils import beijing
6
  import logging
7
 
8
+ app = FastAPI(
9
+ title=f"Log Displayer",
10
+ description=f"Updated at {beijing()}",
11
+ )
12
+
13
+ print("Adding middlewares...", end="")
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"],
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+ print("Done\n")
22
+ add_applications(page.page, app=app, title="Log Displayer")
23
 
 
 
24
 
25
  @app.post("/app")
26
+ async def log_app(message: str = Body(..., embed=True)):
27
+ print(f"[APP] {message}")
28
  logger = logging.getLogger("APP")
29
+ logger.debug(message)
30
+ return True
31
 
32
 
33
  @app.post("/game")
34
+ async def log_game(message: str = Body(..., embed=True)):
35
+ print(f"[GAME] {message}")
36
  logger = logging.getLogger("Game")
37
+ logger.debug(message)
38
+ return True
39
+
40
+
41
+ @app.get("/healthcheck")
42
+ async def health_check():
43
+ return True
page.py CHANGED
@@ -1,16 +1,14 @@
1
  import panel as pn
2
  import logging
 
3
 
4
 
5
  tab_console_names = ["APP", "Game"]
6
- pn.extension("terminal")
7
  tabs = []
8
  for name in tab_console_names:
9
- terminal = pn.widgets.Terminal(
10
- f"Console for {name}\n=====================================================\n\n",
11
- height=600,
12
- sizing_mode="stretch_width",
13
- )
14
  logger = logging.getLogger(name)
15
  logger.setLevel(logging.DEBUG)
16
  stream_handler = logging.StreamHandler(terminal)
@@ -19,15 +17,23 @@ for name in tab_console_names:
19
  stream_handler.setFormatter(formatter)
20
  stream_handler.setLevel(logging.DEBUG)
21
  logger.addHandler(stream_handler)
22
- tabs.append((name, terminal))
23
 
24
- terminal_tabs = pn.Tabs(*tabs)
25
- main = pn.WidgetBox(terminal_tabs)
26
- title = "Log Displayer"
27
- page = pn.Column(
28
- pn.pane.HTML(
29
- f"""<h1 style="text-align: center; font-weight: bold;">{title}</h1>""",
30
- sizing_mode="stretch_width",
31
- ),
32
- terminal_tabs,
33
- )
 
 
 
 
 
 
 
 
 
 
1
  import panel as pn
2
  import logging
3
+ from pathlib import Path
4
 
5
 
6
  tab_console_names = ["APP", "Game"]
7
+ pn.extension("terminal") # type: ignore
8
  tabs = []
9
  for name in tab_console_names:
10
+ terminal = pn.widgets.Terminal(height=600, sizing_mode="stretch_width")
11
+
 
 
 
12
  logger = logging.getLogger(name)
13
  logger.setLevel(logging.DEBUG)
14
  stream_handler = logging.StreamHandler(terminal)
 
17
  stream_handler.setFormatter(formatter)
18
  stream_handler.setLevel(logging.DEBUG)
19
  logger.addHandler(stream_handler)
 
20
 
21
+ log_path = Path(f"data/logs/{name.lower()}.log")
22
+ log_path.parent.mkdir(parents=True, exist_ok=True)
23
+ file_handler = logging.FileHandler(log_path)
24
+ file_handler.setFormatter(formatter)
25
+ logger.addHandler(file_handler)
26
+ downloader = pn.widgets.FileDownload(log_path)
27
+ tabs.append(
28
+ (
29
+ name,
30
+ pn.Column(
31
+ pn.Row(terminal, align="center"),
32
+ pn.Row(downloader, align="center"),
33
+ ),
34
+ )
35
+ )
36
+
37
+
38
+ def page():
39
+ return pn.Column(pn.Tabs(*tabs))
utils.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ from zoneinfo import ZoneInfo
3
+
4
+
5
+ def beijing():
6
+ """get beijing time"""
7
+ return datetime.now(tz=ZoneInfo("Asia/Shanghai"))