File size: 2,504 Bytes
19d5834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Come back to this file and previous versions in this repo to configure a fix for the broken launch_gradio_widget command
"""

import sys
import gradio as gr
import pandas as pd
import evaluate
from evaluate.utils import infer_gradio_input_types, json_to_string_type, parse_readme, parse_test_cases
# from evaluate.utils import launch_gradio_widget # using this directly is erroneous - lets fix this
from fixed_f1 import FixedF1
from pathlib import Path

metric = FixedF1()

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)

local_path = Path(sys.path[0])
test_cases = [ {"predictions":[1,2,3,4,5], "references":[1,2,5,4,3]} ] # configure this randomly using randint generator and feature names?

# configure this based on the input type, etc. for launch_gradio_widget
def compute(input_df: pd.DataFrame, method: str):
    
    metric = FixedF1(average=method)

    cols = [col for col in input_df.columns]

    predicted = [int(num) for num in input_df[cols[0]].to_list()]
    references = [int(num) for num in input_df[cols[1]].to_list()]

    metric.add_batch(predictions=predicted, references=references)
    
    outputs = metric.compute()

    f"Your metrics are as follows: \n {outputs}"

space = gr.Interface(
    fn=compute,
    inputs=[
        gr.Dataframe(
        headers=feature_names,
        col_count=len(feature_names),
        row_count=5,
        datatype=json_to_string_type(gradio_input_types),
        ),
        gr.Radio(
            ["weighted", "micro", "macro", "binary", "None"], 
            label="Averaging Method", 
            info="Method for averaging the F1 score across labels."
        )
    ],
    outputs=gr.Textbox(label=metric.name),
    description=(
        metric.info.description + "\nIf this is a text-based metric, make sure to 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"),
    # TODO: load test cases and use them to populate examples
    examples=[
        # correct depth?
        pd.DataFrame(parse_test_cases(test_cases, feature_names, gradio_input_types)[0]),
        pd.DataFrame(columns=["Metric", "Averaging Method"], data=[["f1", "weighted"]])
    ],
    cache_examples=False
    )

space.launch()