Spaces:
Sleeping
Sleeping
| import evaluate | |
| import json | |
| import os | |
| import re | |
| import sys | |
| from pathlib import Path | |
| module = evaluate.load("SEA-AI/horizon-metrics") | |
| REGEX_YAML_BLOCK = re.compile(r"---[\n\r]+([\S\s]*?)[\n\r]+---[\n\r]") | |
| def json_to_string_type(input_types): | |
| """Maps json input type to str.""" | |
| return ["str" if i == "json" else i for i in input_types] | |
| def parse_readme(filepath): | |
| """Parses a repositories README and removes""" | |
| if not os.path.exists(filepath): | |
| return "No README.md found." | |
| with open(filepath, "r") as f: | |
| text = f.read() | |
| match = REGEX_YAML_BLOCK.search(text) | |
| if match: | |
| text = text[match.end():] | |
| return text | |
| def launch_gradio_widget(metric): | |
| """Launches `metric` widget with Gradio.""" | |
| try: | |
| import gradio as gr | |
| except ImportError as error: | |
| print( | |
| "To create a metric widget with Gradio make sure gradio is installed." | |
| ) | |
| raise error | |
| local_path = Path(sys.path[0]) | |
| def compute(data): | |
| return metric.compute( | |
| ) ##**parse_gradio_data(data, gradio_input_types) | |
| iface = gr.Interface( | |
| fn=compute, | |
| inputs=[ | |
| gr.inputs.Textbox(lines=5, label="Predictions"), | |
| gr.inputs.Textbox(lines=5, label="Ground Truth") | |
| ], | |
| outputs=gr.outputs.Textbox(label=metric.name), | |
| description= | |
| (metric.info.description + | |
| "\nIf this is a text-based metric, make sure to wrap you input in double quotes." | |
| " Alternatively you can use a JSON-formatted list as input."), | |
| title=f"Metric: {metric.name}", | |
| article=parse_readme(local_path / "README.md"), | |
| # TODO: load test cases and use them to populate examples | |
| # examples=[parse_test_cases(test_cases, feature_names, gradio_input_types)] | |
| ) | |
| iface.launch() | |
| launch_gradio_widget(module) | |