Spaces:
Sleeping
Sleeping
John Graham Reynolds
commited on
Commit
·
9739d7b
1
Parent(s):
677fc21
add app module
Browse files
app.py
CHANGED
@@ -1,8 +1,88 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
return 'Hello, ' + name + '!'
|
6 |
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
import gradio as gr
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import evaluate
|
6 |
+
from evaluate.utils import infer_gradio_input_types, json_to_string_type, parse_readme, parse_test_cases
|
7 |
+
# from evaluate.utils import launch_gradio_widget # using this directly is erroneous - lets fix this
|
8 |
+
from fixed_precision import FixedPrecision
|
9 |
+
from pathlib import Path
|
10 |
|
11 |
+
added_description = """
|
12 |
+
See the 🤗 Space showing off how to combine various metrics here:
|
13 |
+
[MarioBarbeque/CombinedEvaluationMetrics](https://huggingface.co/spaces/MarioBarbeque/CombinedEvaluationMetrics)
|
14 |
|
15 |
+
In the specific use case of the `FixedPrecision` metric, one writes the following:\n
|
|
|
16 |
|
17 |
+
```python
|
18 |
+
precision = FixedPrecision(average=..., zero_division=...)
|
19 |
|
20 |
+
precision.add_batch(predictions=..., references=...)
|
21 |
+
precision.compute()
|
22 |
+
```\n
|
23 |
+
|
24 |
+
where the `average` parameter can be chosen to configure the way precision scores across labels are averaged. Acceptable values include `[None, 'micro', 'macro', 'weighted']` (
|
25 |
+
or `binary` if there exist only two labels). Similarly, the `zero_division` parameter "Sets the value to return when there is a zero division". Options include:
|
26 |
+
{`“warn”`, `0.0`, `1.0`, `np.nan`}. Since "warn" can still result in an error, we fix to it NaN in this demo.\n
|
27 |
+
"""
|
28 |
+
|
29 |
+
metric = FixedPrecision()
|
30 |
+
|
31 |
+
if isinstance(metric.features, list):
|
32 |
+
(feature_names, feature_types) = zip(*metric.features[0].items())
|
33 |
+
else:
|
34 |
+
(feature_names, feature_types) = zip(*metric.features.items())
|
35 |
+
gradio_input_types = infer_gradio_input_types(feature_types)
|
36 |
+
|
37 |
+
local_path = Path(sys.path[0])
|
38 |
+
# configure these randomly using randint generator and feature names?
|
39 |
+
test_case_1 = [ {"predictions":[1,2,3,4,5], "references":[1,2,5,4,3]} ]
|
40 |
+
test_case_2 = [ {"predictions":[9,8,7,6,5], "references":[7,8,9,6,5]} ]
|
41 |
+
|
42 |
+
# configure this based on the input type, etc. for launch_gradio_widget
|
43 |
+
def compute(input_df: pd.DataFrame, method: str):
|
44 |
+
|
45 |
+
metric = FixedPrecision(average=method if method != "None" else None, zero_division=np.nan)
|
46 |
+
|
47 |
+
cols = [col for col in input_df.columns]
|
48 |
+
predicted = [int(num) for num in input_df[cols[0]].to_list()]
|
49 |
+
references = [int(num) for num in input_df[cols[1]].to_list()]
|
50 |
+
|
51 |
+
metric.add_batch(predictions=predicted, references=references)
|
52 |
+
outputs = metric.compute()
|
53 |
+
|
54 |
+
return f"The precision score for these predictions is: \n {outputs}"
|
55 |
+
|
56 |
+
space = gr.Interface(
|
57 |
+
fn=compute,
|
58 |
+
inputs=[
|
59 |
+
gr.Dataframe(
|
60 |
+
headers=feature_names,
|
61 |
+
col_count=len(feature_names),
|
62 |
+
row_count=5,
|
63 |
+
datatype=json_to_string_type(gradio_input_types),
|
64 |
+
),
|
65 |
+
gr.Radio(
|
66 |
+
["weighted", "micro", "macro", "None", "binary"],
|
67 |
+
label="Averaging Method",
|
68 |
+
info="Method for averaging the precision score across labels. \n `binary` only works if you are evaluating a binary classification model."
|
69 |
+
)
|
70 |
+
],
|
71 |
+
outputs=gr.Textbox(label=metric.name),
|
72 |
+
description=metric.info.description + added_description,
|
73 |
+
title=f"Metric: {metric.name}",
|
74 |
+
article=parse_readme(local_path / "README.md"),
|
75 |
+
examples=[
|
76 |
+
[
|
77 |
+
parse_test_cases(test_case_1, feature_names, gradio_input_types)[0], # notice how we unpack this for when we fix launch_gradio_widget
|
78 |
+
"weighted"
|
79 |
+
],
|
80 |
+
[
|
81 |
+
parse_test_cases(test_case_2, feature_names, gradio_input_types)[0],
|
82 |
+
"micro"
|
83 |
+
],
|
84 |
+
],
|
85 |
+
cache_examples=False
|
86 |
+
)
|
87 |
+
|
88 |
+
space.launch()
|