server.py 2.83 KB
Newer Older
Matteo's avatar
Matteo committed
1
import subprocess
Matteo's avatar
Matteo committed
2
3
from fastapi import FastAPI, Response, status, Query
from typing import Annotated
Matteo's avatar
Matteo committed
4

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

Matteo's avatar
Matteo committed
8
info = File("config/server.yaml", FileType.YAML).get_content()
Matteo's avatar
Matteo committed
9

Matteo's avatar
Matteo committed
10

Matteo's avatar
Matteo committed
11
app = FastAPI(**info)
Matteo's avatar
Matteo committed
12
13


Matteo's avatar
Matteo committed
14
@app.get("/")
Matteo's avatar
Matteo committed
15
16
17
18
async def list_all_entrypoints() -> list[str]:
    """
    Get the list of available main routes.
    """
Matteo's avatar
Matteo committed
19
20
21
22
23
    return ["restore", "description", "docs"]


@app.get("/description")
async def get_server_info() -> Info:
Matteo's avatar
Matteo committed
24
25
26
    """
    Retrieve all the informations about the software running on the server.
    """
Matteo's avatar
Matteo committed
27
    return info
Matteo's avatar
Matteo committed
28
29
30


@app.get("/restore", status_code=200)
Matteo's avatar
Matteo committed
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
async def restore(
        response: Response,
        equalization_w: Annotated[str, Query(
            description="Actual equalization of the audio tape applied during the recording.",
            example="CCIR"
        )],
        equalization_r: Annotated[str, Query(
            description="Actual equalization of the audio tape applied during the playback.",
            example="NAB"
        )],
        speed_w: Annotated[str, Query(
            description="Actual speed of the audio tape applied during the recording.",
            example="7.5"
        )],
        speed_r: Annotated[str, Query(
            description="Actual speed of the audio tape applied during the playback.",
            example="15"
        )],
        files_name: Annotated[str, Query(
            description="Name of the audio/video file to be analyzed without the extension.",
            example="filename"
        )],
        working_path: Annotated[str, Query(
            description="Path to the directory containing the preservation files.",
            examples={
                "Absolute path": {"value": "/home/user/preservation"},
                "Relative path": {"value": "preservation"}
            }
        )] = None
    ) -> dict:
Matteo's avatar
Matteo committed
61
62
63
    
    process = [
        "python",
Matteo's avatar
Matteo committed
64
        "src/tapeAudioRestoration.py",
Matteo's avatar
Matteo committed
65
66
67
68
69
70
71
72
73
74
        "--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
75
        config = File("config/args.yaml", FileType.YAML).get_content()
Matteo's avatar
Matteo committed
76
        process.extend(["--working_path", config["WORKING_PATH"]])
Matteo's avatar
Matteo committed
77
78
79
80
81
82
        

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

    if cprocess.returncode == 0:
        
Matteo's avatar
Matteo committed
83
        editing_list = File("EditingList.json", FileType.JSON).get_content()
Matteo's avatar
Matteo committed
84
85
86
87
88
89
90
91
92
        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"),
        }}