import gradio as gr from transformers import pipeline from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas # Load a pre-trained image classification model from Hugging Face # Replace with a fine-tuned model if available home_inspector = pipeline("image-classification", model="google/vit-base-patch16-224") # Function to analyze the uploaded image def inspect_home(image): # Analyze the image using the Hugging Face model results = home_inspector(image) # Format the results detected_issues = {result["label"]: round(result["score"], 2) for result in results} # Generate repair suggestions based on detected issues suggestions = [] for issue in detected_issues: suggestion = f"For {issue}, consider inspecting the area closely and consulting a professional if necessary." suggestions.append(suggestion) # Generate a PDF report generate_report(detected_issues, suggestions) # Return results and suggestions return detected_issues, suggestions # Function to generate a PDF report def generate_report(issues, suggestions, filename="home_inspection_report.pdf"): c = canvas.Canvas(filename, pagesize=letter) # Add a title c.setFont("Helvetica-Bold", 16) c.drawString(100, 750, "Home Inspection Report") # Add a subtitle c.setFont("Helvetica", 12) c.drawString(100, 730, "AI-Powered Home Inspector") # Add issues and suggestions y = 700 for issue, suggestion in zip(issues, suggestions): c.setFont("Helvetica-Bold", 12) c.drawString(100, y, f"Issue: {issue} (Confidence: {issues[issue]})") c.setFont("Helvetica", 10) c.drawString(100, y - 20, f"Suggestion: {suggestion}") y -= 40 # Save the PDF c.save() # Custom CSS for a modern blue and silver interface custom_css = """ body { background: linear-gradient(135deg, #1e3c72, #2a5298); color: white; font-family: 'Helvetica', sans-serif; } .gradio-container { background: rgba(255, 255, 255, 0.1); border-radius: 10px; padding: 20px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } .gradio-input, .gradio-output { background: rgba(255, 255, 255, 0.2); border: 1px solid rgba(255, 255, 255, 0.3); border-radius: 5px; padding: 10px; color: white; } .gradio-button { background: #4a90e2; color: white; border: none; border-radius: 5px; padding: 10px 20px; font-size: 16px; cursor: pointer; transition: background 0.3s ease; } .gradio-button:hover { background: #357abd; } """ # Gradio interface with gr.Blocks(css=custom_css) as demo: gr.Markdown("# 🏠 AI-Powered Home Inspector") gr.Markdown("Upload a photo of your home, and the AI will detect issues and provide repair suggestions.") with gr.Row(): with gr.Column(): image_input = gr.Image(label="Upload a Photo", type="pil") inspect_button = gr.Button("Inspect Home", variant="primary") with gr.Column(): issues_output = gr.Label(label="Detected Issues") suggestions_output = gr.Textbox(label="Repair Suggestions", lines=5) # Link the button to the function inspect_button.click( fn=inspect_home, inputs=image_input, outputs=[issues_output, suggestions_output] ) gr.Markdown("### Report Generated") gr.Markdown("A very detailed PDF report has been saved as `home_inspection_report.pdf`.") # Launch the app demo.launch()