from evaluate.utils import launch_gradio_widget,parse_readme
import evaluate
import os
# os.environ['http_proxy']='http://localhost:7890'
# os.environ['https_proxy']='http://localhost:7890'
import gradio as gr
from evaluate.utils.gradio import infer_gradio_input_types,json_to_string_type,parse_gradio_data
from pathlib import Path
from datasets import Value
import sys

def cal_oppo_refuse_match(predictions):
    refuse=evaluate.load('libraxiong/oppo_refuse_match')
    return refuse.compute(predictions=predictions)

def launch_gradio_widget(metric):
    """Launches `metric` widget with Gradio."""
    # del os.environ['http_proxy']
    # del os.environ['https_proxy']
    # os.environ['no_proxy']='localhost, 127.0.0.1, ::1'
    try:
        import gradio as gr
    except ImportError as error:
        # logger.error("To create a metric widget with Gradio make sure gradio is installed.")
        raise error

    local_path = Path(sys.path[0])
    def compute(data):
        print(data)
        data=eval(data)
        return metric.compute(predictions=data)

    iface = gr.Interface(
        fn=compute,
        inputs=gr.Textbox(label=metric.name),
        outputs=gr.Textbox(label=metric.name),
        description=(
            metric.info.description + "\nIf this is a text-based metric. Just input the predictions,the format is ['some','some']"
        ),
        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()
if __name__=='__main__':
    launch_gradio_widget(evaluate.load('libraxiong/oppo_refuse_match'))