WarpWingHF commited on
Commit
565441d
1 Parent(s): 78a4f44
Files changed (3) hide show
  1. Dockerfile +20 -0
  2. app.py +87 -0
  3. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ # Copy the requirements file and install dependencies
10
+ COPY --chown=user ./requirements.txt requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
12
+
13
+ # Copy the application files
14
+ COPY --chown=user . /app
15
+
16
+ # Expose the required port
17
+ EXPOSE 7860
18
+
19
+ # Command to run the app
20
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
6
+ import os
7
+
8
+ app = FastAPI()
9
+
10
+ # Directory where files will be uploaded
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:
19
+ total_size = 0
20
+ for dirpath, dirnames, filenames in os.walk(directory):
21
+ for f in filenames:
22
+ fp = os.path.join(dirpath, f)
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
+
39
+ # Memory stats in GB
40
+ memory = psutil.virtual_memory()
41
+ memory_total_gb = memory.total / (1024 ** 3)
42
+ memory_available_gb = memory.available / (1024 ** 3)
43
+
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
51
+ "memory": {
52
+ "total_gb": round(memory_total_gb, 2),
53
+ "available_gb": round(memory_available_gb, 2),
54
+ "percent": memory.percent
55
+ },
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:
69
+ shutil.copyfileobj(file.file, buffer)
70
+
71
+ return {"info": f"file '{file.filename}' saved at '{file_location}'"}
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():
79
+ raise HTTPException(status_code=404, detail="File not found")
80
+
81
+ return FileResponse(path=file_location, filename=filename)
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}
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ psutil
4
+ python-dotenv
5
+ python-multipart