import requests
import os
import gradio as gr
import gradio_client.utils as _gc_utils
import json
import time
import traceback
from validation import validate_json, validate_croissant, validate_records, validate_rai, generate_validation_report, set_active_token, clear_active_token
# Patch gradio_client.utils to handle boolean JSON schemas.
# gradio_client 1.7.2 crashes when a component schema has `additionalProperties: true/false`.
# The inner recursive function _json_schema_to_python_type receives True/False and either
# raises TypeError ("argument of type 'bool' is not iterable") or APIInfoParseError.
# Patching the inner function is the correct fix since it's called recursively.
_orig_json_schema_to_python_type = _gc_utils._json_schema_to_python_type
def _safe_json_schema_to_python_type(schema, defs=None):
if not isinstance(schema, dict):
return "any"
return _orig_json_schema_to_python_type(schema, defs)
_gc_utils._json_schema_to_python_type = _safe_json_schema_to_python_type
# HF Spaces embeds the app in an iframe. Gradio's LoginButton uses window.location.assign()
# which navigates the iframe itself, causing cookie issues in Safari and redirect loops in
# incognito. HF docs recommend using target=_blank to open OAuth in a new tab instead.
import gradio.components.login_button as _login_btn_mod
_login_btn_mod._js_handle_redirect = """
(buttonValue) => {
const path = buttonValue === BUTTON_DEFAULT_VALUE ? '/login/huggingface' : '/logout';
const url = window.location.origin + path + window.location.search;
window.parent?.postMessage({ type: "SET_SCROLLING", enabled: true }, "*");
setTimeout(() => { window.open(url, '_blank'); }, 500);
}
"""
def process_file(file):
results = []
json_data = None
filename = file.name.split("/")[-1]
# Check 1: JSON validation
json_valid, json_message, json_data = validate_json(file.name)
json_message = json_message.replace("\nā\n", "\n")
results.append(("JSON Format Validation", json_valid, json_message, "pass" if json_valid else "error"))
if not json_valid:
return results, None
# Check 2: Croissant validation
croissant_valid, croissant_message, croissant_status = validate_croissant(json_data)
croissant_message = croissant_message.replace("\nā\n", "\n")
results.append(("Croissant Schema Validation", croissant_valid, croissant_message, croissant_status))
if not croissant_valid:
return results, None
# Check 3: Responsible AI metadata
rai_valid, rai_message = validate_rai(json_data)
results.append(("Responsible AI Metadata", rai_valid, rai_message, "pass" if rai_valid else "error"))
# Check 4: Records validation (with timeout-safe and error-specific logic)
records_valid, records_message, records_status = validate_records(json_data)
records_message = records_message.replace("\nā\n", "\n")
results.append(("Records Generation Test", records_valid, records_message, records_status))
# Generate final report
report = generate_validation_report(filename, json_data, results)
return results, report
def create_ui():
with gr.Blocks(theme=gr.themes.Soft()) as app:
gr.HTML("

")
gr.Markdown("# š„ Croissant Validator for NeurIPS")
gr.Markdown("""
Upload your Croissant JSON-LD file or enter a URL to validate if it meets the requirements for NeurIPS submission. Read more about why this is required..
*It doesn't matter where your dataset is hosted. It can be Hugging Face, Kaggle, OpenML, Harvard Dataverse, self-hosted, or elsewhere.*
""")
gr.Markdown("""
The validator will check:
1. If the file is valid JSON
2. If it passes Croissant schema validation
3. If all required Responsible AI metadata fields are present
4. If records can be generated within a reasonable time
""")
gr.HTML("""
""")
if os.environ.get("SPACE_ID"):
with gr.Column(elem_id="login-card"):
gr.HTML(""
"If your dataset is hosted on Hugging Face, please log in to avoid rate limiting issues. "
"If your dataset is not on Hugging Face, you can still use the validator without logging in.
")
gr.LoginButton(variant="secondary", icon=None, scale=0, elem_classes=["login-button"])
# Track the active tab for conditional UI updates
active_tab = gr.State("upload") # Default to upload tab
# Create a container for the entire input section
with gr.Group():
# Input tabs
with gr.Tabs() as tabs:
with gr.TabItem("Upload File", id="upload_tab"):
file_input = gr.File(label="Upload Croissant JSON-LD File", file_types=[".json", ".jsonld"])
validate_btn = gr.Button("Validate Uploaded File", variant="primary")
with gr.TabItem("URL Input", id="url_tab"):
url_input = gr.Textbox(
label="Enter Croissant JSON-LD URL",
placeholder="e.g. https://huggingface.co/api/datasets/facebook/natural_reasoning/croissant"
)
fetch_btn = gr.Button("Fetch and Validate", variant="primary")
# Change initial message to match upload tab
upload_progress = gr.HTML(
"""Ready for upload
""",
visible=True)
# Now create the validation results section in a separate group
with gr.Group():
# Validation results
validation_results = gr.HTML(value="", visible=True)
validation_progress = gr.HTML(visible=False)
# Collapsible report section
with gr.Accordion("Download full validation report", visible=False, open=False) as report_group:
with gr.Column():
report_md = gr.File(
label="Download Report",
visible=True,
file_types=[".md"]
)
report_text = gr.Textbox(
label="Report Content",
visible=True,
lines=10,
elem_id="report-text-box"
)
# Define CSS for the validation UI
gr.HTML("""
""")
# Update helper messages based on tab changes
def on_tab_change(evt: gr.SelectData):
tab_id = evt.value
if tab_id == "Upload File":
return [
"upload",
"""Ready for upload
""",
gr.update(value=""), # Clear validation results
gr.update(visible=False), # Hide report group
None, # Clear report text
None, # Clear report file
None, # Clear file input
gr.update(value="") # Clear URL input
]
else:
return [
"url",
"""Enter a URL to fetch
""",
gr.update(value=""), # Clear validation results
gr.update(visible=False), # Hide report group
None, # Clear report text
None, # Clear report file
None, # Clear file input
gr.update(value="") # Clear URL input
]
def on_copy_click(report):
return report
def on_download_click(report, file_name):
report_file = f"report_{file_name}.md"
with open(report_file, "w") as f:
f.write(report)
return report_file
def on_file_upload(file):
if file is None:
return [
"""Ready for upload
""",
gr.update(value=""), # Clear validation results
gr.update(visible=False), # Hide report group
None, # Clear report text
None # Clear report file
]
return [
"""ā
File uploaded successfully
""",
gr.update(value=""), # Clear validation results
gr.update(visible=False), # Hide report group
None, # Clear report text
None # Clear report file
]
def fetch_from_url(url, oauth_token: gr.OAuthToken | None = None):
if not url:
yield [
"""Please enter a URL
""",
gr.update(value=""),
gr.update(visible=False),
None,
None
]
return
try:
# Fetch JSON from URL
response = requests.get(url, timeout=10)
response.raise_for_status()
json_data = response.json()
results = []
results.append(("JSON Format Validation", True, "The URL returned valid JSON."))
croissant_valid, croissant_message, croissant_status = validate_croissant(json_data)
results.append(("Croissant Schema Validation", croissant_valid, croissant_message, croissant_status))
if not croissant_valid:
yield [
"""ā
JSON fetched successfully from URL
""",
build_results_html(results),
gr.update(visible=False),
None,
None
]
return
# Responsible AI metadata (fast)
rai_valid, rai_message = validate_rai(json_data)
results.append(("Responsible AI Metadata", rai_valid, rai_message, "pass" if rai_valid else "error"))
# Show partial results while records test runs
records_spinner = """Running records generation test... """
yield [
"""ā
JSON fetched successfully from URL
""",
gr.update(value=build_results_html(results)["value"] + records_spinner, visible=True),
gr.update(visible=False),
None,
None
]
# Records validation (slow)
set_active_token(oauth_token.token if oauth_token else None)
try:
records_valid, records_message, records_status = validate_records(json_data)
finally:
clear_active_token()
results.append(("Records Generation Test (Optional)", records_valid, records_message, records_status))
report = generate_validation_report(url.split("/")[-1], json_data, results)
report_filename = f"report_croissant-validation_{json_data.get('name', 'unnamed')}.md"
if report:
with open(report_filename, "w") as f:
f.write(report)
yield [
"""ā
JSON fetched successfully from URL
""",
build_results_html(results),
gr.update(visible=True),
report,
report_filename
]
except requests.exceptions.RequestException as e:
error_message = f"Error fetching URL: {str(e)}"
yield [
f"""{error_message}
""",
gr.update(value=""),
gr.update(visible=False),
None,
None
]
except json.JSONDecodeError as e:
error_message = f"URL did not return valid JSON: {str(e)}"
yield [
f"""{error_message}
""",
gr.update(value=""),
gr.update(visible=False),
None,
None
]
except Exception as e:
error_message = f"Unexpected error: {str(e)}"
yield [
f"""{error_message}
""",
gr.update(value=""),
gr.update(visible=False),
None,
None
]
def build_results_html(results):
html = ''
for i, result in enumerate(results):
if len(result) == 4:
test_name, passed, message, status = result
else:
test_name, passed, message = result
status = "pass" if passed else "error"
if status == "pass":
status_class = "status-success"
status_icon = "ā"
message_with_emoji = "ā
" + message
elif status == "warning":
status_class = "status-warning"
status_icon = "?"
if "Records" in test_name:
if "429" in message or "Too Many Requests" in message or "rate limit" in message.lower():
message_with_emoji = (
"ā ļø Rate limit hit while trying to generate records. "
"Log in with your Hugging Face account (button above) to use your own token and avoid this.\n\n"
+ message
)
else:
message_with_emoji = "ā ļø Could not automatically generate records. This is oftentimes not an issue (e.g. datasets could be too large or too complex), and it's not required to pass this test to submit to NeurIPS.\n\n" + message
else:
message_with_emoji = "ā ļø " + message
else: # error
status_class = "status-error"
status_icon = "ā"
message_with_emoji = "ā " + message
message_with_emoji = message_with_emoji.replace("\n", "
")
html += f'''
'''
html += '
'
return gr.update(value=html, visible=True)
def on_validate(file, oauth_token: gr.OAuthToken | None = None):
if file is None:
yield [
gr.update(value=""), # validation_results
gr.update(visible=False), # validation_progress
gr.update(visible=False), # report_group
None, # report_text
None # report_md
]
return
# Show initial spinner
progress_html = """
"""
yield [
gr.update(value=""),
gr.update(visible=True, value=progress_html),
gr.update(visible=False),
None,
None
]
results = []
filename = file.name.split("/")[-1]
# Check 1: JSON
json_valid, json_message, json_data = validate_json(file.name)
json_message = json_message.replace("\nā\n", "\n")
results.append(("JSON Format Validation", json_valid, json_message, "pass" if json_valid else "error"))
if not json_valid:
yield [build_results_html(results), gr.update(visible=False), gr.update(visible=False), None, None]
return
# Check 2: Croissant schema
croissant_valid, croissant_message, croissant_status = validate_croissant(json_data)
croissant_message = croissant_message.replace("\nā\n", "\n")
results.append(("Croissant Schema Validation", croissant_valid, croissant_message, croissant_status))
if not croissant_valid:
yield [build_results_html(results), gr.update(visible=False), gr.update(visible=False), None, None]
return
# Check 3: Responsible AI metadata (fast)
rai_valid, rai_message = validate_rai(json_data)
results.append(("Responsible AI Metadata", rai_valid, rai_message, "pass" if rai_valid else "error"))
# Show partial results + spinner for records test
records_spinner = """
Running records generation test...
This test tries to load your data and may be slow for large datasets.
It's not strictly required to pass this test to submit to NeurIPS. You can still submit your Croissant file if this test takes too long.
"""
yield [
build_results_html(results),
gr.update(visible=True, value=records_spinner),
gr.update(visible=False),
None,
None
]
# Check 4: Records (slow)
set_active_token(oauth_token.token if oauth_token else None)
try:
records_valid, records_message, records_status = validate_records(json_data)
finally:
clear_active_token()
records_message = records_message.replace("\nā\n", "\n")
results.append(("Records Generation Test", records_valid, records_message, records_status))
# Generate report
report = generate_validation_report(filename, json_data, results)
dataset_name = json_data.get('name', 'unnamed') if isinstance(json_data, dict) else 'unnamed'
report_filename = f"report_croissant-validation_{dataset_name}.md"
if report:
with open(report_filename, "w") as f:
f.write(report)
yield [
build_results_html(results),
gr.update(visible=False),
gr.update(visible=True) if report else gr.update(visible=False),
report if report else None,
report_filename if report else None
]
# Connect UI events to functions with updated outputs
tabs.select(
on_tab_change,
None,
[active_tab, upload_progress, validation_results, report_group, report_text, report_md, file_input, url_input]
)
file_input.change(
on_file_upload,
inputs=file_input,
outputs=[upload_progress, validation_results, report_group, report_text, report_md]
)
# Add progress state handling
def show_progress():
progress_html = """
"""
return [
gr.update(visible=False), # validation_results
gr.update(visible=True, value=progress_html), # validation_progress
gr.update(visible=False), # report_group
None, # report_text
None # report_md
]
validate_btn.click(
fn=on_validate,
inputs=file_input,
outputs=[validation_results, validation_progress, report_group, report_text, report_md]
)
fetch_btn.click(
fetch_from_url,
inputs=url_input,
outputs=[upload_progress, validation_results, report_group, report_text, report_md]
)
# Footer
gr.HTML("""
""")
gr.HTML("""
ā ļø It is possible that this validator is currently being used by a lot of people at the same time, which may trigger rate limiting by the platform hosting your data.
The app will then try again and may get into a very long loop. If it takes too long to run, we recommend using any of the following options:
- š¤ If your dataset is on Hugging Face, please log in first to avoid rate limiting issues.
- š Click the button with the three dots (āÆ) above and select "Duplicate this Space" to run this app in your own Hugging Face space.
- š» Click the button with the three dots (āÆ) above and select "Run Locally" and then "Clone (git)" to get instructions to run the checker locally. You can also use docker option (you don't need the tokens).
- š„ Run the Croissant validation code yourself (GitHub), e.g. with these scripts (validate and load).
""")
return app
if __name__ == "__main__":
app = create_ui()
app.launch()