server.py 1.98 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
from fastapi import FastAPI, Response, status, Query
from typing import Annotated

from mpai_cae_arp.files import File, FileType
from mpai_cae_arp.types.schema import Info

info = File("config/server.yaml", FileType.YAML).get_content()
app = FastAPI(**info)


@app.get("/")
def list_all_entrypoints() -> list[str]:
    """
    Get the list of available main routes.
    """
    return ["restore", "description", "docs"]


@app.get("/description")
def get_server_info() -> Info:
    """
    Retrieve all the informations about the software running on the server.
    """
    return info


@app.get("/package", status_code=200)
async def predict(
        response: Response,
        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
    ):
    
    process = [
        "python",
        "src/packager.py",
        ]

    config_file = File("config/args.yaml", FileType.YAML)
    # Read configuration file
    config = config_file.get_content()

    # Update configuration file with query parameters
    config["FilesName"] = files_name
    if working_path is not None:
        config["WorkingPath"] = working_path
    config_file.write_content(config)


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