Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from transformers import YolosForObjectDetection, YolosImageProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Load the YOLOv5 model and processor from Hugging Face
|
| 10 |
+
model = YolosForObjectDetection.from_pretrained('ultralytics/yolov5')
|
| 11 |
+
processor = YolosImageProcessor.from_pretrained('ultralytics/yolov5')
|
| 12 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 13 |
+
model.to(device)
|
| 14 |
+
model.eval()
|
| 15 |
+
|
| 16 |
+
@app.post("/predict/")
|
| 17 |
+
async def predict(file: UploadFile = File(...)):
|
| 18 |
+
contents = await file.read()
|
| 19 |
+
image = Image.open(io.BytesIO(contents))
|
| 20 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 21 |
+
outputs = model(**inputs)
|
| 22 |
+
return outputs
|