OpenDBM REST API
Summary
Inside the OpenDBM repository, there is a folder named rest_api. This folder means to showcase that you can leverage OpenDBM library in API service or Web application, which this folder will showcase the former.
The OpenDBM API designed with OpenAPI 2.0 to standardize API definition for programmatic usage, better developer experience and documentation. It will also protected by standard security protocol.
How to use
To use the Rest API is very straighforward
- Go to the
rest_api
directory - Install the neccessary dependencies
pip install -r requirements.txt
docker pull opendbmteam/dbm-openface
- Then you can use Make syntax to run it
make dev
Wait until the rest api ready to serve requests
INFO: Will watch for changes in these directories: ['/Users/smarty_not_dummy__user/open_dbm/rest_api/app']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [80258] using StatReload
INFO: Started server process [80275]
- You can test using the OpenAPI specs like mentioned in here. You can open
http://127.0.0.1:8000/docs
orhttp://127.0.0.1:8000/redoc
. - Before you start using the APIs, you need to create your credentials for authentication. Alternatively, you can try to use username 'aicure' and the password is 'opendbm'.
You can skip below steps if you want to use 'aicure' credential
- To create your own username, first you generate your own password by following these steps:
cd rest_api/app
python
Python 3.7.13 (default, Mar 28 2022, 07:24:34)
[Clang 12.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
Then you can start type your own password using this python command
from services.auth.auth import get_password_hash
print(get_password_hash('test'))
$2b$12$qYnAxjQd..feDyPBYa/mDuRrH.PkcpNHJk3F.l/c0h3l/1NcZup7O
Save the password in the last line and your preferred username in app/services/auth/auth.py
def get_db():
fake_users_db = {
"your_new_username": {
"username": "your_new_username",
"full_name": "New Full name",
"email": "new email",
"hashed_password": "$2b$12$qYnAxjQd..feDyPBYa/mDuRrH.PkcpNHJk3F.l/c0h3l/1NcZup7O ",
"disabled": False,
},
"aicure": {
"username": "aicure",
"full_name": "AiCure OpenDBM",
"email": "opendbm@aicure.com",
"hashed_password": "$2b$12$k4R5SPuHkjFKBsQV5gAHl.e/BlxrX2z1H3vxiB9uGtaDZLFXjggCm",
"disabled": False,
},
"alice": {
"username": "alice",
"full_name": "Alice Wonderson",
"email": "alice@aicure.com",
"hashed_password": "fakehashedsecret2",
"disabled": True,
},
}
return fake_users_db
- After that you can login using either of these icons, or using the curl alternative
- API Docs
- cUrl
curl 'http://127.0.0.1:8000/odbm/v1/login' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Authorization: Basic Og==' \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-raw 'grant_type=password&username=aicure&password=opendbm' \
- Start with uploading the files, put the "local" string value in the
platform
parameter, then save the upload file name.
- API Docs
- cUrl
curl 'http://127.0.0.1:8000/odbm/v1/upload?file_name=facial_speech_verbal_video_test.mp4&file_extension=mp4&platform=local' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhaWN1cmUiLCJleHAiOjE2NjI5MDM4MjB9.oYW3z44NdcBLZswxy4Vt1S2r92wYmus59NWnZYDr2CA' \
-H 'Content-Type: multipart/form-data; ' \
-H 'accept: application/json' \
--data-raw $'Content-Disposition: form-data; name="file"; filename="facial_speech_verbal_video_test.mp4" Content-Type: video/mp4 \
--compressed
The API response will be
{
"info": "file 'facial_speech_verbal_video_test.mp4' saved at 'files/facial_speech_verbal_video_test.mp4'"
}
Save the file location returned somewhere files/facial_speech_verbal_video_test.mp4
- Then you can use any API you want, insert the previous upload file url into
file_url
parameter. Then you can specify variables you want to get. For example, if you use the/video/acoustic
API, you can specify the request body like
[
"audio_intensity",
"pitch_frequency"
]
- API Docs
- cUrl
curl 'http://127.0.0.1:8000/odbm/v1/video/acoustic?file_url=files%2Ffacial_speech_verbal_video_test.mp4&platform=local' \
-H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhaWN1cmUiLCJleHAiOjE2NjI5MDkwMzd9.RQgtXhPCcgzctfuNg2GbByR9OMdr1vWTerl1iKPWqfk' \
-H 'Content-Type: application/json' \
-H 'accept: application/json' \
--data-raw $'[\n "audio_intensity",\n "pitch_frequency"\n]' \
--compressed
After you execute the API, you will get the zip file, with the csv results compressed inside that file
You can check more details on rest_api usage in
rest_api/README.md
Technology Stack
The OpenDBM API will use FastAPI framework. The main reason is because the asynchronous behaviour of FastAPI will works perfectly to complex and high performance methods like data upload, data pre / processing and other possible long operations.
We also use Uvicorn is an ASGI web server implementation for Python. Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all async frameworks. Uvicorn currently supports HTTP/1.1 and WebSockets.
API Documentations
The Rest API documentations can be found when you run the the app. Change the relative path to /docs or /redoc