Spaces:
Sleeping
Sleeping
saniaE commited on
Commit ·
f7ba59a
1
Parent(s): 69c878c
added api setup
Browse files- Dockerfile +20 -0
- app.py +73 -0
- requirements.txt +15 -0
Dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set up a new user named "user" with user ID 1000
|
| 4 |
+
RUN useradd -m -u 1000 user
|
| 5 |
+
|
| 6 |
+
# Switch to the new user
|
| 7 |
+
USER user
|
| 8 |
+
|
| 9 |
+
# Set environment variables
|
| 10 |
+
ENV HOME=/home/user \
|
| 11 |
+
PATH=/home/user/.local/bin:$PATH
|
| 12 |
+
|
| 13 |
+
WORKDIR $HOME/app
|
| 14 |
+
|
| 15 |
+
COPY --chown=user requirements.txt .
|
| 16 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 17 |
+
|
| 18 |
+
COPY --chown=user . .
|
| 19 |
+
|
| 20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from fastapi import FastAPI, File, UploadFile
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from huggingface_hub import hf_hub_download, login
|
| 8 |
+
|
| 9 |
+
# Import Mask RCNN modules
|
| 10 |
+
from mrcnn.config import Config
|
| 11 |
+
import mrcnn.model as modellib
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# CORS Configuration
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"],
|
| 19 |
+
allow_methods=["*"],
|
| 20 |
+
allow_headers=["*"],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
class PredictionConfig(Config):
|
| 24 |
+
NAME = "petrol_station"
|
| 25 |
+
IMAGES_PER_GPU = 1
|
| 26 |
+
NUM_CLASSES = 1 + 1
|
| 27 |
+
DETECTION_MIN_CONFIDENCE = 0.9
|
| 28 |
+
|
| 29 |
+
# Configuration constants
|
| 30 |
+
REPO_ID = "SaniaE/MRCNN_Petrol_Pump_Segmentation"
|
| 31 |
+
FILENAME = "mask_rcnn_petrol station_0080.h5"
|
| 32 |
+
config = PredictionConfig()
|
| 33 |
+
graph = tf.get_default_graph()
|
| 34 |
+
model_eval = None
|
| 35 |
+
|
| 36 |
+
@app.on_event("startup")
|
| 37 |
+
def load_model():
|
| 38 |
+
global model_eval
|
| 39 |
+
token = os.getenv("HF_Token")
|
| 40 |
+
|
| 41 |
+
if token:
|
| 42 |
+
login(token=token)
|
| 43 |
+
else:
|
| 44 |
+
print("No HF_Token found - attempting public download")
|
| 45 |
+
|
| 46 |
+
print(f"Downloading weights from {REPO_ID}")
|
| 47 |
+
|
| 48 |
+
weights_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, token=token)
|
| 49 |
+
|
| 50 |
+
model_eval = modellib.MaskRCNN(mode="inference", model_dir=".", config=config)
|
| 51 |
+
model_eval.load_weights(weights_path, by_name=True)
|
| 52 |
+
|
| 53 |
+
model_eval.keras_model._make_predict_function()
|
| 54 |
+
print("Model loaded successfully.")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
@app.post("/predict")
|
| 58 |
+
async def predict(file: UploadFile = File(...)):
|
| 59 |
+
contents = await file.read()
|
| 60 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 61 |
+
image_np = np.array(image)
|
| 62 |
+
|
| 63 |
+
global graph
|
| 64 |
+
with graph.as_default():
|
| 65 |
+
results = model_eval.detect([image_np], verbose=0)
|
| 66 |
+
|
| 67 |
+
r = results[0]
|
| 68 |
+
return {
|
| 69 |
+
"rois": r['rois'].tolist(),
|
| 70 |
+
"class_ids": r['class_ids'].tolist(),
|
| 71 |
+
"scores": r['scores'].tolist(),
|
| 72 |
+
"num_detections": len(r['class_ids'])
|
| 73 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
python-multipart
|
| 4 |
+
huggingface-hub
|
| 5 |
+
pandas==1.3.5
|
| 6 |
+
numpy==1.24.3
|
| 7 |
+
matplotlib==3.5.3
|
| 8 |
+
scikit-image==0.19.3
|
| 9 |
+
opencv-python-headless==4.10.0.84
|
| 10 |
+
h5py==2.10.0
|
| 11 |
+
keras==2.2.0
|
| 12 |
+
tensorflow-gpu==1.15.0
|
| 13 |
+
Keras-applications==1.0.7
|
| 14 |
+
protobuf==3.20.3
|
| 15 |
+
Pillow==9.5.0
|