Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,56 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, UploadFile, File, Depends, HTTPException, status
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
-
import secrets
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
import shutil
|
| 9 |
|
| 10 |
-
# Load env
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
-
app = FastAPI(title="Image Upload API")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
security = HTTPBasic()
|
| 17 |
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME")
|
| 18 |
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
|
| 19 |
|
|
|
|
| 20 |
UPLOAD_DIR = "/tmp/images"
|
| 21 |
Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if not (
|
| 28 |
raise HTTPException(
|
| 29 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 30 |
-
detail="
|
| 31 |
headers={"WWW-Authenticate": "Basic"},
|
| 32 |
)
|
| 33 |
return credentials.username
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
@app.post("/upload")
|
| 37 |
-
def upload_image(
|
| 38 |
-
file: UploadFile = File(...),
|
| 39 |
-
username: str = Depends(verify_credentials)
|
| 40 |
-
):
|
| 41 |
file_ext = Path(file.filename).suffix
|
| 42 |
safe_name = file.filename.replace(" ", "_")
|
| 43 |
-
|
| 44 |
|
| 45 |
-
with open(
|
| 46 |
-
shutil.copyfileobj(file.file,
|
| 47 |
|
| 48 |
return {
|
| 49 |
-
"message": "Image uploaded successfully",
|
| 50 |
-
"filename":
|
| 51 |
"url": f"/images/{safe_name}"
|
| 52 |
}
|
| 53 |
|
| 54 |
-
#
|
| 55 |
app.mount("/images", StaticFiles(directory=UPLOAD_DIR), name="images")
|
| 56 |
-
|
| 57 |
-
# Health check (optional)
|
| 58 |
-
@app.get("/")
|
| 59 |
-
def read_root():
|
| 60 |
-
return {"message": "Welcome to the Hugging Face Image Upload API!"}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import secrets
|
| 3 |
+
import shutil
|
| 4 |
+
from pathlib import Path
|
| 5 |
from fastapi import FastAPI, UploadFile, File, Depends, HTTPException, status
|
| 6 |
from fastapi.staticfiles import StaticFiles
|
| 7 |
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
| 8 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load .env for local dev (ignored by Hugging Face)
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
+
app = FastAPI(title="Image Upload API for Hugging Face Spaces")
|
| 14 |
|
| 15 |
+
# Auth setup
|
| 16 |
security = HTTPBasic()
|
| 17 |
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME")
|
| 18 |
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
|
| 19 |
|
| 20 |
+
# Upload dir
|
| 21 |
UPLOAD_DIR = "/tmp/images"
|
| 22 |
Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)
|
| 23 |
|
| 24 |
+
# Auth dependency
|
| 25 |
+
def verify_admin(credentials: HTTPBasicCredentials = Depends(security)):
|
| 26 |
+
correct_user = secrets.compare_digest(credentials.username, ADMIN_USERNAME)
|
| 27 |
+
correct_pass = secrets.compare_digest(credentials.password, ADMIN_PASSWORD)
|
| 28 |
+
if not (correct_user and correct_pass):
|
| 29 |
raise HTTPException(
|
| 30 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 31 |
+
detail="Invalid credentials",
|
| 32 |
headers={"WWW-Authenticate": "Basic"},
|
| 33 |
)
|
| 34 |
return credentials.username
|
| 35 |
|
| 36 |
+
@app.get("/")
|
| 37 |
+
def read_root():
|
| 38 |
+
return {"message": "Welcome to the Hugging Face Image Upload API!"}
|
| 39 |
+
|
| 40 |
@app.post("/upload")
|
| 41 |
+
def upload_image(file: UploadFile = File(...), username: str = Depends(verify_admin)):
|
|
|
|
|
|
|
|
|
|
| 42 |
file_ext = Path(file.filename).suffix
|
| 43 |
safe_name = file.filename.replace(" ", "_")
|
| 44 |
+
dest_path = Path(UPLOAD_DIR) / safe_name
|
| 45 |
|
| 46 |
+
with open(dest_path, "wb") as out_file:
|
| 47 |
+
shutil.copyfileobj(file.file, out_file)
|
| 48 |
|
| 49 |
return {
|
| 50 |
+
"message": "Image uploaded successfully.",
|
| 51 |
+
"filename": safe_name,
|
| 52 |
"url": f"/images/{safe_name}"
|
| 53 |
}
|
| 54 |
|
| 55 |
+
# Serve images statically
|
| 56 |
app.mount("/images", StaticFiles(directory=UPLOAD_DIR), name="images")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|