File size: 1,788 Bytes
c401996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f86790
c401996
 
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
import evaluate
from evaluate.utils import infer_gradio_input_types, parse_gradio_data, json_to_string_type, parse_readme
from pathlib import Path
import sys

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

    local_path = Path(sys.path[0])
    
    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)
    
    def compute(data):
        return metric._compute(
            model='gpt2',
            tasks='wikitext', 
            **parse_gradio_data(data, gradio_input_types)
        )
    
    iface = gr.Interface(
        fn=compute,
        inputs=gr.Dataframe(
            headers=feature_names,
            col_count=len(feature_names),
            row_count=1,
            datatype=json_to_string_type(gradio_input_types),
        ),
        outputs=gr.Textbox(label=metric.name),
        description=(
            metric.info.description + "\nThis metric is computed using the 'gpt2' model on the 'wikitext' task.\n"
            "Ensure your input is appropriate for the selected task. "
            "If this is a text-based metric, wrap your input 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"),
    )
    
    iface.launch()

module = evaluate.load("d-matrix/dmxMetric")

launch_gradio_widget(module)