John Graham Reynolds commited on
Commit
19d5834
·
1 Parent(s): edd642a

add code reminder for fixing widget fn

Browse files
Files changed (1) hide show
  1. issue_fix.py +72 -0
issue_fix.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Come back to this file and previous versions in this repo to configure a fix for the broken launch_gradio_widget command
3
+ """
4
+
5
+ import sys
6
+ import gradio as gr
7
+ import pandas as pd
8
+ import evaluate
9
+ from evaluate.utils import infer_gradio_input_types, json_to_string_type, parse_readme, parse_test_cases
10
+ # from evaluate.utils import launch_gradio_widget # using this directly is erroneous - lets fix this
11
+ from fixed_f1 import FixedF1
12
+ from pathlib import Path
13
+
14
+ metric = FixedF1()
15
+
16
+ if isinstance(metric.features, list):
17
+ (feature_names, feature_types) = zip(*metric.features[0].items())
18
+ else:
19
+ (feature_names, feature_types) = zip(*metric.features.items())
20
+ gradio_input_types = infer_gradio_input_types(feature_types)
21
+
22
+ local_path = Path(sys.path[0])
23
+ test_cases = [ {"predictions":[1,2,3,4,5], "references":[1,2,5,4,3]} ] # configure this randomly using randint generator and feature names?
24
+
25
+ # configure this based on the input type, etc. for launch_gradio_widget
26
+ def compute(input_df: pd.DataFrame, method: str):
27
+
28
+ metric = FixedF1(average=method)
29
+
30
+ cols = [col for col in input_df.columns]
31
+
32
+ predicted = [int(num) for num in input_df[cols[0]].to_list()]
33
+ references = [int(num) for num in input_df[cols[1]].to_list()]
34
+
35
+ metric.add_batch(predictions=predicted, references=references)
36
+
37
+ outputs = metric.compute()
38
+
39
+ f"Your metrics are as follows: \n {outputs}"
40
+
41
+ space = gr.Interface(
42
+ fn=compute,
43
+ inputs=[
44
+ gr.Dataframe(
45
+ headers=feature_names,
46
+ col_count=len(feature_names),
47
+ row_count=5,
48
+ datatype=json_to_string_type(gradio_input_types),
49
+ ),
50
+ gr.Radio(
51
+ ["weighted", "micro", "macro", "binary", "None"],
52
+ label="Averaging Method",
53
+ info="Method for averaging the F1 score across labels."
54
+ )
55
+ ],
56
+ outputs=gr.Textbox(label=metric.name),
57
+ description=(
58
+ metric.info.description + "\nIf this is a text-based metric, make sure to wrap your input in double quotes."
59
+ " Alternatively you can use a JSON-formatted list as input."
60
+ ),
61
+ title=f"Metric: {metric.name}",
62
+ article=parse_readme(local_path / "README.md"),
63
+ # TODO: load test cases and use them to populate examples
64
+ examples=[
65
+ # correct depth?
66
+ pd.DataFrame(parse_test_cases(test_cases, feature_names, gradio_input_types)[0]),
67
+ pd.DataFrame(columns=["Metric", "Averaging Method"], data=[["f1", "weighted"]])
68
+ ],
69
+ cache_examples=False
70
+ )
71
+
72
+ space.launch()