import gradio as gr import requests from PIL import Image import io # Your n8n webhook URL WEBHOOK_URL = "https://sanjay192005.app.n8n.cloud/webhook/a3acb431-0656-43cb-91f9-4d09827c4226" def redact_pii(image): """ Takes an uploaded image and sends it to the n8n webhook for PII redaction. Returns the redacted image. """ if image is None: return None, "Please upload an image first." try: # Convert the image to bytes img = Image.fromarray(image) img_byte_arr = io.BytesIO() img.save(img_byte_arr, format='JPEG') img_byte_arr.seek(0) # Prepare the file for upload files = { 'file': ('image.jpg', img_byte_arr, 'image/jpeg') } # Send POST request to n8n webhook response = requests.post(WEBHOOK_URL, files=files, timeout=120) # Check if request was successful if response.status_code == 200: # Convert response bytes to image redacted_image = Image.open(io.BytesIO(response.content)) return redacted_image, "✅ Redaction completed successfully!" else: return None, f"❌ Error: Server returned status code {response.status_code}" except requests.exceptions.Timeout: return None, "❌ Error: Request timed out. Please try again." except requests.exceptions.RequestException as e: return None, f"❌ Error: {str(e)}" except Exception as e: return None, f"❌ Error processing image: {str(e)}" # Create the Gradio interface with gr.Blocks(title="PII Redaction Tool", theme=gr.themes.Soft()) as demo: gr.Markdown( """ # 🔒 PII Redaction Tool Upload a document image containing Personally Identifiable Information (PII). The system will automatically detect and redact: - **Names** - **Dates of Birth** - **ID Numbers** (Aadhaar, etc.) - **Addresses** - **Faces** """ ) with gr.Row(): with gr.Column(): gr.Markdown("### 📤 Upload Document") input_image = gr.Image( label="Upload Image", type="numpy", height=400 ) redact_btn = gr.Button("🔒 Redact PII", variant="primary", size="lg") with gr.Column(): gr.Markdown("### ✅ Redacted Document") output_image = gr.Image( label="Redacted Image", height=400 ) status_text = gr.Textbox( label="Status", interactive=False, lines=2 ) gr.Markdown( """ --- ### ⚠️ Privacy Notice - Your documents are processed securely - No data is stored permanently - Images are transmitted over secure connections ### 📝 Supported Documents - Aadhaar Cards - Passports - Driver's Licenses - Any document with text and faces """ ) # Connect the button to the function redact_btn.click( fn=redact_pii, inputs=input_image, outputs=[output_image, status_text] ) # Also allow pressing Enter to submit input_image.change( fn=lambda: "Image loaded. Click 'Redact PII' to process.", inputs=None, outputs=status_text ) # Launch the app if __name__ == "__main__": demo.launch()