File size: 2,199 Bytes
41dfd1b
7e936ac
04abc02
 
 
 
 
 
 
 
 
41dfd1b
04abc02
 
 
d94c92e
04abc02
d94c92e
41dfd1b
04abc02
 
41dfd1b
04abc02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08d668a
 
04abc02
 
 
08d668a
04abc02
08d668a
 
04abc02
 
 
08d668a
04abc02
08d668a
04abc02
 
19a950c
8f86dcf
04abc02
 
 
b0179e8
08d668a
04abc02
08d668a
 
 
 
04abc02
 
19a950c
41dfd1b
08d668a
 
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
# from evaluate.utils import launch_gradio_widget
from tests import test_cases
from evaluate.utils.logging import get_logger
from evaluate.utils import (
    infer_gradio_input_types,
    parse_gradio_data,
    json_to_string_type,
    parse_readme,
    parse_test_cases,
)
from pathlib import Path

import evaluate
import sys
import evaluate

logger = get_logger(__name__)


def launch_gradio_widget(metric, test_cases):
    """Launches `metric` widget with Gradio."""

    try:
        import gradio as gr
    except ImportError as error:
        logger.error(
            "To create a metric widget with Gradio make sure gradio is installed."
        )
        raise error

    local_path = Path(sys.path[0])
    # if there are several input types, use first as default.
    if isinstance(metric.features, list):
        (feature_names, feature_types) = zip(*metric.features[0].items())
    else:
        (feature_names, feature_types) = zip(*metric.features.items())
    gradio_input_types = infer_gradio_input_types(feature_types)

    parsed_test_cases = parse_test_cases(test_cases, feature_names, gradio_input_types)

    def compute(data):
        return metric.compute(**parse_gradio_data(data, gradio_input_types))

    demo = gr.Interface(
        fn=compute,
        inputs=gr.Dataframe(
            value=parsed_test_cases[0],
            headers=feature_names,
            col_count=len(feature_names),
            datatype=json_to_string_type(gradio_input_types),
            # row_count=5,
        ),
        outputs=gr.Textbox(label=metric.name),
        description=(
            metric.info.description
            + "\nISCO codes must be wrapped in double quotes."
            # " Alternatively you can use a JSON-formatted list as input."
        ),
        title=f"Metric: {metric.name}",
        article=parse_readme(local_path / "README.md"),
        # TODO: load test cases and use them to populate examples
        examples=[parsed_test_cases],
    )
    if __name__ == "__main__":
        demo.launch()
    else:
        return demo


module = evaluate.load("danieldux/isco_hierarchical_accuracy")

if __name__ == "__main__":
    launch_gradio_widget(module, test_cases)