Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyily committed Jul 24, 2023
1 parent 8470775 commit d356a9e
Show file tree
Hide file tree
Showing 14 changed files with 18,274 additions and 0 deletions.
192 changes: 192 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/


# Node
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/react_app/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Custom
*.wav
/backend_temp
/webui
/hppnet/models
102 changes: 102 additions & 0 deletions backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
from typing import Union
import uvicorn
from fastapi import FastAPI,UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse

from pydantic import BaseModel

from hppnet.hppnet_onnx import HPPNetNumpyDecoder,HPPNetOnnx

class HppnetInferTask(BaseModel):
file_path:Union[str, None] = None
model_name:str
device:str
onset_t:float
frame_t:float
gpu_id:Union[str, None] = None

app = FastAPI()
app.mount("/static", StaticFiles(directory="./webui/static"), name="static")
app.mount("/assets", StaticFiles(directory="./webui/assets"), name="assets")
origins = [
"http://localhost:3000",
]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

hppnet_onnx = None # type: HPPNetOnnx
hppnet_onnx_state = {}
hppnet_decoder = HPPNetNumpyDecoder()

@app.get("/")
def root():
with open('./webui/index.html','r',encoding='utf-8') as f:
html_content = f.read()
return HTMLResponse(content=html_content, status_code=200)

def check_hppnet_onnx_state_change(model_name,device,gpu_id):
new_state = {"model":model_name,"device":device,"gpu_id":gpu_id}
return not bool(new_state == hppnet_onnx_state)

def init_hppnet_onnx(onset_onnx,frame_onnx,device,gpu_id):
global hppnet_onnx
if device=='gpu':
provider_options = [{'device_id': gpu_id}] if gpu_id else None
hppnet_onnx = HPPNetOnnx(onset_onnx,frame_onnx,provider_options=provider_options)
else:
hppnet_onnx = HPPNetOnnx(onset_onnx,frame_onnx,providers=['CPUExecutionProvider'])

@app.get('/hppnet_models')
def get_available_hppnet_models():
return {"models":os.listdir('./hppnet/models')}

@app.post('/infer_hppnet')
def run_hppnet_infer(hppnet_infer_task:HppnetInferTask):
file_path=hppnet_infer_task.file_path if hppnet_infer_task.file_path else './backend_temp/temp.bin'
model_name=hppnet_infer_task.model_name
device=hppnet_infer_task.device
onset_t=hppnet_infer_task.onset_t
frame_t=hppnet_infer_task.frame_t
gpu_id=hppnet_infer_task.gpu_id
print(file_path)
global hppnet_onnx
onset_onnx = f'./hppnet/models/{model_name}/onset_subnet.onnx'
frame_onnx = f'./hppnet/models/{model_name}/frame_subnet.onnx'
output_mid = './backend_temp/temp.mid'
# Check if hppnet_onnx is initialised
if hppnet_onnx:
if check_hppnet_onnx_state_change(model_name,device,gpu_id):
del hppnet_onnx
init_hppnet_onnx(onset_onnx,frame_onnx,device,gpu_id)
else:
init_hppnet_onnx(onset_onnx,frame_onnx,device,gpu_id)
hppnet_onnx_state['model'] = model_name
hppnet_onnx_state['device'] = device
hppnet_onnx_state['gpu_id'] = gpu_id
# inference
hppnet_onnx.load_model()
onset,frame,velocity = hppnet_onnx.inference_audio_file(file_path)
hppnet_decoder.export_infer_result_to_midi(onset,frame,velocity,output_mid,onset_t,frame_t)
return FileResponse(output_mid,media_type='blob')

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile):
contents = await file.read()
f = open('./backend_temp/temp.bin','wb')
f.write(contents)
f.close()
return {"filename": file.filename}


if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Loading

0 comments on commit d356a9e

Please sign in to comment.