import gradio as gr from pathlib import Path from tapshex.defaults import DEFAULT_CONFIGFILE from tapshex.config import tapshex_config from tapshex.csvreader import dctap_csvreader from tapshex.shexify import tapdict_to_shexc from tapshex.template import SHEX_JINJA from dctap.utils import expand_uri_prefixes from dctap.inspect import pprint_tapshapes, print_warnings import json from ruamel.yaml import YAML def dctap_interface(csvfile_content, uris, warnings, output_format, config_content): if not csvfile_content.strip(): return "CSV file content is empty. Please provide valid input." if config_content: config_dict = tapshex_config(config_str=config_content) else: config_dict = tapshex_config() tapshapes_dict = dctap_csvreader(csvfile_str=csvfile_content, config_dict=config_dict) if uris: tapshapes_dict = expand_uri_prefixes(tapshapes_dict, config_dict) if output_format == "TAP/JSON": if not warnings: del tapshapes_dict["warnings"] return json.dumps(tapshapes_dict, indent=2) if output_format == "TAP/YAML": if not warnings: del tapshapes_dict["warnings"] y = YAML(typ=['rt', 'string']) y.indent(mapping=2, sequence=4, offset=2) sort_key = lambda k: (k != 'namespaces', k != 'shapes', k) sorted_tapshapes_dict = dict(sorted(tapshapes_dict.items(), key=sort_key)) tap_yaml = y.dump_to_string(sorted_tapshapes_dict, add_final_eol=True) return tap_yaml elif output_format == "ShExC": shexc_output = tapdict_to_shexc(dctap_as_dict=tapshapes_dict, shex_template=SHEX_JINJA) return shexc_output else: return pprint_tapshapes(tapshapes_dict, config_dict) # Gradio Interface gr.Interface( fn=dctap_interface, inputs=[ gr.components.Textbox(lines=20, placeholder="Enter CSV content here..."), gr.components.Checkbox(label="Expand URIs"), gr.components.Checkbox(label="Show Warnings"), # gr.components.Radio(["TAP/JSON", "ShExC", "Default"], label="Output Format"), gr.components.Radio(["TAP/JSON", "TAP/YAML", "ShExC"], label="Output Format"), gr.components.Textbox(lines=5, placeholder="Enter Config content (optional)..."), ], outputs=gr.components.Textbox(), live=False ).launch()