File size: 3,349 Bytes
c66fb0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34ab5fd
c66fb0b
 
b6d2398
c66fb0b
 
 
 
34ab5fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6d2398
c66fb0b
 
34ab5fd
 
 
 
c66fb0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aecbb0
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# pylint: disable=no-name-in-module
# pylint: disable=no-member

"""
Author : Bastien GUILLAUME
Version : 0.0.1
Date : 2023-03-16

Title : Inference With Gradio running an onnxruntime backend
"""

import json
import logging
import os
from functools import reduce
from jsonschema import validate

logging.basicConfig(
    format="%(asctime)s.%(msecs)03d %(levelname)-8s [%(filename)s:%(lineno)s - %(funcName)20s() ] - %(message)s",
    level=logging.INFO,
    datefmt="%Y%m%d-%H%M%S",
)

schema = {
    "definitions": {},
    "type": "string",
    "properties": {
        "title": {"type": "string"},
        "description": {"type": "string"},
        "tasks": {
            "type": "object",
            "properties": {
                "tasks": {"type": "object"},
                "properties": {
                    "shortname": {"type": "string"},
                    "name": {"type": "object"},
                    # "description": {"type": "string"},
                    "products": {"type": "array"},
                    "models": {"type": "object"},
                },
                "additionalProperties": True,
            },
        },
    },
    "additionalProperties": True,
}

with open("config_file/demo_yvesrocher.json") as config_file:
    config = json.load(config_file)

print(type(config["tasks"]["task1"]["description"]))

validate(json.dumps(config), schema)

logging.log(level=logging.DEBUG, msg=f"Loaded config file : {json.dumps(config)}")


def deep_get(dictionary, keys, default=None):
    return reduce(
        lambda d, key: d.get(key, default) if isinstance(d, dict) else default,
        keys.split("."),
        dictionary,
    )


def get_tasks():
    tasks = []
    for task in config["tasks"].keys():
        tasks.append(config["tasks"][task]["name"]["fr"])
    return tasks


def get_tasks_name():
    tasks_name = []
    for task in config["tasks"].keys():
        tasks_name.append(config["tasks"][task]["shortname"])
    return tasks_name


def get_tasks_description():
    tasks_descripion = []
    for task in config["tasks"].keys():
        tasks_descripion.append(config["tasks"][task]["description"]["fr"])
    return tasks_descripion


def get_tasks_products():
    tasks_products = []
    for task in config["tasks"].keys():
        tasks_products.append(config["tasks"][task]["products"])
    return tasks_products


title = (
    os.getenv("GRADIO_TITLE")
    if "GRADIO_TITLE" in os.environ
    else config.get("title", "TITLE neither set in config file nor in ENV")
)
description = (
    os.getenv("GRADIO_DESCRIPTION")
    if "GRADIO_DESCRIPTION" in os.environ
    else config.get("description", "DESCRIPTION neither set in config file nor in ENV")
)
tasks = get_tasks()
tasks_name = get_tasks_name()
tasks_description = get_tasks_description()
tasks_products = get_tasks_products()
logging.log(level=logging.INFO, msg=f"Parsed Data :")
logging.log(level=logging.INFO, msg=f"Title : {title}")
logging.log(level=logging.INFO, msg=f"Description : {description}")
logging.log(level=logging.INFO, msg=f"Tasks : {tasks}")
logging.log(level=logging.INFO, msg=f"Tasks name : {tasks_name}")
logging.log(level=logging.INFO, msg=f"Tasks descrption : {tasks_description}")
logging.log(level=logging.INFO, msg=f"Tasks products : {tasks_products}")
logging.log(level=logging.INFO, msg=f"End of Parsed Data\n")