Skip to content

Commit

Permalink
Merge pull request #119 from Oneflow-Inc/support-flask-rest-api
Browse files Browse the repository at this point in the history
Support flask rest api
  • Loading branch information
BBuf authored Mar 8, 2023
2 parents c0fb352 + 43f5d2b commit 5a3c08c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion utils/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def attempt_download(file, repo="Oneflow-Inc/one-yolov5", release="v1.2.0"):
# Attempt file download from GitHub release assets if not found locally. release = 'latest', 'v7.0', etc.
from utils.general import LOGGER

if file.endswith(".pt"):
if str(file).endswith(".pt"):
repo, release = "ultralytics/yolov5", "v7.0"

def github_assets(repository, version="latest"):
Expand Down
2 changes: 1 addition & 1 deletion utils/flask_rest_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[REST](https://en.wikipedia.org/wiki/Representational_state_transfer) [API](https://en.wikipedia.org/wiki/API)s are
commonly used to expose Machine Learning (ML) models to other services. This folder contains an example REST API
created using Flask to expose the YOLOv5s model from [PyTorch Hub](https://pytorch.org/hub/ultralytics_yolov5/).
created using Flask to expose the YOLOv5s model from [OneFlow Hub](https://oneflow.readthedocs.io/en/master/hub.html).

## Requirements

Expand Down
8 changes: 5 additions & 3 deletions utils/flask_rest_api/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

import argparse
import io

import ssl
import oneflow as torch
from flask import Flask, request
from PIL import Image

# https://stackoverflow.com/questions/50236117/scraping-ssl-certificate-verify-failed-error-for-http-en-wikipedia-org
ssl._create_default_https_context = ssl._create_unverified_context

app = Flask(__name__)
models = {}

Expand Down Expand Up @@ -41,8 +44,7 @@ def predict(model):
parser.add_argument("--port", default=5000, type=int, help="port number")
parser.add_argument("--model", nargs="+", default=["yolov5s"], help="model(s) to run, i.e. --model yolov5n yolov5s")
opt = parser.parse_args()

for m in opt.model:
models[m] = torch.hub.load("ultralytics/yolov5", m, force_reload=True, skip_validation=True)
models[m] = torch.hub.load("Oneflow-Inc/one-yolov5", "custom", m, force_reload=True, skip_validation=True)

app.run(host="0.0.0.0", port=opt.port) # debug=True causes Restarting with stat

0 comments on commit 5a3c08c

Please sign in to comment.