server.py 2.14 KB
Newer Older
Matteo's avatar
Matteo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import subprocess
import json
import yaml
from fastapi import FastAPI, Response, status

from tapeAudioRestoration import __author__, __version__, __license__, __email__, __maintainer__

app = FastAPI(
    title="Tape Audio Restoration",
    description="""
[![MPAI CAE-ARP](https://img.shields.io/badge/MPAI%20CAE--ARP-gray?style=for-the-badge&logo=AppleMusic&logoColor=cyan&link=https://mpai.community/standards/mpai-cae/about-mpai-cae/)](https://mpai.community/standards/mpai-cae/about-mpai-cae/)

Implements the Technical Specification of [MPAI CAE-ARP](https://mpai.community/standards/mpai-cae/about-mpai-cae/#Figure2) *Tape Audio Restoration* AIM, providing:
* Restored Audio Files;
* Editing List.
""",
    version=__version__,
    contact={
        "name": __maintainer__,
        "email": __email__
    },
    license_info={
        "name": __license__,
    }
)


@app.get("/")
def index():
    return {"endpoints": ["restore", "docs"]}


@app.get("/restore", status_code=200)
async def restore(files_name: str, equalization_w: str, equalization_r: str, speed_w: str, speed_r: str, response: Response, working_path: str | None = None):
    
    process = [
        "python",
        "tapeAudioRestoration.py",
        "--files_name", files_name,
        "--equalization-w", equalization_w,
        "--equalization-r", equalization_r,
        "--speed-w", speed_w,
        "--speed-r", speed_r
        ]

    if working_path is not None:
        process.extend(["--working_path", working_path])
    else:
        with open("config.yaml") as yaml_file:
            config = yaml.safe_load(yaml_file)
        process.extend(["--working_path", config["WorkingPath"]])
        

    cprocess = subprocess.run(process, capture_output=True)

    if cprocess.returncode == 0:
        
        with open('EditingList.json') as json_file:
            editing_list = json.load(json_file)
        return editing_list

    else:
        response.status_code = status.HTTP_412_PRECONDITION_FAILED
        return {"error": {
            "returncode": cprocess.returncode,
            "stdout": cprocess.stdout.decode("utf-8"),
            "stderr": cprocess.stderr.decode("utf-8"),
        }}