WarpWing commited on
Commit
0953f7c
1 Parent(s): 565441d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -1,5 +1,6 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
  from fastapi.responses import FileResponse
 
3
  from pathlib import Path
4
  import psutil
5
  import shutil
@@ -11,8 +12,11 @@ app = FastAPI()
11
  UPLOAD_DIRECTORY = Path("uploads")
12
  UPLOAD_DIRECTORY.mkdir(parents=True, exist_ok=True)
13
 
14
- # Hugging Face Spaces has a 50GB limit
15
- TOTAL_SPACE_GB = 50
 
 
 
16
 
17
  # Helper function to calculate the size of a directory
18
  def get_directory_size(directory: Path) -> int:
@@ -23,16 +27,22 @@ def get_directory_size(directory: Path) -> int:
23
  total_size += os.path.getsize(fp)
24
  return total_size
25
 
 
 
 
 
 
 
26
  # Health check endpoint
27
  @app.get("/health")
28
- def health_check():
29
  return {
30
  "status": "healthy"
31
  }
32
 
33
  # System metrics endpoint: CPU, RAM, and disk usage for uploads folder (in GB and percentage of 50GB limit)
34
  @app.get("/metrics")
35
- def get_metrics():
36
  # CPU percentage (rounded to the nearest whole number)
37
  cpu_percent = round(psutil.cpu_percent(interval=1))
38
 
@@ -44,7 +54,7 @@ def get_metrics():
44
  # Disk stats for uploads directory
45
  uploads_size_bytes = get_directory_size(UPLOAD_DIRECTORY)
46
  uploads_size_gb = uploads_size_bytes / (1024 ** 3)
47
- uploads_percent = (uploads_size_gb / TOTAL_SPACE_GB) * 100
48
 
49
  return {
50
  "cpu_percent": cpu_percent, # Rounded CPU percentage
@@ -56,13 +66,13 @@ def get_metrics():
56
  "disk": {
57
  "uploads_folder_size_gb": round(uploads_size_gb, 2),
58
  "uploads_usage_percent_of_50gb": round(uploads_percent, 2),
59
- "total_space_gb": TOTAL_SPACE_GB
60
  }
61
  }
62
 
63
  # File upload endpoint
64
  @app.post("/uploadfile/")
65
- async def upload_file(file: UploadFile = File(...)):
66
  file_location = UPLOAD_DIRECTORY / file.filename
67
 
68
  with file_location.open("wb") as buffer:
@@ -72,7 +82,7 @@ async def upload_file(file: UploadFile = File(...)):
72
 
73
  # File download endpoint
74
  @app.get("/downloadfile/{filename}")
75
- def download_file(filename: str):
76
  file_location = UPLOAD_DIRECTORY / filename
77
 
78
  if not file_location.exists():
@@ -82,6 +92,6 @@ def download_file(filename: str):
82
 
83
  # Show current files endpoint
84
  @app.get("/files/")
85
- def list_files():
86
  files = [f for f in os.listdir(UPLOAD_DIRECTORY) if os.path.isfile(UPLOAD_DIRECTORY / f)]
87
  return {"files": files}
 
1
+ from fastapi import FastAPI, UploadFile, File, HTTPException, Depends
2
  from fastapi.responses import FileResponse
3
+ from fastapi.security import OAuth2PasswordBearer
4
  from pathlib import Path
5
  import psutil
6
  import shutil
 
12
  UPLOAD_DIRECTORY = Path("uploads")
13
  UPLOAD_DIRECTORY.mkdir(parents=True, exist_ok=True)
14
 
15
+ # OAuth2 scheme for token-based authorization
16
+ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
17
+
18
+ # Hugging Face secret token from environment variables
19
+ VALID_TOKEN = os.environ.get("SECRET_TOKEN")
20
 
21
  # Helper function to calculate the size of a directory
22
  def get_directory_size(directory: Path) -> int:
 
27
  total_size += os.path.getsize(fp)
28
  return total_size
29
 
30
+ # Dependency to verify the token
31
+ def get_current_token(token: str = Depends(oauth2_scheme)):
32
+ if token != VALID_TOKEN:
33
+ raise HTTPException(status_code=401, detail="Invalid token")
34
+ return token
35
+
36
  # Health check endpoint
37
  @app.get("/health")
38
+ def health_check(token: str = Depends(get_current_token)):
39
  return {
40
  "status": "healthy"
41
  }
42
 
43
  # System metrics endpoint: CPU, RAM, and disk usage for uploads folder (in GB and percentage of 50GB limit)
44
  @app.get("/metrics")
45
+ def get_metrics(token: str = Depends(get_current_token)):
46
  # CPU percentage (rounded to the nearest whole number)
47
  cpu_percent = round(psutil.cpu_percent(interval=1))
48
 
 
54
  # Disk stats for uploads directory
55
  uploads_size_bytes = get_directory_size(UPLOAD_DIRECTORY)
56
  uploads_size_gb = uploads_size_bytes / (1024 ** 3)
57
+ uploads_percent = (uploads_size_gb / 50) * 100 # Assuming 50GB total space limit
58
 
59
  return {
60
  "cpu_percent": cpu_percent, # Rounded CPU percentage
 
66
  "disk": {
67
  "uploads_folder_size_gb": round(uploads_size_gb, 2),
68
  "uploads_usage_percent_of_50gb": round(uploads_percent, 2),
69
+ "total_space_gb": 50
70
  }
71
  }
72
 
73
  # File upload endpoint
74
  @app.post("/uploadfile/")
75
+ async def upload_file(file: UploadFile = File(...), token: str = Depends(get_current_token)):
76
  file_location = UPLOAD_DIRECTORY / file.filename
77
 
78
  with file_location.open("wb") as buffer:
 
82
 
83
  # File download endpoint
84
  @app.get("/downloadfile/{filename}")
85
+ def download_file(filename: str, token: str = Depends(get_current_token)):
86
  file_location = UPLOAD_DIRECTORY / filename
87
 
88
  if not file_location.exists():
 
92
 
93
  # Show current files endpoint
94
  @app.get("/files/")
95
+ def list_files(token: str = Depends(get_current_token)):
96
  files = [f for f in os.listdir(UPLOAD_DIRECTORY) if os.path.isfile(UPLOAD_DIRECTORY / f)]
97
  return {"files": files}