server.py 2.24 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
69
70
71
import subprocess
import yaml
from fastapi import FastAPI, Response, status

from packager 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) *Packager* AIM, providing:
* Access Copy Files:
  1. Restored Audio Files;
  2. Editing List;
  3. Set of Irregularity Images in a .zip file;
  4. Irregularity File.
* Preservation Master Files:
  1. Preservation Audio File;
  2. Preservation Audio-Visual File where the audio has been replaced with the Audio of the Preservation Audio File
     fully synchronised with the video;
  3. Set of Irregularity Images in a .zip file;
  4. Irregularity File.
""",
    version=__version__,
    contact={
        "name": __maintainer__,
        "email": __email__
    },
    license_info={
        "name": __license__,
    }
)


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


@app.get("/package", status_code=200)
async def predict(response: Response, files_name: str, working_path: str | None = None):
    
    process = [
        "python",
        "packager.py",
        ]

    # Read configuration file
    with open("config.yaml") as yaml_file:
        config = yaml.safe_load(yaml_file)

    # Update configuration file with query parameters
    config["FilesName"] = files_name
    if working_path is not None:
        config["WorkingPath"] = working_path
    with open("config.yaml", "w") as yaml_file:
        yaml.dump(config, yaml_file)

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

    if cprocess.returncode == 0:
        return {"message": "Package created successfully"}

    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"),
        }}