Refactor Gradio widget and import statements
Browse files
app.py
CHANGED
@@ -1,41 +1,68 @@
|
|
1 |
-
# import evaluate
|
2 |
# from evaluate.utils import launch_gradio_widget
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
# launch_gradio_widget(module)
|
7 |
|
8 |
-
import evaluate
|
9 |
-
import gradio as gr
|
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 |
-
iface.launch()
|
|
|
|
|
1 |
# from evaluate.utils import launch_gradio_widget
|
2 |
+
from tests import test_cases
|
3 |
+
from evaluate.utils.logging import get_logger
|
4 |
+
from evaluate.utils import (
|
5 |
+
infer_gradio_input_types,
|
6 |
+
parse_gradio_data,
|
7 |
+
json_to_string_type,
|
8 |
+
parse_readme,
|
9 |
+
parse_test_cases,
|
10 |
+
)
|
11 |
+
from pathlib import Path
|
12 |
|
13 |
+
import evaluate
|
14 |
+
import sys
|
15 |
+
import evaluate
|
16 |
+
import datasets
|
17 |
|
18 |
+
logger = get_logger(__name__)
|
|
|
19 |
|
|
|
|
|
20 |
|
21 |
+
def launch_gradio_widget(metric, test_cases):
|
22 |
+
"""Launches `metric` widget with Gradio."""
|
23 |
|
24 |
+
try:
|
25 |
+
import gradio as gr
|
26 |
+
except ImportError as error:
|
27 |
+
logger.error(
|
28 |
+
"To create a metric widget with Gradio make sure gradio is installed."
|
29 |
+
)
|
30 |
+
raise error
|
31 |
+
|
32 |
+
local_path = Path(sys.path[0])
|
33 |
+
# if there are several input types, use first as default.
|
34 |
+
if isinstance(metric.features, list):
|
35 |
+
(feature_names, feature_types) = zip(*metric.features[0].items())
|
36 |
+
else:
|
37 |
+
(feature_names, feature_types) = zip(*metric.features.items())
|
38 |
+
gradio_input_types = infer_gradio_input_types(feature_types)
|
39 |
+
|
40 |
+
def compute(data):
|
41 |
+
return metric.compute(**parse_gradio_data(data, gradio_input_types))
|
42 |
+
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=compute,
|
45 |
+
inputs=gr.inputs.Dataframe(
|
46 |
+
headers=feature_names,
|
47 |
+
col_count=len(feature_names),
|
48 |
+
row_count=1,
|
49 |
+
datatype=json_to_string_type(gradio_input_types),
|
50 |
+
),
|
51 |
+
outputs=gr.outputs.Textbox(label=metric.name),
|
52 |
+
description=(
|
53 |
+
metric.info.description
|
54 |
+
+ "\nIf this is a text-based metric, make sure to wrap you input in double quotes."
|
55 |
+
" Alternatively you can use a JSON-formatted list as input."
|
56 |
+
),
|
57 |
+
title=f"Metric: {metric.name}",
|
58 |
+
article=parse_readme(local_path / "README.md"),
|
59 |
+
# TODO: load test cases and use them to populate examples
|
60 |
+
examples=[parse_test_cases(test_cases, feature_names, gradio_input_types)],
|
61 |
+
)
|
62 |
+
|
63 |
+
iface.launch()
|
64 |
+
|
65 |
+
|
66 |
+
module = evaluate.load("danieldux/metric_template_1")
|
67 |
|
68 |
+
launch_gradio_widget(module, test_cases)
|
|