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