Spaces:
Sleeping
Sleeping
Commit ·
1455613
0
Parent(s):
initial commit
Browse files- .dockerignore +16 -0
- Dockerfile +22 -0
- __pycache__/main.cpython-312.pyc +0 -0
- docker-compose.yml +9 -0
- main.py +69 -0
- requirements.txt +1 -0
.dockerignore
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.pyo
|
| 4 |
+
*.pyd
|
| 5 |
+
*.sqlite3
|
| 6 |
+
.env
|
| 7 |
+
.venv/
|
| 8 |
+
venv/
|
| 9 |
+
.env.*
|
| 10 |
+
.git/
|
| 11 |
+
.gitignore
|
| 12 |
+
.idea/
|
| 13 |
+
.vscode/
|
| 14 |
+
dist/
|
| 15 |
+
build/
|
| 16 |
+
uploads/
|
Dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
+
PYTHONUNBUFFERED=1
|
| 5 |
+
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
# Install dependencies first for better layer caching before copying the rest of the code
|
| 9 |
+
COPY requirements.txt .
|
| 10 |
+
RUN python -m pip install --upgrade pip \
|
| 11 |
+
&& pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
COPY . .
|
| 14 |
+
|
| 15 |
+
# Create a non-root user for security
|
| 16 |
+
RUN useradd -m appuser
|
| 17 |
+
USER appuser
|
| 18 |
+
|
| 19 |
+
# docs only for port app listens on
|
| 20 |
+
EXPOSE 7860
|
| 21 |
+
|
| 22 |
+
CMD ["bash", "-lc", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-7860}"]
|
__pycache__/main.cpython-312.pyc
ADDED
|
Binary file (2.97 kB). View file
|
|
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: '3.0'
|
| 2 |
+
services:
|
| 3 |
+
app:
|
| 4 |
+
build: .
|
| 5 |
+
ports:
|
| 6 |
+
- 8000:8000
|
| 7 |
+
volumes:
|
| 8 |
+
- .:/app
|
| 9 |
+
command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
main.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from enum import Enum
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Annotated
|
| 5 |
+
from fastapi import FastAPI, File, UploadFile
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import shutil, uuid
|
| 8 |
+
import uvicorn
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
@app.get("/healthz")
|
| 14 |
+
def health():
|
| 15 |
+
return {"ok": True}
|
| 16 |
+
|
| 17 |
+
class ModelName(str, Enum):
|
| 18 |
+
alexnet = "alexnet"
|
| 19 |
+
resnet = "resnet"
|
| 20 |
+
lenet = "lenet"
|
| 21 |
+
|
| 22 |
+
@app.get("/models/{model_name}")
|
| 23 |
+
async def get_model(model_name: ModelName):
|
| 24 |
+
if model_name is ModelName.alexnet:
|
| 25 |
+
return {"model_name": model_name, "message": "Deep Learning FTW!"}
|
| 26 |
+
if model_name.value == "lenet":
|
| 27 |
+
return {"model_name": model_name, "message": "LeCNN all the images"}
|
| 28 |
+
return {"model_name": model_name, "message": "Have some residuals"}
|
| 29 |
+
|
| 30 |
+
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
|
| 31 |
+
|
| 32 |
+
class Item(BaseModel):
|
| 33 |
+
name: str
|
| 34 |
+
description: str | None = None
|
| 35 |
+
price: float
|
| 36 |
+
tax: float | None = None
|
| 37 |
+
|
| 38 |
+
@app.post("/items/")
|
| 39 |
+
async def create_item(item: Item):
|
| 40 |
+
item_dict = item.model_dump()
|
| 41 |
+
if item.tax is not None:
|
| 42 |
+
price_with_tax = item.price + item.tax
|
| 43 |
+
item_dict.update({"price_with_tax": price_with_tax})
|
| 44 |
+
return item_dict
|
| 45 |
+
|
| 46 |
+
@app.post("/files/")
|
| 47 |
+
async def create_file(file: Annotated[bytes, File()]):
|
| 48 |
+
return {"file_size": len(file)}
|
| 49 |
+
|
| 50 |
+
UPLOAD_DIR = Path("/tmp/uploads")
|
| 51 |
+
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@app.post("/uploadfile/")
|
| 55 |
+
async def create_upload_file(file: UploadFile = File(...)):
|
| 56 |
+
# avoid trusting user-supplied filenames
|
| 57 |
+
ext = (Path(file.filename).suffix or ".bin").lower()
|
| 58 |
+
safe_name = f"{uuid.uuid4().hex}{ext}"
|
| 59 |
+
dest = UPLOAD_DIR / safe_name
|
| 60 |
+
|
| 61 |
+
with dest.open("wb") as f:
|
| 62 |
+
shutil.copyfileobj(file.file, f)
|
| 63 |
+
|
| 64 |
+
return {"saved_as": str(dest)}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
port = int(os.getenv("PORT", "7860"))
|
| 69 |
+
uvicorn.run(app, host="0.0.0.0", port=port, reload=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
fastapi[standard]
|