server.py 2.34 KB
Newer Older
Matteo's avatar
Matteo committed
1
2
3
4
import subprocess
import json
from fastapi import FastAPI, Response, status

Matteo's avatar
Matteo committed
5
6
from mpai_cae_arp.types.schema import Info
from mpai_cae_arp.files import get_file_content
Matteo's avatar
Matteo committed
7

Matteo's avatar
Matteo committed
8
9
10
from src.tapeAudioRestoration import __author__, __version__, __license__, __email__, __maintainer__

info = Info(
Matteo's avatar
Matteo committed
11
12
13
14
15
16
17
18
19
20
21
22
23
    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__
    },
Matteo's avatar
Matteo committed
24
    license_info={"name": __license__, "url": "https://www.gnu.org/licenses/gpl-3.0.html"}
Matteo's avatar
Matteo committed
25
26
27
)


Matteo's avatar
Matteo committed
28
29
30
app = FastAPI(**(json.loads(info.json())))


Matteo's avatar
Matteo committed
31
@app.get("/")
Matteo's avatar
Matteo committed
32
33
34
35
36
37
38
def list_all_entrypoints() -> list[str]:
    return ["restore", "description", "docs"]


@app.get("/description")
async def get_server_info() -> Info:
    return info
Matteo's avatar
Matteo committed
39
40
41
42
43
44
45


@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",
Matteo's avatar
Matteo committed
46
        "src/tapeAudioRestoration.py",
Matteo's avatar
Matteo committed
47
48
49
50
51
52
53
54
55
56
        "--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:
Matteo's avatar
Matteo committed
57
58
        config = get_file_content("config/args.yaml", "yaml")
        process.extend(["--working_path", config["WORKING_PATH"]])
Matteo's avatar
Matteo committed
59
60
61
62
63
64
        

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

    if cprocess.returncode == 0:
        
Matteo's avatar
Matteo committed
65
        editing_list = get_file_content("EditingList.json", "json")
Matteo's avatar
Matteo committed
66
67
68
69
70
71
72
73
74
        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"),
        }}