#!/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 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 = 3 # Maximum number of attempts to generate valid RDF ENABLE_VALIDATION_LOOP = True # Set to False to disable validation loop for debugging # 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 = ''' Sample Monograph Title Sample Author Author Example Library ''' SAMPLE_INVALID_RDF = ''' Incomplete Title ''' # 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 get_ai_suggestions(validation_results: str, rdf_content: str) -> str: """ 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 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)} """ prompt = f"""You are an expert in RDF/XML and SHACL validation. Analyze the following validation results and provide clear, actionable suggestions for fixing the RDF issues. Validation Results: {validation_results} Original RDF (first 1000 chars): {rdf_content[:1000]}... Please provide: 1. A clear summary of what's wrong 2. Specific step-by-step instructions to fix each issue 3. Example corrections where applicable 4. Best practices to prevent similar issues Format your response in a helpful, structured way using markdown.""" # Make API call using OpenAI client print(f"🔄 Making API call to: {HF_ENDPOINT_URL}") print(f"🔄 Using model: {HF_MODEL}") print(f"🔄 Client base_url: {client.base_url}") chat_completion = client.chat.completions.create( model=HF_MODEL, messages=[ { "role": "user", "content": prompt } ], max_tokens=1500, temperature=0.7, top_p=0.9 ) print("✅ API call successful") generated_text = chat_completion.choices[0].message.content return f"🤖 **AI-Powered Suggestions:**\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) -> 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) Returns: str: Corrected RDF/XML that should pass validation """ # Use configuration default if not specified if max_attempts is None: max_attempts = MAX_CORRECTION_ATTEMPTS # Check if validation loop is enabled if not ENABLE_VALIDATION_LOOP: max_attempts = 1 # Fall back to single attempt if validation loop disabled if not OPENAI_AVAILABLE: 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: return f""" {generate_manual_correction_hints(validation_results, rdf_content)}""" try: client = get_openai_client() if not client: return f""" {generate_manual_correction_hints(validation_results, rdf_content)}""" # Try multiple attempts to generate valid RDF for attempt in range(max_attempts): prompt = f"""You are an expert in RDF/XML. Fix the following RDF/XML based on the validation errors provided. 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""" print(f"🔄 Correction attempt {attempt + 1}/{max_attempts}") print(f"🔄 Using endpoint: {HF_ENDPOINT_URL}") print(f"🔄 Using model: {HF_MODEL}") chat_completion = client.chat.completions.create( model=HF_MODEL, messages=[ { "role": "user", "content": prompt } ], max_tokens=2000, temperature=0.3 # Lower temperature for more consistent output ) corrected_rdf = chat_completion.choices[0].message.content.strip() # Extract RDF content if it's wrapped in code blocks corrected_rdf = extract_rdf_from_response(corrected_rdf) # Validate the corrected RDF if VALIDATOR_AVAILABLE: try: # Validate the corrected RDF using the same template conforms, new_results = validate_rdf(corrected_rdf.encode('utf-8'), template) if conforms: print(f"✅ Correction validated successfully on attempt {attempt + 1}") return f""" {corrected_rdf}""" else: print(f"❌ Correction attempt {attempt + 1} still has validation errors") # Update validation_results for next attempt validation_results = new_results except Exception as e: print(f"⚠️ Error validating correction attempt {attempt + 1}: {str(e)}") # Continue to next attempt else: # If validator not available, return the first attempt print("⚠️ Validator not available, returning unvalidated correction") return corrected_rdf # All attempts failed return f""" {generate_manual_correction_hints(validation_results, rdf_content)}""" except Exception as e: logger.error(f"LLM API error: {str(e)}") return f""" {generate_manual_correction_hints(validation_results, rdf_content)}""" def generate_manual_suggestions(validation_results: str) -> str: """Generate rule-based suggestions when AI is not available""" suggestions = [] if "Constraint Violation" in validation_results: suggestions.append("• Fix SHACL constraint violations by ensuring required properties are present") if "Missing property" in validation_results or "missing" in validation_results.lower(): suggestions.append("• Add missing required properties (check template requirements)") if "datatype" in validation_results.lower(): suggestions.append("• Correct data type mismatches (ensure proper literal types)") if "namespace" in validation_results.lower() or "prefix" in validation_results.lower(): suggestions.append("• Add missing namespace declarations at the top of your RDF") if "XML" in validation_results or "syntax" in validation_results.lower(): suggestions.append("• Fix XML syntax errors (check for unclosed tags, invalid characters)") if not suggestions: suggestions.append("• Review detailed validation results for specific issues") suggestions.append("• Ensure your RDF follows the selected template requirements") suggestions_text = "\n".join(suggestions) return f""" 📋 **Manual Analysis:** {suggestions_text} 💡 **General Tips:** • Check namespace declarations at the top of your RDF • Ensure all required properties are present • Verify data types match expected formats • Make sure XML structure is well-formed 🔧 **Common Fixes:** • Add missing namespace prefixes • Include required properties like rdf:type • Fix malformed URIs or literals • Ensure proper XML syntax """ def generate_manual_correction_hints(validation_results: str, rdf_content: str) -> str: """Generate manual correction hints when AI is not available""" return f""" {rdf_content} """ def validate_rdf_interface(rdf_content: str, template: str, use_ai: bool = True): """ Main validation function for Gradio interface and MCP server. This function provides comprehensive RDF/XML validation with AI-powered suggestions and corrections. It serves as the primary interface for both the Gradio web UI and MCP client tools. Args: rdf_content (str): The RDF/XML content to validate template (str): Validation template to use ('monograph' or 'custom') use_ai (bool): Whether to enable AI-powered suggestions and corrections Returns: tuple: (status, results_text, suggestions, corrected_rdf) containing: - status: Validation status message - results_text: Detailed validation results - suggestions: AI or manual fix suggestions - corrected_rdf: AI-generated corrections or success message """ if not rdf_content.strip(): return "❌ Error", "No RDF/XML data provided", "", "" # Validate RDF result = validate_rdf_tool(rdf_content, template) if "error" in result: return f"❌ Error: {result['error']}", "", "", "" status = result["status"] results_text = result["results"] if result["conforms"]: suggestions = "✅ No issues found! Your RDF/XML is valid according to the selected template." corrected_rdf = "✅ Your RDF/XML is already valid - no corrections needed!" else: if use_ai: suggestions = get_ai_suggestions(results_text, rdf_content) corrected_rdf = get_ai_correction(results_text, rdf_content, template) else: suggestions = generate_manual_suggestions(results_text) corrected_rdf = generate_manual_correction_hints(results_text, rdf_content) return status, results_text, suggestions, corrected_rdf 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": ''' Example Book Title 2024 New York ''' } 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"""

🔍 RDF Validation Server with AI

Validate RDF/XML against SHACL schemas with AI-powered suggestions and corrections

Status: {api_status}

Debug Info
{debug_info}
""") # 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" ) 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 ) # 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..." ) # Event handlers validate_btn.click( fn=validate_rdf_interface, inputs=[rdf_input, template_dropdown, use_ai_checkbox], outputs=[status_output, results_output, suggestions_output, corrected_output] ) # Auto-validate on input change (debounced) 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, corrected_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 - 🔄 Auto-validation as you type - 📋 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+) )