danieldux commited on
Commit
04abc02
1 Parent(s): 41dfd1b

Refactor Gradio widget and import statements

Browse files
Files changed (1) hide show
  1. app.py +61 -34
app.py CHANGED
@@ -1,41 +1,68 @@
1
- # import evaluate
2
  # from evaluate.utils import launch_gradio_widget
 
 
 
 
 
 
 
 
 
 
3
 
 
 
 
 
4
 
5
- # module = evaluate.load("danieldux/metric_template_1")
6
- # launch_gradio_widget(module)
7
 
8
- import evaluate
9
- import gradio as gr
10
 
11
- # Load your metric module
12
- module = evaluate.load("danieldux/metric_template_1")
13
 
14
- # Define pre-populated examples
15
- examples = [
16
- {
17
- "references": ["1111", "1112", "1113", "1114"],
18
- "predictions": ["1111", "1113", "1120", "1211"],
19
- }
20
- # {"references": ["It is a sunny day."], "predictions": ["The day is sunny."]}
21
- ]
22
-
23
-
24
- # Use the modules "compute" function that takes references and predictions
25
- def compute_metric(references, predictions):
26
- return module.compute(references=[references], predictions=[predictions])
27
-
28
-
29
- # Create a Gradio interface
30
- iface = gr.Interface(
31
- fn=compute_metric,
32
- inputs=[
33
- gr.inputs.Textbox(lines=2, label="References"),
34
- gr.inputs.Textbox(lines=2, label="Predictions"),
35
- ],
36
- outputs="text",
37
- examples=examples, # Pass the pre-populated examples here
38
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Launch the interface
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)