mcp4rdf / app.py
RDF Validation Deployment
improve fixing processing
37cea3a
raw
history blame
59.4 kB
#!/usr/bin/env python3
"""
Hugging Face Gradio App for RDF Validation with MCP Server and Anthropic AI
This app serves both as a web interface and can expose MCP server functionality.
Deploy this on Hugging Face Spaces with your Anthropic API key.
"""
import gradio as gr
import os
import json
import sys
import asyncio
import logging
import requests
import re
from typing import Any, Dict, List, Optional
import threading
import time
# Add current directory to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Import our validation logic
try:
from validator import validate_rdf
VALIDATOR_AVAILABLE = True
except ImportError:
VALIDATOR_AVAILABLE = False
print("⚠️ Warning: validator.py not found. Some features may be limited.")
# Optional: Check if OpenAI and requests are available
try:
from openai import OpenAI
OPENAI_AVAILABLE = True
except ImportError:
OPENAI_AVAILABLE = False
print("πŸ’‘ Install 'openai' package for AI-powered corrections: pip install openai")
try:
import requests
HF_INFERENCE_AVAILABLE = True
except ImportError:
HF_INFERENCE_AVAILABLE = False
print("πŸ’‘ Install 'requests' package for AI-powered corrections: pip install requests")
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Configuration - Your specific Hugging Face Inference Endpoint (hardcoded)
HF_API_KEY = os.getenv('HF_API_KEY', '') # Hugging Face API key from Secret
HF_ENDPOINT_URL = "https://evxgv66ksxjlfrts.us-east-1.aws.endpoints.huggingface.cloud/v1/"
HF_MODEL = "lmstudio-community/Llama-3.3-70B-Instruct-GGUF" # Correct model name for your endpoint
# AI Correction Configuration
MAX_CORRECTION_ATTEMPTS = 5 # Increased to allow more retries
ENABLE_VALIDATION_LOOP = True # Enable validation loop by default
# OpenAI client configuration for the endpoint
def get_openai_client():
"""Get configured OpenAI client for HF Inference Endpoint"""
if not HF_API_KEY:
print("❌ No HF_API_KEY available for OpenAI client")
return None
print(f"πŸ”— Creating OpenAI client with:")
print(f" base_url: {HF_ENDPOINT_URL}")
print(f" api_key: {'***' + HF_API_KEY[-4:] if len(HF_API_KEY) > 4 else 'HIDDEN'}")
return OpenAI(
base_url=HF_ENDPOINT_URL,
api_key=HF_API_KEY,
timeout=120.0 # Increase timeout for cold starts
)
# Sample RDF data for examples
SAMPLE_VALID_RDF = '''<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:bf="http://id.loc.gov/ontologies/bibframe/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<bf:Work rdf:about="http://example.org/work/1">
<rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Text"/>
<bf:title>
<bf:Title>
<bf:mainTitle>Sample Monograph Title</bf:mainTitle>
</bf:Title>
</bf:title>
<bf:contribution>
<bf:Contribution>
<bf:agent>
<bf:Agent>
<rdfs:label>Sample Author</rdfs:label>
</bf:Agent>
</bf:agent>
<bf:role>
<bf:Role>
<rdfs:label>Author</rdfs:label>
</bf:Role>
</bf:role>
</bf:Contribution>
</bf:contribution>
<bf:language rdf:resource="http://id.loc.gov/vocabulary/languages/eng"/>
<bf:content rdf:resource="http://id.loc.gov/vocabulary/contentTypes/txt"/>
<bf:adminMetadata>
<bf:AdminMetadata>
<bf:assigner>
<bf:Organization>
<rdfs:label>Example Library</rdfs:label>
</bf:Organization>
</bf:assigner>
</bf:AdminMetadata>
</bf:adminMetadata>
</bf:Work>
</rdf:RDF>'''
SAMPLE_INVALID_RDF = '''<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!-- Missing namespace declarations -->
<!-- Missing required properties -->
<bf:Work rdf:about="http://example.org/work/1">
<bf:title>Incomplete Title</bf:title>
<!-- Missing rdf:type -->
<!-- Missing proper title structure -->
</bf:Work>
</rdf:RDF>'''
# MCP Server Tools (can be used independently)
def validate_rdf_tool(rdf_content: str, template: str = "monograph") -> dict:
"""
Validate RDF/XML content against SHACL templates.
This tool validates RDF/XML data against predefined SHACL shapes to ensure
compliance with metadata standards like BIBFRAME. Returns detailed validation
results with conformance status and specific violation information.
Args:
rdf_content (str): The RDF/XML content to validate
template (str): Validation template to use ('monograph' or 'custom')
Returns:
dict: Validation results with conformance status and detailed feedback
"""
if not rdf_content:
return {"error": "No RDF/XML content provided", "conforms": False}
if not VALIDATOR_AVAILABLE:
return {
"error": "Validator not available - ensure validator.py is present",
"conforms": False
}
try:
conforms, results_text = validate_rdf(rdf_content.encode('utf-8'), template)
return {
"conforms": conforms,
"results": results_text,
"template": template,
"status": "βœ… Valid RDF" if conforms else "❌ Invalid RDF"
}
except Exception as e:
logger.error(f"Validation error: {str(e)}")
return {
"error": f"Validation failed: {str(e)}",
"conforms": False
}
def filter_validation_results_by_class(validation_results: str, rdf_content: str) -> dict:
"""
Filter validation results by RDF class (Work, Instance, etc.)
Args:
validation_results (str): Full validation results
rdf_content (str): Original RDF content
Returns:
dict: Validation results organized by class
"""
import re
# Parse validation results to extract class information
class_results = {
'Work': [],
'Instance': [],
'Title': [],
'Contribution': [],
'Other': []
}
lines = validation_results.split('\n')
current_section = []
current_class = 'Other'
for line in lines:
# Detect which class this error relates to
if 'bf:Work' in line or '/work/' in line:
current_class = 'Work'
elif 'bf:Instance' in line or '/instance/' in line:
current_class = 'Instance'
elif 'bf:Title' in line:
current_class = 'Title'
elif 'bf:Contribution' in line:
current_class = 'Contribution'
# Collect lines for current violation
if 'Constraint Violation' in line:
if current_section:
class_results[current_class].extend(current_section)
current_section = [line]
elif line.strip():
current_section.append(line)
# Add last section
if current_section:
class_results[current_class].extend(current_section)
# Remove empty classes
return {k: '\n'.join(v) for k, v in class_results.items() if v}
def get_ai_suggestions(validation_results: str, rdf_content: str, include_warnings: bool = False) -> str:
"""Generate AI-powered, plain-language suggestions based on validation results.
Avoids RDF/SHACL jargon and focuses on actionable fixes.
"""
if not OPENAI_AVAILABLE:
return generate_manual_suggestions(validation_results)
current_api_key = os.getenv('HF_API_KEY', '')
if not current_api_key:
return f"""
πŸ”‘ **AI suggestions disabled**: Please set your Hugging Face API key as a Secret in your Space settings.
{generate_manual_suggestions(validation_results)}
"""
try:
client = get_openai_client()
if not client:
return f"""
πŸ”‘ **AI suggestions disabled**: HF_API_KEY not configured.
{generate_manual_suggestions(validation_results)}
"""
severity_instruction = (
"Focus only on violations (errors) and ignore any warnings."
if not include_warnings else
"Address both violations and warnings."
)
# Group errors by class to focus the prompt
class_results = filter_validation_results_by_class(validation_results, rdf_content)
if class_results:
primary_class = max(class_results.keys(), key=lambda k: len(class_results[k]))
focused_results = class_results[primary_class]
else:
primary_class = "Record"
focused_results = validation_results
simplified_summary = parse_shacl_results_for_ai(focused_results)
relevant_rdf = extract_relevant_rdf_section(rdf_content, primary_class)
prompt = f"""
You are a helpful metadata librarian. Write in plain language (no RDF/SHACL jargon). Analyze the validation errors for the {primary_class} and provide concise, actionable fixes.
{severity_instruction}
Validation Errors for {primary_class}:
{focused_results[:1500]}
Validation Summary (plain language):
{simplified_summary}
Relevant RDF Section:
{relevant_rdf[:800]}
Instructions:
1. ONE sentence: What's wrong with this {primary_class}?
2. List errors (max 3 words each)
3. Show exact XML fixes
Format:
**Issue:** [One sentence about the {primary_class} problem]
**Errors:**
β€’ Error 1
β€’ Error 2
**Fix:**
```xml
[Complete corrected {primary_class} section]
```
Be ultra-concise. Show the fix, not explanations."""
chat_completion = client.chat.completions.create(
model=HF_MODEL,
messages=[
{
"role": "system",
"content": "You are a friendly librarian helping fix catalog records. Never use technical RDF or SHACL terminology."
},
{
"role": "user",
"content": prompt
}
],
max_tokens=800,
temperature=0.5,
top_p=0.9
)
generated_text = chat_completion.choices[0].message.content
generated_text = clean_technical_jargon(generated_text)
other_classes = [k for k in class_results.keys() if k != primary_class]
class_note = (
f"\n\nπŸ“Œ **Note:** Focused on {primary_class} errors. " +
(f"Also found issues in: {', '.join(other_classes)}" if other_classes else "")
)
return f"πŸ€– **AI-Powered Suggestions ({('Violations + Warnings' if include_warnings else 'Violations Only')}):**\n\n{generated_text}{class_note}"
except Exception as e:
logger.error(f"OpenAI/HF Inference Endpoint error: {str(e)}")
return f"""
❌ **AI suggestions error**: {str(e)}
{generate_manual_suggestions(validation_results)}
"""
def extract_relevant_rdf_section(rdf_content: str, class_name: str) -> str:
"""
Extract only the relevant RDF section for a specific class
Args:
rdf_content (str): Full RDF content
class_name (str): Class name to extract (Work, Instance, etc.)
Returns:
str: Relevant RDF section
"""
import re
# Map class names to RDF patterns
patterns = {
'Work': r'<bf:Work.*?</bf:Work>',
'Instance': r'<bf:Instance.*?</bf:Instance>',
'Title': r'<bf:Title.*?</bf:Title>',
'Contribution': r'<bf:Contribution.*?</bf:Contribution>'
}
pattern = patterns.get(class_name)
if not pattern:
return rdf_content[:1000] # Fallback to first 1000 chars
# Extract matching section
match = re.search(pattern, rdf_content, re.DOTALL)
if match:
section = match.group(0)
# Also include namespace declarations
namespaces = re.findall(r'xmlns:\w+="[^"]*"', rdf_content[:500])
if namespaces:
return f"<!-- Namespaces: {' '.join(namespaces[:3])} -->\n{section}"
return section
return rdf_content[:1000] # Fallback
def get_ai_correction(validation_results: str, rdf_content: str, template: str = 'monograph', max_attempts: int = None, include_warnings: bool = False, enable_validation_loop: bool | None = None, steps_log: Optional[List[str]] = None) -> str:
"""
Generate AI-powered corrected RDF/XML based on validation errors.
This tool takes invalid RDF/XML and validation results, then generates
a corrected version that addresses all identified validation issues.
The generated correction is validated before being returned to the user.
Args:
validation_results (str): The validation error messages
rdf_content (str): The original invalid RDF/XML content
template (str): The validation template to use
max_attempts (int): Maximum number of attempts to generate valid RDF (uses MAX_CORRECTION_ATTEMPTS if None)
include_warnings (bool): Whether to fix warnings in addition to violations
Returns:
str: Corrected RDF/XML that should pass validation
"""
# Determine whether to iterate based on parameter or global default
iterate_enabled = ENABLE_VALIDATION_LOOP if enable_validation_loop is None else enable_validation_loop
if steps_log is not None:
steps_log.append(f"Planning correction: iterate_enabled={iterate_enabled}, include_warnings={include_warnings}")
# Use configuration default if not specified
if max_attempts is None:
max_attempts = MAX_CORRECTION_ATTEMPTS
if steps_log is not None:
steps_log.append(f"Max attempts set to {max_attempts}")
# If iteration disabled, force single attempt
if not iterate_enabled:
max_attempts = 1
if steps_log is not None:
steps_log.append("Iteration disabled; forcing single attempt")
if not OPENAI_AVAILABLE:
if steps_log is not None:
steps_log.append("OPENAI client not available; falling back to manual hints")
return generate_manual_correction_hints(validation_results, rdf_content)
# Get API key dynamically at runtime
current_api_key = os.getenv('HF_API_KEY', '')
if not current_api_key:
if steps_log is not None:
steps_log.append("HF_API_KEY not set; cannot call model; returning manual hints")
return f"""<!-- AI correction disabled: Set HF_API_KEY as a Secret in your Space settings -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
try:
client = get_openai_client()
if not client:
if steps_log is not None:
steps_log.append("Failed to initialize OpenAI client; returning manual hints")
return f"""<!-- AI correction disabled: HF_API_KEY not configured -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
# Add timeout protection
import time
start_time = time.time()
timeout = 120 # Increased to 120 second total timeout
if steps_log is not None:
steps_log.append(f"Timeout budget: {timeout}s total")
severity_instruction = "Fix only the violations (errors) and ignore any warnings." if not include_warnings else "Fix both violations and warnings."
# Filter validation results by class
class_results = filter_validation_results_by_class(validation_results, rdf_content)
# Process each class separately to avoid overwhelming the LLM
corrected_sections = {}
for class_name, class_errors in class_results.items():
if not class_errors:
continue
# Check timeout
if time.time() - start_time > timeout - 10:
print(f"⏰ Approaching timeout, skipping {class_name}")
break
print(f"πŸ”„ Correcting {class_name} section")
# Extract relevant section
relevant_section = extract_relevant_rdf_section(rdf_content, class_name)
prompt = f"""Fix this {class_name} RDF section based on these specific errors.
{severity_instruction}
Errors for {class_name}:
{class_errors[:800]}
Current {class_name} RDF:
{relevant_section[:800]}
Return ONLY the corrected {class_name} XML section. No explanations."""
try:
chat_completion = client.chat.completions.create(
model=HF_MODEL,
messages=[
{
"role": "user",
"content": prompt
}
],
max_tokens=1000,
temperature=0.3,
timeout=45 # Increased per-section timeout
)
corrected_section = chat_completion.choices[0].message.content.strip()
corrected_sections[class_name] = extract_rdf_from_response(corrected_section)
except Exception as e:
print(f"❌ Error correcting {class_name}: {str(e)}")
continue
# Merge corrections back into original RDF
if corrected_sections:
corrected_rdf = merge_corrected_sections(rdf_content, corrected_sections)
return f"""<!-- AI-generated correction (class-based processing) -->
{corrected_rdf}"""
else:
return f"""<!-- AI correction failed - timeout or errors -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
except Exception as e:
logger.error(f"LLM API error: {str(e)}")
return f"""<!-- Error generating AI correction: {str(e)} -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
def merge_corrected_sections(original_rdf: str, corrected_sections: dict) -> str:
"""
Merge corrected class sections back into the original RDF
Args:
original_rdf (str): Original RDF content
corrected_sections (dict): Corrected sections by class
Returns:
str: Merged RDF with corrections
"""
import re
result = original_rdf
# Replace each corrected section
for class_name, corrected_section in corrected_sections.items():
patterns = {
'Work': r'<bf:Work.*?</bf:Work>',
'Instance': r'<bf:Instance.*?</bf:Instance>',
'Title': r'<bf:Title.*?</bf:Title>',
'Contribution': r'<bf:Contribution.*?</bf:Contribution>'
}
pattern = patterns.get(class_name)
if pattern:
result = re.sub(pattern, corrected_section, result, count=1, flags=re.DOTALL)
return result
# Sample RDF data for examples
# MCP Server Tools (can be used independently)
# Note: This section exists earlier in the file, we're removing the duplicates
"""
Generate AI-powered fix suggestions for invalid RDF/XML.
This tool analyzes validation results and provides actionable suggestions
for fixing RDF/XML validation errors using AI or rule-based analysis.
Args:
validation_results (str): The validation error messages
rdf_content (str): The original RDF/XML content that failed validation
include_warnings (bool): Whether to include warnings in suggestions
Returns:
str: Detailed suggestions for fixing the RDF validation issues
"""
if not OPENAI_AVAILABLE:
return generate_manual_suggestions(validation_results)
# Get API key dynamically at runtime
current_api_key = os.getenv('HF_API_KEY', '')
if not current_api_key:
return f"""
πŸ”‘ **AI suggestions disabled**: Please set your Hugging Face API key as a Secret in your Space settings.
{generate_manual_suggestions(validation_results)}
"""
try:
# Use OpenAI client with your Hugging Face Inference Endpoint
client = get_openai_client()
if not client:
return f"""
πŸ”‘ **AI suggestions disabled**: HF_API_KEY not configured.
{generate_manual_suggestions(validation_results)}
"""
severity_instruction = "Focus only on violations (errors) and ignore any warnings." if not include_warnings else "Address both violations and warnings."
prompt = f"""You are an expert in RDF/XML and SHACL validation. Analyze the validation errors and provide CONCISE, ACTIONABLE fix suggestions.
{severity_instruction}
Validation Results:
{validation_results}
Original RDF (first 1000 chars):
{rdf_content[:1000]}...
Instructions:
1. Start with a ONE-SENTENCE summary of the main issue
2. List the specific errors in bullet points (max 5 words per error)
3. Provide the exact fix for each error with code snippets
4. Keep explanations minimal - focus on solutions
Format:
**Main Issue:** [One sentence]
**Errors Found:**
β€’ Error 1 name
β€’ Error 2 name
**Fixes:**
1. **Error 1**:
```xml
[exact code to add/fix]
```
2. **Error 2**:
```xml
[exact code to add/fix]
```
Be direct and solution-focused. No lengthy explanations."""
# Make API call using OpenAI client
print(f"πŸ”„ Making API call to: {HF_ENDPOINT_URL}")
print(f"πŸ”„ Using model: {HF_MODEL}")
print(f"πŸ”„ Include warnings: {include_warnings}")
chat_completion = client.chat.completions.create(
model=HF_MODEL,
messages=[
{
"role": "system",
"content": "You are a friendly librarian helping fix catalog records. Never use technical RDF or SHACL terminology."
},
{
"role": "user",
"content": prompt
}
],
max_tokens=1500,
temperature=0.6,
top_p=0.9
)
print("βœ… API call successful")
generated_text = chat_completion.choices[0].message.content
return f"πŸ€– **AI-Powered Suggestions ({('Violations + Warnings' if include_warnings else 'Violations Only')}):**\n\n{generated_text}"
except Exception as e:
logger.error(f"OpenAI/HF Inference Endpoint error: {str(e)}")
return f"""
❌ **AI suggestions error**: {str(e)}
{generate_manual_suggestions(validation_results)}
"""
def extract_rdf_from_response(response: str) -> str:
"""
Extract RDF/XML content from AI response, handling code blocks.
Args:
response (str): AI response that may contain RDF wrapped in code blocks
Returns:
str: Extracted RDF/XML content
"""
response = response.strip()
# Handle ```xml code blocks
if "```xml" in response:
try:
return response.split("```xml")[1].split("```")[0].strip()
except IndexError:
pass
# Handle generic ``` code blocks
if "```" in response and response.count("```") >= 2:
try:
return response.split("```")[1].split("```")[0].strip()
except IndexError:
pass
# If no code blocks found, return the response as-is
return response
def get_ai_correction(validation_results: str, rdf_content: str, template: str = 'monograph', max_attempts: int = None, include_warnings: bool = False, enable_validation_loop: bool | None = None, steps_log: Optional[List[str]] = None) -> str:
"""
Generate AI-powered corrected RDF/XML based on validation errors.
This tool takes invalid RDF/XML and validation results, then generates
a corrected version that addresses all identified validation issues.
The generated correction is validated before being returned to the user.
Args:
validation_results (str): The validation error messages
rdf_content (str): The original invalid RDF/XML content
template (str): The validation template to use
max_attempts (int): Maximum number of attempts to generate valid RDF (uses MAX_CORRECTION_ATTEMPTS if None)
include_warnings (bool): Whether to fix warnings in addition to violations
Returns:
str: Corrected RDF/XML that should pass validation
"""
# Determine whether to iterate based on parameter or global default
iterate_enabled = ENABLE_VALIDATION_LOOP if enable_validation_loop is None else enable_validation_loop
if steps_log is not None:
steps_log.append(f"Planning correction: iterate_enabled={iterate_enabled}, include_warnings={include_warnings}")
# Use configuration default if not specified
if max_attempts is None:
max_attempts = MAX_CORRECTION_ATTEMPTS
if steps_log is not None:
steps_log.append(f"Max attempts set to {max_attempts}")
# If iteration disabled, force single attempt
if not iterate_enabled:
max_attempts = 1
if steps_log is not None:
steps_log.append("Iteration disabled; forcing single attempt")
if not OPENAI_AVAILABLE:
if steps_log is not None:
steps_log.append("OPENAI client not available; falling back to manual hints")
return generate_manual_correction_hints(validation_results, rdf_content)
# Get API key dynamically at runtime
current_api_key = os.getenv('HF_API_KEY', '')
if not current_api_key:
if steps_log is not None:
steps_log.append("HF_API_KEY not set; cannot call model; returning manual hints")
return f"""<!-- AI correction disabled: Set HF_API_KEY as a Secret in your Space settings -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
try:
client = get_openai_client()
if not client:
if steps_log is not None:
steps_log.append("Failed to initialize OpenAI client; returning manual hints")
return f"""<!-- AI correction disabled: HF_API_KEY not configured -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
# Add timeout protection
import time
start_time = time.time()
timeout = 120 # Increased to 120 second total timeout
if steps_log is not None:
steps_log.append(f"Timeout budget: {timeout}s total")
severity_instruction = "Fix only the violations (errors) and ignore any warnings." if not include_warnings else "Fix both violations and warnings."
# Try multiple attempts to generate valid RDF
for attempt in range(max_attempts):
# Check timeout
elapsed = time.time() - start_time
if elapsed > timeout:
if steps_log is not None:
steps_log.append(f"Timeout reached after {int(elapsed)}s; stopping attempts")
print(f"⏰ Timeout reached after {timeout} seconds")
break
attempt_no = attempt + 1
if steps_log is not None:
steps_log.append(f"Attempt {attempt_no}/{max_attempts}: requesting model correction")
print(f"πŸ”„ Correction attempt {attempt_no}/{max_attempts}")
prompt = f"""You are an expert in RDF/XML. Fix the following RDF/XML based on the validation errors provided.
{severity_instruction}
Validation Errors:
{validation_results}
Original RDF/XML:
{rdf_content}
{f"Previous attempt {attempt} still had validation errors. Please fix ALL issues this time." if attempt > 0 else ""}
Please provide the corrected RDF/XML that addresses all validation issues.
- Return only the corrected XML without additional explanation
- Maintain the original structure as much as possible while fixing errors
- Ensure all namespace declarations are present
- Add any missing required properties
- Fix any syntax or structural issues"""
try:
chat_completion = client.chat.completions.create(
model=HF_MODEL,
messages=[
{
"role": "user",
"content": prompt
}
],
max_tokens=2000,
temperature=0.3,
timeout=60 # Increased to 60 second timeout per API call
)
corrected_rdf = chat_completion.choices[0].message.content.strip()
if steps_log is not None:
steps_log.append(f"Attempt {attempt_no}: model responded; extracting XML block if present")
# Extract RDF content if it's wrapped in code blocks
corrected_rdf = extract_rdf_from_response(corrected_rdf)
# Only validate if we have the validator and haven't hit timeout
if VALIDATOR_AVAILABLE and (time.time() - start_time < timeout - 10):
try:
# Quick validation check
conforms, new_results = validate_rdf(corrected_rdf.encode('utf-8'), template)
if conforms:
if steps_log is not None:
steps_log.append(f"Attempt {attempt_no}: correction PASSED validation")
print(f"βœ… Correction validated successfully on attempt {attempt_no}")
return f"""<!-- AI-generated correction validated successfully -->
{corrected_rdf}"""
else:
if steps_log is not None:
steps_log.append(f"Attempt {attempt_no}: still invalid; will retry with updated errors")
print(f"❌ Correction attempt {attempt_no} still has validation errors")
# Update validation_results for next attempt
validation_results = new_results
except Exception as e:
if steps_log is not None:
steps_log.append(f"Attempt {attempt_no}: error during validation: {str(e)} β€” returning correction anyway")
print(f"⚠️ Error validating correction attempt {attempt_no}: {str(e)}")
# If validation fails, return the correction anyway
return f"""<!-- AI-generated correction (validation check failed) -->
{corrected_rdf}"""
else:
# If validator not available or timeout approaching, return the correction
if steps_log is not None:
steps_log.append("Skipping validation check (validator unavailable or timeout)")
print("⚠️ Returning correction without validation")
return f"""<!-- AI-generated correction (validation skipped) -->
{corrected_rdf}"""
except Exception as api_error:
if steps_log is not None:
steps_log.append(f"Attempt {attempt_no}: API error: {str(api_error)}")
print(f"❌ API error on attempt {attempt_no}: {str(api_error)}")
if attempt == max_attempts - 1: # Last attempt
raise api_error
continue
# All attempts failed or timed out
if steps_log is not None:
steps_log.append("All attempts failed or timed out; returning manual hints")
return f"""<!-- AI correction failed after {max_attempts} attempts or timeout -->
<!-- Please correct manually using the validation results as a guide -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
except Exception as e:
logger.error(f"LLM API error: {str(e)}")
if steps_log is not None:
steps_log.append(f"Fatal error invoking model: {str(e)}")
return f"""<!-- Error generating AI correction: {str(e)} -->
{generate_manual_correction_hints(validation_results, rdf_content)}"""
def generate_manual_suggestions(validation_results: str) -> str:
"""Generate generic, pattern-based suggestions when AI is not available.
Note: Avoid hardcoding SHACL rules or specific property requirements; rely only on
patterns present in the validation output text.
"""
vr_lower = validation_results.lower() if validation_results else ""
suggestions: List[str] = []
# Missing/required
if ("mincount" in vr_lower) or ("missing" in vr_lower) or ("required" in vr_lower):
suggestions.append("β€’ Some required fields are missing. Add the missing information where indicated.")
# Too many values
if ("maxcount" in vr_lower) or ("too many" in vr_lower) or ("more than allowed" in vr_lower):
suggestions.append("β€’ Some fields have too many values. Keep only the main/one value as required.")
# Datatype/format issues
if ("datatype" in vr_lower) or ("type mismatch" in vr_lower) or ("expected" in vr_lower and "datatype" in vr_lower):
suggestions.append("β€’ Some values are in the wrong format. Use the expected format (e.g., dates like YYYY-MM-DD).")
# URI/identifier issues
if ("iri" in vr_lower) or ("uri" in vr_lower) or ("identifier" in vr_lower and "invalid" in vr_lower):
suggestions.append("β€’ Some identifiers look malformed. Use complete, valid web addresses or proper identifiers.")
# Namespace/prefix issues
if ("namespace" in vr_lower) or ("prefix" in vr_lower):
suggestions.append("β€’ Define all XML namespace prefixes at the top and use them consistently.")
# XML syntax/structure
if ("xml" in vr_lower) or ("syntax" in vr_lower) or ("well-formed" in vr_lower):
suggestions.append("β€’ Fix XML structure issues (unclosed tags, invalid characters, or nesting problems).")
# Fallback
if not suggestions:
suggestions.append("β€’ Review the validation details and update the record where issues are highlighted.")
suggestions.append("β€’ Follow the selected template; add missing fields and correct formats as needed.")
suggestions_text = "\n".join(suggestions)
return f"""
πŸ“‹ **What needs fixing:**
{suggestions_text}
πŸ’‘ **Quick tips:**
β€’ Include required fields when noted
β€’ Keep single-value fields to one value
β€’ Use the expected formats (e.g., for dates)
β€’ Declare and use XML namespace prefixes consistently
β€’ Ensure the XML is well‑formed
Need help? Load an example and compare the structure.
"""
def clean_technical_jargon(text: str) -> str:
"""Replace technical RDF/SHACL terms with plain language for end users."""
if not text:
return text
replacements = {
# RDF/SHACL jargon
"URIRef": "identifier",
"URI": "identifier",
"IRI": "identifier",
"Literal": "text value",
"triple": "field entry",
"graph": "dataset",
"node": "record",
"subject": "record",
"predicate": "field type",
"object": "value",
"SHACL": "validation",
"constraint": "rule",
"conformance": "compliance",
"violation": "issue",
"sh:": "",
"rdf:": "",
"rdfs:": "",
"xsd:": "",
# Tone softening
"Error:": "Issue:",
"Invalid": "Incorrect",
"Failed": "Did not pass",
"Missing": "Not found",
}
cleaned = text
for k, v in replacements.items():
cleaned = cleaned.replace(k, v)
return cleaned
def parse_shacl_results_for_ai(results_text: str) -> str:
"""Simplify SHACL results into clearer sentences for AI processing.
Pattern-based only; does not depend on any SHACL rule definitions.
"""
if not results_text:
return ""
import re
simplified: List[str] = []
# Generic patterns
patterns = [
(re.compile(r"minCount", re.IGNORECASE), "A required field is missing."),
(re.compile(r"maxCount", re.IGNORECASE), "A field has more values than allowed; only one may be permitted."),
(re.compile(r"datatype", re.IGNORECASE), "A field has a value in the wrong format."),
(re.compile(r"iri|uri", re.IGNORECASE), "An identifier looks malformed or incomplete."),
(re.compile(r"namespace|prefix", re.IGNORECASE), "A namespace prefix is undefined or inconsistent."),
(re.compile(r"xml|syntax|well-formed", re.IGNORECASE), "The XML structure has an error (e.g., unclosed tag)."),
]
lines = [ln.strip() for ln in results_text.splitlines() if ln.strip()]
for ln in lines:
matched = False
for regex, message in patterns:
if regex.search(ln):
simplified.append(message)
matched = True
break
if not matched and ("Constraint Violation" in ln or "Violation" in ln):
simplified.append("A record rule was not met.")
# Deduplicate while preserving order
seen = set()
unique = []
for s in simplified:
if s not in seen:
unique.append(s)
seen.add(s)
return "\n".join(unique) if unique else results_text
def generate_manual_correction_hints(validation_results: str, rdf_content: str) -> str:
"""Generate manual correction hints when AI is not available"""
return f"""<!-- Manual correction hints based on validation results -->
<!-- Set HF_API_KEY as a Secret in your Space settings for AI-powered corrections -->
{rdf_content}
<!--
VALIDATION ISSUES FOUND:
{validation_results[:500]}...
MANUAL CORRECTION STEPS:
1. Add missing namespace declarations
2. Include required properties (rdf:type, etc.)
3. Fix XML syntax errors
4. Ensure proper URI formats
5. Validate data types
-->"""
def extract_xml_from_text(text: str) -> str:
"""Extract RDF/XML from model output that may include extra formatting.
Looks for the first <rdf:RDF ...> ... </rdf:RDF> block. If not found,
returns the original text unchanged.
"""
if not text:
return text
import re
# Try to capture XML block even if fenced in code blocks
# Use DOTALL to span multiple lines
pattern = re.compile(r"<rdf:RDF[\s\S]*?</rdf:RDF>", re.IGNORECASE)
m = pattern.search(text)
if m:
return m.group(0)
# Strip common markdown fences if present
fenced = re.sub(r"^```[a-zA-Z]*\n|```$", "", text.strip())
return fenced if fenced else text
# --- Namespace and wrapper helpers to avoid XML parser errors ---
STANDARD_NAMESPACES = {
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"bf": "http://id.loc.gov/ontologies/bibframe/",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
}
def _extract_declared_namespaces(xml_text: str) -> dict:
import re
decls = {}
for prefix, uri in re.findall(r"xmlns:([A-Za-z0-9_-]+)=\"([^\"]+)\"", xml_text[:2000]):
decls[prefix] = uri
return decls
def _detect_used_prefixes(xml_text: str) -> set:
import re
used = set()
# Tag prefixes like <bf:Work ...> and attribute prefixes like rdf:type="..."
for m in re.finditer(r"<\s*([A-Za-z0-9_-]+):[A-Za-z0-9_-]+", xml_text):
used.add(m.group(1))
for m in re.finditer(r"\s([A-Za-z0-9_-]+):[A-Za-z0-9_-]+=", xml_text):
used.add(m.group(1))
return used
def ensure_rdf_wrapper_and_namespaces(xml_text: str, original_text: Optional[str] = None, steps_log: Optional[List[str]] = None) -> str:
"""Ensure the XML has an <rdf:RDF> wrapper and required xmlns declarations for used prefixes.
- If wrapper exists, add any missing xmlns: declarations for standard, used prefixes.
- If wrapper is missing, wrap the content and include standard namespaces for used prefixes.
"""
if not xml_text or not isinstance(xml_text, str):
return xml_text
import re
declared = _extract_declared_namespaces(xml_text)
if original_text:
# Merge any declarations present in the original input
declared.update(_extract_declared_namespaces(original_text))
used = _detect_used_prefixes(xml_text)
# Always consider rdf used for wrapper
used.add("rdf")
# Only inject namespaces for known standards to avoid guessing
missing = [p for p in used if p not in declared and p in STANDARD_NAMESPACES]
added_attrs = " ".join([f"xmlns:{p}=\"{STANDARD_NAMESPACES[p]}\"" for p in missing])
has_wrapper = bool(re.search(r"<rdf:RDF[^>]*>", xml_text))
updated = xml_text
if has_wrapper:
if added_attrs:
# Inject before the closing '>' of the first <rdf:RDF ...>
def _inject(match):
start_tag = match.group(0)
if start_tag.endswith('>'):
return start_tag[:-1] + ' ' + added_attrs + '>'
return start_tag + ' ' + added_attrs
updated = re.sub(r"<rdf:RDF[^>]*>", _inject, updated, count=1)
if steps_log is not None and missing:
steps_log.append(f"Injected missing namespace declarations: {', '.join(missing)}")
else:
# Build a wrapper with standard namespaces for used prefixes we know
attrs = [f"xmlns:rdf=\"{STANDARD_NAMESPACES['rdf']}\""]
for p in used:
if p == 'rdf':
continue
uri = declared.get(p) or STANDARD_NAMESPACES.get(p)
if uri:
attrs.append(f"xmlns:{p}=\"{uri}\"")
wrapper_open = "<rdf:RDF " + " ".join(attrs) + ">\n"
wrapper_close = "\n</rdf:RDF>"
updated = wrapper_open + xml_text + wrapper_close
if steps_log is not None:
steps_log.append("Wrapped snippet in <rdf:RDF> with standard namespace declarations")
return updated
def validate_rdf_interface(rdf_content: str, template: str, use_ai: bool = True, include_warnings: bool = False, iterate_until_valid: bool = True, max_attempts: int = 5, show_steps: bool = True):
"""Main validation function for Gradio interface"""
if not rdf_content.strip():
return "❌ Error", "No RDF/XML data provided", "", "", "", "", ""
# Validate RDF
prepped_input = ensure_rdf_wrapper_and_namespaces(rdf_content)
result = validate_rdf_tool(prepped_input, template)
if "error" in result:
return f"❌ Error: {result['error']}", "", "", "", "", "", ""
status = result["status"]
results_text = result["results"]
# Filter results if warnings should be excluded
filtered_results = results_text
if not include_warnings and "Warning" in results_text:
# Split results into lines and filter out warnings
lines = results_text.split('\n')
filtered_lines = []
skip_until_next_section = False
for line in lines:
if "Warning" in line and ("Constraint Violation" in line or "sh:Warning" in line):
skip_until_next_section = True
elif "Constraint Violation" in line and "Warning" not in line:
skip_until_next_section = False
filtered_lines.append(line)
elif not skip_until_next_section:
filtered_lines.append(line)
filtered_results = '\n'.join(filtered_lines)
corrected_status = ""
corrected_results = ""
steps_log: List[str] = []
steps_log.append(f"Initial validation: {'PASSED' if result['conforms'] else 'FAILED'} using template '{template}'")
if not include_warnings:
steps_log.append("Configured to ignore warnings in AI processing")
if iterate_until_valid:
steps_log.append(f"Iteration enabled with max_attempts={max_attempts}")
if result["conforms"]:
suggestions = "βœ… No issues found! Your RDF/XML is valid according to the selected template."
corrected_rdf = ""
corrected_status = "β€”"
corrected_results = ""
steps_log.append("No correction needed; record already conforms")
else:
if use_ai:
# Pass filtered results to AI functions
suggestions = get_ai_suggestions(filtered_results, rdf_content, include_warnings)
steps_log.append("Requested AI suggestions for concise guidance")
corrected_rdf = get_ai_correction(
filtered_results,
rdf_content,
template,
max_attempts=max_attempts,
include_warnings=include_warnings,
enable_validation_loop=iterate_until_valid,
steps_log=steps_log,
)
# Attempt re-validation of corrected RDF
try:
corrected_xml = extract_xml_from_text(corrected_rdf)
corrected_xml = ensure_rdf_wrapper_and_namespaces(corrected_xml, original_text=prepped_input, steps_log=steps_log)
reval = validate_rdf_tool(corrected_xml, template)
if "error" in reval:
corrected_status = f"❌ Re-validation Error: {reval['error']}"
corrected_results = ""
steps_log.append(f"Re-validation failed with error: {reval['error']}")
else:
corrected_status = reval.get("status", "")
corrected_results = reval.get("results", "")
steps_log.append(f"Re-validation: {corrected_status}")
except Exception as re_ex:
corrected_status = f"❌ Re-validation Error: {re_ex}"
corrected_results = ""
steps_log.append(f"Re-validation error: {re_ex}")
else:
suggestions = generate_manual_suggestions(filtered_results)
corrected_rdf = generate_manual_correction_hints(filtered_results, rdf_content)
corrected_status = "β€”"
corrected_results = ""
steps_log.append("AI disabled; produced manual suggestions and hints")
steps_text = "\n".join(steps_log) if show_steps else ""
return status, results_text, suggestions, steps_text, corrected_rdf, corrected_status, corrected_results
def get_rdf_examples(example_type: str = "valid") -> str:
"""
Retrieve example RDF/XML snippets for testing and learning.
This tool provides sample RDF/XML content that can be used to test
the validation system or learn proper RDF structure. Examples include
valid BibFrame Work records, invalid records for testing corrections,
and BibFrame Instance records.
Args:
example_type (str): Type of example to retrieve. Options:
- 'valid': A complete, valid BibFrame Work record
- 'invalid': An incomplete BibFrame Work with validation errors
- 'bibframe': A BibFrame Instance record example
Returns:
str: Complete RDF/XML example content ready for validation testing
"""
examples = {
"valid": SAMPLE_VALID_RDF,
"invalid": SAMPLE_INVALID_RDF,
"bibframe": '''<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:bf="http://id.loc.gov/ontologies/bibframe/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<bf:Instance rdf:about="http://example.org/instance/1">
<rdf:type rdf:resource="http://id.loc.gov/ontologies/bibframe/Print"/>
<bf:instanceOf rdf:resource="http://example.org/work/1"/>
<bf:title>
<bf:Title>
<bf:mainTitle>Example Book Title</bf:mainTitle>
</bf:Title>
</bf:title>
<bf:provisionActivity>
<bf:Publication>
<bf:date>2024</bf:date>
<bf:place>
<bf:Place>
<rdfs:label>New York</rdfs:label>
</bf:Place>
</bf:place>
</bf:Publication>
</bf:provisionActivity>
</bf:Instance>
</rdf:RDF>'''
}
return examples.get(example_type, examples["valid"])
# Create Gradio Interface
def create_interface():
"""Create the main Gradio interface"""
# Check API key status dynamically
current_api_key = os.getenv('HF_API_KEY', '')
api_status = "πŸ”‘ AI features enabled" if (OPENAI_AVAILABLE and current_api_key) else "⚠️ AI features disabled (set HF_API_KEY)"
with gr.Blocks(
title="RDF Validation Server with AI",
theme=gr.themes.Soft(),
css="""
.status-box {
font-weight: bold;
padding: 10px;
border-radius: 5px;
}
.header-text {
text-align: center;
padding: 20px;
}
"""
) as demo:
# Header
debug_info = f"""
Debug Info:
- OPENAI_AVAILABLE: {OPENAI_AVAILABLE}
- HF_INFERENCE_AVAILABLE: {HF_INFERENCE_AVAILABLE}
- HF_API_KEY set: {'Yes' if current_api_key else 'No'}
- HF_API_KEY length: {len(current_api_key) if current_api_key else 0}
- HF_ENDPOINT_URL: {HF_ENDPOINT_URL}
- HF_MODEL: {HF_MODEL}
"""
gr.HTML(f"""
<div class="header-text">
<h1>πŸ” RDF Validation Server with AI</h1>
<p>Validate RDF/XML against SHACL schemas with AI-powered suggestions and corrections</p>
<p><strong>Status:</strong> {api_status}</p>
<details><summary>Debug Info</summary><pre>{debug_info}</pre></details>
</div>
""")
# Main interface
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### πŸ“ Input")
rdf_input = gr.Textbox(
label="RDF/XML Content",
placeholder="Paste your RDF/XML content here...",
lines=15,
show_copy_button=True
)
with gr.Row():
template_dropdown = gr.Dropdown(
label="Validation Template",
choices=["monograph", "custom"],
value="monograph",
info="Select the SHACL template to validate against"
)
use_ai_checkbox = gr.Checkbox(
label="Use AI Features",
value=True,
info="Enable AI-powered suggestions and corrections"
)
include_warnings_checkbox = gr.Checkbox(
label="Include Warnings",
value=False,
info="Include warnings in AI corrections (violations only by default)"
)
with gr.Row():
iterate_checkbox = gr.Checkbox(
label="Iterate until valid",
value=True,
info="Try multiple correction attempts until validation passes or attempts run out"
)
max_attempts_slider = gr.Slider(
label="Max attempts",
minimum=1,
maximum=5,
value=5,
step=1,
info="Maximum number of correction attempts when iterating"
)
show_steps_checkbox = gr.Checkbox(
label="Show steps",
value=True,
info="Display step-by-step process (turn off for a simpler response)"
)
validate_btn = gr.Button("πŸ” Validate RDF", variant="primary", size="lg")
# Examples and controls
gr.Markdown("### πŸ“š Examples & Tools")
with gr.Row():
example1_btn = gr.Button("βœ… Valid RDF Example", variant="secondary")
example2_btn = gr.Button("❌ Invalid RDF Example", variant="secondary")
clear_btn = gr.Button("πŸ—‘οΈ Clear All", variant="stop")
# Results section
with gr.Row():
with gr.Column():
gr.Markdown("### πŸ“Š Results")
status_output = gr.Textbox(
label="Validation Status",
interactive=False,
lines=1,
elem_classes=["status-box"]
)
results_output = gr.Textbox(
label="Detailed Validation Results",
interactive=False,
lines=8,
show_copy_button=True
)
suggestions_output = gr.Textbox(
label="πŸ’‘ Fix Suggestions",
interactive=False,
lines=8,
show_copy_button=True
)
steps_output = gr.Textbox(
label="🧭 Correction Steps",
interactive=False,
lines=10,
show_copy_button=True,
placeholder="Step-by-step log of how the system derived the corrected XML"
)
# Corrected RDF section
with gr.Row():
with gr.Column():
gr.Markdown("### πŸ› οΈ AI-Generated Corrections")
corrected_output = gr.Textbox(
label="Corrected RDF/XML",
interactive=False,
lines=15,
show_copy_button=True,
placeholder="Corrected RDF will appear here after validation..."
)
with gr.Row():
corrected_status_output = gr.Textbox(
label="Re-validation Status (Corrected RDF)",
interactive=False,
lines=1,
elem_classes=["status-box"]
)
corrected_results_output = gr.Textbox(
label="Re-validation Details",
interactive=False,
lines=6,
show_copy_button=True
)
# Event handlers
validate_btn.click(
fn=validate_rdf_interface,
inputs=[rdf_input, template_dropdown, use_ai_checkbox, include_warnings_checkbox, iterate_checkbox, max_attempts_slider, show_steps_checkbox],
outputs=[status_output, results_output, suggestions_output, steps_output, corrected_output, corrected_status_output, corrected_results_output]
)
# Remove auto-validation to prevent processing loops
# rdf_input.change(
# fn=validate_rdf_interface,
# inputs=[rdf_input, template_dropdown, use_ai_checkbox],
# outputs=[status_output, results_output, suggestions_output, corrected_output]
# )
# Example buttons
example1_btn.click(
lambda: get_rdf_examples("valid"),
outputs=[rdf_input]
)
example2_btn.click(
lambda: get_rdf_examples("invalid"),
outputs=[rdf_input]
)
clear_btn.click(
lambda: ("", "", "", "", "", "", "", ""),
outputs=[rdf_input, status_output, results_output, suggestions_output, steps_output, corrected_output, corrected_status_output, corrected_results_output]
)
# Footer with instructions
gr.Markdown("""
---
### πŸš€ **Deployment Instructions for Hugging Face Spaces:**
1. **Create a new Space** on [Hugging Face](https://huggingface.co/spaces)
2. **Set up your Hugging Face Inference Endpoint** and get the endpoint URL
3. **Set your tokens** in Space settings (use Secrets for security):
- Go to Settings β†’ Repository secrets
- Add: `HF_API_KEY` = `your_huggingface_api_key_here`
- Endpoint is now hardcoded to your specific Inference Endpoint
4. **Upload these files** to your Space repository
5. **Install requirements**: The Space will auto-install from `requirements.txt`
### πŸ”§ **MCP Server Mode:**
This app functions as both a web interface AND an MCP server for Claude Desktop and other MCP clients.
**Available MCP Tools:**
- `validate_rdf_tool`: Validate RDF/XML against SHACL shapes
- `get_ai_suggestions`: Get AI-powered fix suggestions
- `get_ai_correction`: Generate corrected RDF/XML
- `get_rdf_examples`: Retrieve example RDF snippets
- `validate_rdf_interface`: Complete validation with AI suggestions and corrections (primary tool)
**MCP Configuration (Streamable HTTP):**
Add this configuration to your MCP client (Claude Desktop, etc.):
```json
{
"mcpServers": {
"rdf-validator": {
"url": "https://jimfhahn-mcp4rdf.hf.space/gradio_api/mcp/"
}
}
}
```
**Alternative SSE Configuration:**
```json
{
"mcpServers": {
"rdf-validator": {
"url": "https://jimfhahn-mcp4rdf.hf.space/gradio_api/mcp/sse"
}
}
}
```
### πŸ’‘ **Features:**
- βœ… Real-time RDF/XML validation against SHACL schemas
- πŸ€– AI-powered error suggestions and corrections (with HF Inference Endpoint)
- πŸ“š Built-in examples and templates
- οΏ½ Manual validation on-demand (click to validate)
- πŸ“‹ Copy results with one click
**Note:** AI features require a valid Hugging Face API key (HF_API_KEY) set as a Secret. Manual suggestions are provided as fallback.
""")
return demo
# Launch configuration
if __name__ == "__main__":
demo = create_interface()
# Configuration for different environments
port = int(os.getenv('PORT', 7860)) # Hugging Face uses PORT env variable
demo.launch(
server_name="0.0.0.0", # Important for external hosting
server_port=port, # Use environment PORT or default to 7860
share=False, # Don't create gradio.live links in production
show_error=True, # Show errors in the interface
show_api=True, # Enable API endpoints
allowed_paths=["."], # Allow serving files from current directory
mcp_server=True # Enable MCP server functionality (Gradio 5.28+)
)