Upload app.py
Browse files12344 unmodified lines9101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936import torchimport torch.nn as nn4 unmodified linesimport numpy as npimport matplotlib.pyplot as pltfrom io import BytesIOimport json, urllib.requestfrom torch.nn.functional import interpolateimport warningswarnings.filterwarnings('ignore')DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")print(f"Running on: {DEVICE}")@torch.no_grad()def load_model_and_labels():print("Loading ResNet50...")model = models.resnet50(weights='IMAGENET1K_V2')model.eval()model = model.to(DEVICE)print("Loading ImageNet labels...")url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"labels = json.loads(urllib.request.urlopen(url).read())print(f"Model ready – {len(labels)} classes")return model, labelsmodel, IMAGENET_LABELS = load_model_and_labels()target_layer = model.layer4[-1]gradcam = LayerGradCam(model, target_layer)transform = transforms.Compose([transforms.Resize((224, 224)),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])def predict_and_explain(image):if image is None:return "Please upload an image", Nonetry:img_tensor = transform(image).unsqueeze(0).to(DEVICE)with torch.no_grad():output = model(img_tensor)probabilities = torch.softmax(output, dim=1)top5_prob, top5_idx = torch.topk(probabilities, 5)pred_class = top5_idx[0][0].item()confidence = top5_prob[0][0].item()attributions = gradcam.attribute(img_tensor, target=pred_class)attr_resized = interpolate(attributions, size=(224, 224), mode='bilinear', align_corners=False)attr_np = attr_resized.squeeze().cpu().detach().numpy()attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min() + 1e-8)fig, axes = plt.subplots(1, 3, figsize=(15, 4.5))fig.patch.set_facecolor('#0a0a0a')plt.subplots_adjust(wspace=0.12, left=0.02, right=0.98, top=0.92, bottom=0.08)axes[0].imshow(image)axes[0].set_title("Original", fontsize=13, fontweight='600', color='#e0e0e0', pad=12)axes[0].axis('off')im = axes[1].imshow(attr_np, cmap='viridis')axes[1].set_title("Grad-CAM", fontsize=13, fontweight='600', color='#e0e0e0', pad=12)axes[1].axis('off')cbar = plt.colorbar(im, ax=axes[1], fraction=0.046, pad=0.04)cbar.ax.tick_params(labelsize=9, colors='#a0a0a0')cbar.set_label('Focus', rotation=270, labelpad=15, color='#a0a0a0', fontsize=10)axes[2].imshow(image)axes[2].imshow(attr_np, cmap='viridis', alpha=0.5)axes[2].set_title("AI Focus", fontsize=13, fontweight='600', color='#e0e0e0', pad=12)axes[2].axis('off')buf = BytesIO()plt.savefig(buf, format='png', dpi=100, bbox_inches='tight', facecolor='#0a0a0a')buf.seek(0)result_image = Image.open(buf)plt.close(fig)if confidence > 0.8:badge = "high"badge_text = "High Confidence"badge_icon = "🎯"elif confidence > 0.5:badge = "medium"badge_text = "Medium Confidence"badge_icon = "⚡"else:badge = "low"badge_text = "Low Confidence"badge_icon = "⚠️"top5_html = "<div class='top5-grid'>"icons = ["🥇", "🥈", "🥉", "4️⃣", "5️⃣"]for i, (prob, idx) in enumerate(zip(top5_prob[0], top5_idx[0])):pct = prob.item() * 100top5_html += f"""<div class='top5-row'><span class='rank'>{icons[i]}</span><span class='label'>{IMAGENET_LABELS[idx.item()].title()}</span><div class='bar-wrap'><div class='bar' style='width:{pct}%'></div></div><span class='pct'>{pct:.1f}%</span></div>"""top5_html += "</div>"prediction_text = f"""<div class="result-card"><div class="pred-header"><h2 class="pred-label">{IMAGENET_LABELS[pred_class].title()}</h2><div class="badge badge-{badge}">{badge_icon} {badge_text}</div></div><div class="conf-score">{confidence*100:.1f}%</div><div class="divider"></div>{top5_html}</div>"""return prediction_text, result_imageexcept Exception as e:return f"<div class='error-msg'>⚠️ Error: {str(e)}</div>", Nonecustom_css = """@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');* {box-sizing: border-box;margin: 0;padding: 0;}body, .gradio-container {margin: 0 !important;padding: 0 !important;width: 100vw !important;min-height: 100vh !important;max-width: 100vw !important;background: linear-gradient(135deg, #0a0a0a 0%, #1a1a1a 50%, #0f0f0f 100%) !important;font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif !important;color: #e0e0e0 !important;overflow-x: hidden !important;}.gradio-container { padding: 0 !important; }body::before {content: '';position: fixed;top: 0;left: 0;width: 100%;height: 100%;opacity: 0.03;pointer-events: none;background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 400 400' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' /%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)' /%3E%3C/svg%3E");z-index: 1;}.main-wrapper {padding: 1.5rem;max-width: 1920px;margin: 0 auto;position: relative;z-index: 2;}.hero-header {text-align: center;padding: 2rem 1rem 1.5rem;margin-bottom: 1.5rem;position: relative;}.hero-header h1 {font-size: clamp(2rem, 5vw, 3.5rem);font-weight: 800;background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 50%, #3b82f6 100%);background-size: 200% 200%;-webkit-background-clip: text;-webkit-text-fill-color: transparent;margin: 0 0 0.5rem;letter-spacing: -1px;}.hero-header .subtitle {font-size: clamp(0.95rem, 2vw, 1.2rem);color: #808080;font-weight: 400;margin: 0;}.tab-nav {display: flex;gap: 2rem;justify-content: center;margin-bottom: 2rem;padding: 0.5rem;background: rgba(20, 20, 20, 0.6);border-radius: 16px;width: fit-content;margin-left: auto;margin-right: auto;border: 1px solid rgba(255, 255, 255, 0.1);}.gr-tab {background: transparent !important;border: none !important;border-radius: 12px !important;color: #a0a0a0 !important;font-weight: 600 !important;padding: 0.75rem 2rem !important;transition: all 0.3s ease !important;font-size: 1rem !important;cursor: pointer !important;}.gr-tab:hover {background: rgba(59, 130, 246, 0.1) !important;color: #e0e0e0 !important;}.gr-tab.selected {background: linear-gradient(135deg, #3b82f6, #8b5cf6) !important;color: white !important;box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3) !important;}.top-section {display: grid;grid-template-columns: 400px 1fr;gap: 1.25rem;margin-bottom: 1.25rem;}.glass-card {background: rgba(20, 20, 20, 0.8);border: 1px solid rgba(255, 255, 255, 0.1);border-radius: 24px;padding: 1.5rem;backdrop-filter: blur(20px);box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);transition: all 0.3s ease;}.glass-card:hover {border-color: rgba(59, 130, 246, 0.3);box-shadow: 0 12px 40px rgba(59, 130, 246, 0.2);transform: translateY(-2px);}.upload-panel {background: rgba(20, 20, 20, 0.8);border: 1px solid rgba(255, 255, 255, 0.1);border-radius: 24px;padding: 1.5rem;backdrop-filter: blur(20px);box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);height: fit-content;}.section-label {font-size: 1.1rem;font-weight: 700;background: linear-gradient(135deg, #3b82f6, #8b5cf6);-webkit-background-clip: text;-webkit-text-fill-color: transparent;margin: 0 0 1rem;text-align: center;letter-spacing: 0.5px;}#input-image {border: 2px dashed rgba(59, 130, 246, 0.4) !important;border-radius: 20px !important;background: rgba(10, 10, 10, 0.6) !important;height: 320px !important;transition: all 0.3s ease;position: relative;overflow: hidden;}#input-image::before {display: none;}#input-image:hover {border-color: #3b82f6 !important;background: rgba(20, 20, 30, 0.8) !important;transform: scale(1.02);box-shadow: 0 0 30px rgba(59, 130, 246, 0.2);}.btn-row {display: flex;gap: 0.75rem;margin-top: 1rem;}.gr-button {border-radius: 14px !important;font-weight: 700 !important;height: 50px !important;font-size: 0.95rem !important;transition: all 0.3s ease !important;border: none !important;letter-spacing: 0.5px;text-transform: uppercase;}.gr-button-primary {background: linear-gradient(135deg, #3b82f6, #8b5cf6) !important;color: white !important;box-shadow: 0 4px 20px rgba(59, 130, 246, 0.4) !important;}.gr-button-primary:hover {transform: translateY(-3px) !important;box-shadow: 0 8px 30px rgba(59, 130, 246, 0.6) !important;}.gr-button-secondary {background: rgba(40, 40, 40, 0.8) !important;color: #a0a0a0 !important;border: 1px solid rgba(255, 255, 255, 0.1) !important;}.gr-button-secondary:hover {background: rgba(50, 50, 50, 0.9) !important;color: #e0e0e0 !important;}.results-panel {background: rgba(20, 20, 20, 0.8);border: 1px solid rgba(255, 255, 255, 0.1);border-radius: 24px;padding: 1.75rem;backdrop-filter: blur(20px);box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);}.result-card {padding: 0;}.pred-header {display: flex;align-items: center;justify-content: space-between;flex-wrap: wrap;gap: 1rem;margin-bottom: 0.75rem;}.pred-label {font-size: clamp(1.5rem, 3vw, 2rem);font-weight: 800;color: #ffffff;margin: 0;letter-spacing: -0.5px;}.badge {padding: 0.5rem 1.25rem;border-radius: 50px;font-size: 0.875rem;font-weight: 700;text-transform: uppercase;letter-spacing: 0.5px;box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);}.badge-high {background: linear-gradient(135deg, #10b981, #059669);color: white;}.badge-medium {background: linear-gradient(135deg, #f59e0b, #d97706);color: white;}.badge-low {background: linear-gradient(135deg, #ef4444, #dc2626);color: white;}.conf-score {font-size: clamp(2rem, 5vw, 3rem);font-weight: 900;background: linear-gradient(135deg, #3b82f6, #8b5cf6);-webkit-background-clip: text;-webkit-text-fill-color: transparent;margin-bottom: 1.25rem;letter-spacing: -1px;}.divider {height: 2px;background: linear-gradient(90deg, transparent, rgba(59, 130, 246, 0.3), transparent);margin: 1.5rem 0;}.top5-grid {display: flex;flex-direction: column;gap: 0.875rem;}.top5-row {display: grid;grid-template-columns: 40px 1fr auto 70px;align-items: center;gap: 0.875rem;font-size: 0.95rem;padding: 0.5rem;border-radius: 12px;background: rgba(30, 30, 30, 0.5);transition: all 0.3s ease;}.top5-row:hover {background: rgba(40, 40, 40, 0.7);transform: translateX(5px);}.rank {font-size: 1.5rem;text-align: center;}.label {color: #e0e0e0;font-weight: 600;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}.bar-wrap {background: rgba(40, 40, 40, 0.8);height: 10px;border-radius: 5px;overflow: hidden;min-width: 100px;box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);}.bar {background: linear-gradient(90deg, #3b82f6, #8b5cf6);height: 100%;transition: width 1s ease;border-radius: 5px;box-shadow: 0 0 10px rgba(59, 130, 246, 0.5);}.pct {color: #3b82f6;font-weight: 700;font-size: 0.9rem;text-align: right;}.viz-section {background: rgba(20, 20, 20, 0.8);border: 1px solid rgba(255, 255, 255, 0.1);border-radius: 24px;padding: 1.75rem;backdrop-filter: blur(20px);box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);}#result-image {border-radius: 16px !important;overflow: hidden;width: 100%;height: auto;box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);}.about-content {max-width: 900px;margin: 0 auto;padding: 2rem;background: rgba(20, 20, 20, 0.6);border: 1px solid rgba(255, 255, 255, 0.1);border-radius: 20px;line-height: 1.8;}.placeholder {text-align: center;padding: 4rem 1.5rem;color: #606060;font-size: 1.1rem;line-height: 1.6;}.placeholder strong {color: #3b82f6;}.error-msg {color: #ef4444;background: rgba(239, 68, 68, 0.1);padding: 1.5rem;border-radius: 16px;text-align: center;border: 1px solid rgba(239, 68, 68, 0.3);}.gr-accordion {background: rgba(20, 20, 20, 0.8) !important;border: 1px solid rgba(255, 255, 255, 0.1) !important;border-radius: 20px !important;backdrop-filter: blur(20px);margin-top: 1.5rem;overflow: hidden;}.gr-accordion summary {color: #e0e0e0 !important;font-weight: 700 !important;padding: 1.25rem 1.5rem !important;font-size: 1.1rem !important;cursor: pointer;transition: all 0.3s ease;}.gr-accordion summary:hover {background: rgba(59, 130, 246, 0.1);}.gr-accordion[open] summary {border-bottom: 1px solid rgba(255, 255, 255, 0.1);}.gr-tab {background: rgba(20, 20, 20, 0.6) !important;border: 1px solid rgba(255, 255, 255, 0.1) !important;border-radius: 16px 16px 0 0 !important;color: #a0a0a0 !important;font-weight: 600 !important;transition: all 0.3s ease;}.gr-tab:hover {background: rgba(30, 30, 30, 0.8) !important;color: #e0e0e0 !important;}.gr-tab.selected {background: linear-gradient(135deg, #3b82f6, #8b5cf6) !important;color: white !important;border-bottom: none !important;}.gr-tabitem {background: transparent !important;border: none !important;padding: 0 !important;}footer, .footer { display: none !important; }::-webkit-scrollbar {width: 10px;height: 10px;}::-webkit-scrollbar-track {background: rgba(20, 20, 20, 0.5);}::-webkit-scrollbar-thumb {background: rgba(59, 130, 246, 0.5);border-radius: 5px;}::-webkit-scrollbar-thumb:hover {background: rgba(59, 130, 246, 0.7);}@media (max-width: 1024px) {.top-section {grid-template-columns: 360px 1fr;gap: 1rem;}#input-image {height: 280px !important;}.features-grid {grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));}}@media (max-width: 768px) {.main-wrapper {padding: 1rem;}.hero-header {padding: 1.5rem 0.75rem;}.top-section {grid-template-columns: 1fr;gap: 1rem;}.upload-panel, .results-panel, .viz-section {width: 100%;}#input-image {height: 240px !important;}.top5-row {grid-template-columns: 35px 1fr 60px;gap: 0.625rem;}.bar-wrap {grid-column: 1 / -1;margin-top: 0.375rem;}.btn-row {flex-direction: column;}.gr-button {width: 100% !important;}.about-hero {padding: 2rem 1rem;}.use-case-item {flex-direction: column;gap: 1rem;}}@media (max-width: 480px) {.hero-header h1 {font-size: 1.75rem;}#input-image {height: 220px !important;}.pred-header {flex-direction: column;align-items: flex-start;}.gr-button {height: 46px !important;font-size: 0.875rem !important;}.feature-card {padding: 1.5rem;}.use-cases {padding: 1.5rem;}}"""with gr.Blocks(css=custom_css, theme=gr.themes.Base(), title="Explainable AI Classifier") as demo:gr.HTML("""<link rel="icon" href="https://res.cloudinary.com/ddn0xuwut/image/upload/v1761284764/encryption_hc0fxo.png" type="image/png">""")with gr.Tabs() as tabs:with gr.TabItem("🔮 Classifier", id=0):with gr.Column(elem_classes="main-wrapper"):gr.HTML("""<div class="hero-header"><h1>Explainable AI Classifier</h1><p class="subtitle">See exactly what the AI sees – powered by ResNet50 + Grad-CAM</p></div>""")with gr.Row(elem_classes="top-section"):with gr.Column(scale=0, min_width=400, elem_classes="upload-panel"):gr.HTML("<div class='section-label'>📤 Upload Image</div>")input_image = gr.Image(type="pil",label=None,elem_id="input-image",show_label=False,container=False)with gr.Row(elem_classes="btn-row"):predict_btn = gr.Button("🚀 Analyze", variant="primary", size="lg", scale=2)clear_btn = gr.ClearButton([input_image], value="🗑️ Clear", size="lg", scale=1)with gr.Accordion("💡 Tips for Best Results", open=False):gr.Markdown("""- Use **clear, well-lit photos**- **Center the main subject**- Avoid heavy filters or edits- Works best with **single objects**- Supports JPG, PNG, WebP formats""")with gr.Column(scale=1, elem_classes="results-panel"):output_text = gr.HTML("""<div class='placeholder'><strong>👋 Welcome!</strong><br><br>Upload an image and click <strong>Analyze</strong> to see:<br>✓ AI predictions with confidence scores<br>✓ Visual explanations (what the AI focuses on)<br>✓ Top 5 possible classifications</div>""")with gr.Column(elem_classes="viz-section"):gr.HTML("<div class='section-label'>🎯 Visual Explainability</div>")output_image = gr.Image(label=None,type="pil",show_label=False,elem_id="result-image",height=450)with gr.Accordion("📚 How It Works", open=False):gr.Markdown("""### The Technology Behind This Tool**1. ResNet50 Neural Network**A deep learning model trained on 1,000+ categories from ImageNet dataset.Achieves 76%+ accuracy on real-world images.**2. Grad-CAM (Gradient-weighted Class Activation Mapping)**Developed by researchers at Georgia Tech, Grad-CAM highlights which regionsof the image were most important for the AI's decision.**3. Real-Time Inference**Your image is processed locally in this interface – we never store your uploads.### Why This Matters- **Transparency**: See *why* the AI made its decision- **Trust**: Verify the AI is focusing on relevant features- **Debugging**: Understand when and why predictions fail- **Compliance**: Meet explainability requirements (EU AI Act, GDPR)""")with gr.TabItem("ℹ️ About", id=1):with gr.Column(elem_classes="main-wrapper about-page"):gr.HTML("""<div class="about-hero"><h1>About Explainable AI</h1><p>Making artificial intelligence transparent, interpretable, and trustworthy for everyone</p></div>""")gr.HTML("""<div class="features-grid"><div class="feature-card"><span class="feature-icon">🔍</span><h3>Transparency</h3><p>No more "black box" decisions. See exactly which parts of your image influenced the AI's prediction with visual heatmaps.</p></div><div class="feature-card"><span class="feature-icon">🛡️</span><h3>Trust & Safety</h3><p>Verify that AI models are making decisions based on relevant features, not spurious correlations or biases.</p></div><div class="feature-card"><span class="feature-icon">⚖️</span><h3>Regulatory Compliance</h3><p>Meet requirements from EU AI Act, GDPR, FDA guidelines for medical AI, and other global regulations.</p></div><div class="feature-card"><span class="feature-icon">🎓</span><h3>Educational</h3><p>Learn how deep learning works by visualizing what neural networks "see" when processing images.</p></div><div class="feature-card"><span class="feature-icon">🚀</span><h3>Production-Ready</h3><p>Built with PyTorch and Captum (Meta's interpretability library), this tool uses industry-standard techniques.</p></div><div class="feature-card"><span class="feature-icon">🌐</span><h3>Open Source Spirit</h3><p>Based on open research and publicly available models. Knowledge should be accessible to all.</p></div></div><div class="use-cases"><h2>Real-World Applications</h2><div class="use-case-item"><span class="use-case-icon">🏥</span><div class="use-case-content"><h3>Healthcare & Medical Imaging</h3><p><strong>Problem:</strong> Radiologists need to trust AI diagnostic tools.<br><strong>Solution:</strong> Grad-CAM shows if the AI is focusing on tumors, lesions, or irrelevant artifacts. This builds trust and helps doctors make informed decisions.</p></div></div><div class="use-case-item"><span class="use-case-icon">🚗</span><div class="use-case-content"><h3>Autonomous Vehicles</h3><p><strong>Problem:</strong> Self-driving cars must explain their perception to passengers and regulators.<br><strong>Solution:</strong> Visual explanations confirm the AI detected pedestrians, traffic signs, and road boundaries – not random patterns.</p></div></div><div class="use-case-item"><span class="use-case-icon">💰</span><div class="use-case-content"><h3>Financial Services</h3><p><strong>Problem:</strong> Banks must explain loan approval/denial decisions (Fair Lending Act).<br><strong>Solution:</strong> Explainable AI shows which factors (income, credit score, employment) influenced the decision, ensuring fairness.</p></div></div><div class="use-case-item"><span class="use-case-icon">🔒</span><div class="use-case-content"><h3>Security & Surveillance</h3><p><strong>Problem:</strong> Facial recognition and threat detection systems face scrutiny over bias.<br><strong>Solution:</strong> Grad-CAM reveals if the system is using facial features appropriately or making decisions based on background elements.</p></div></div><div class="use-case-item"><span class="use-case-icon">🌾</span><div class="use-case-content"><h3>Agriculture & Environment</h3><p><strong>Problem:</strong> Farmers need to verify AI crop disease detection accuracy.<br><strong>Solution:</strong> Visual explanations show if the AI identified actual disease symptoms (leaf discoloration, spots) or environmental factors.</p></div></div><div class="use-case-item"><span class="use-case-icon">🛒</span><div class="use-case-content"><h3>E-Commerce & Retail</h3><p><strong>Problem:</strong> Product recognition systems must be reliable for inventory and checkout.<br><strong>Solution:</strong> Explainability helps debug failures – did the AI misidentify a product because of lighting, angle, or packaging?</p></div></div></div>""")gr.Markdown("""## Why Explainability is the FutureAs AI becomes more integrated into critical decision-making processes, **explainability is no longer optional**:- **EU AI Act (2024)**: High-risk AI systems must provide explanations- **GDPR Right to Explanation**: Users can demand explanations for automated decisions- **FDA Guidelines**: Medical AI must demonstrate interpretability- **Ethical AI Standards**: Leading tech companies committed to transparency---## Technical StackThis tool is built with:- **PyTorch**: Deep learning framework- **torchvision**: Pre-trained ResNet50 model- **Captum**: Meta's interpretability library- **Gradio**: Interactive web interface- **Grad-CAM**: Visual explanation technique---""")predict_btn.click(fn=predict_and_explain,inputs=[input_image],outputs=[output_text, output_image])if __name__ == "__main__":demo.launch(share=False, show_error=True)12345674 unmodified lines12131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293"""Explainable AI Image Classifier - Optimized Production Version==============================================================="""import torchimport torch.nn as nn4 unmodified linesimport numpy as npimport matplotlib.pyplot as pltfrom io import BytesIOimport urllib.requestfrom torch.nn.functional import interpolateimport warningswarnings.filterwarnings('ignore')# ============================================================================# CONFIGURATION & MODEL LOADING# ============================================================================DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")@torch.no_grad()def load_model_and_labels():"""Load ResNet50 with optimized settings"""model = models.resnet50(weights='IMAGENET1K_V2')model.eval()model = model.to(DEVICE)# Load cleaner ImageNet labelsurl = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"response = urllib.request.urlopen(url)labels = [line.decode('utf-8').strip() for line in response.readlines()]return model, labelsmodel, IMAGENET_LABELS = load_model_and_labels()# Setup Grad-CAMtarget_layer = model.layer4[-1]gradcam = LayerGradCam(model, target_layer)# Optimized preprocessingtransform = transforms.Compose([transforms.Resize((224, 224)),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])# ============================================================================# OPTIMIZED PREDICTION FUNCTION# ============================================================================def predict_and_explain(image):if image is None:return "Please upload an image", None, Nonetry:# Preprocessimg_tensor = transform(image).unsqueeze(0).to(DEVICE)# Predict with temperature scaling for better confidencewith torch.no_grad():output = model(img_tensor)# Apply temperature scaling (makes confidence more realistic)temperature = 1.0 # Adjust between 0.8-1.5 if neededscaled_output = output / temperatureprobabilities = torch.softmax(scaled_output, dim=1)top10_prob, top10_idx = torch.topk(probabilities, 10)pred_class = top10_idx[0][0].item()confidence = top10_prob[0][0].item()# Generate Grad-CAMattributions = gradcam.attribute(img_tensor, target=pred_class)attr_resized = interpolate(attributions, size=(224, 224), mode='bilinear', align_corners=False)attr_np = attr_resized.squeeze().cpu().detach().numpy()attr_np = (attr_np - attr_np.min()) / (attr_np.max() - attr_np.min() + 1e-8)# Create enhanced visualization (LARGER SIZE)fig = plt.figure(figsize=(20, 12)) # Increased from 18x10 to 20x12fig.patch.set_facecolor('#0a0a0a')gs = fig.add_gridspec(2, 3, height_ratios=[2, 1], hspace=0.25, wspace=0.12) # More space for images# Top row: Larger image panelsax1 = fig.add_subplot(gs[0, 0])ax2 = fig.add_subplot(gs[0, 1])ax3 = fig.add_subplot(gs[0, 2])ax4 = fig.add_subplot(gs[1, :])# Panel 1: Originalax1.imshow(image)ax1.set_title("Original Image", fontsize=15, fontweight='600', color='#e0e0e0', pad=15)ax1.axis('off')# Panel 2: Detailed Heatmapim = ax2.imshow(attr_np, cmap='jet', interpolation='bilinear')ax2.set_title("Grad-CAM Heatmap", fontsize=15, fontweight='600', color='#e0e0e0', pad=15)ax2.axis('off')cbar = plt.colorbar(im, ax=ax2, fraction=0.046, pad=0.04)cbar.ax.tick_params(labelsize=10, colors='#a0a0a0')cbar.set_label('Importance', rotation=270, labelpad=20, color='#e0e0e0', fontsize=11, fontweight='600')# Panel 3: Overlayax3.imshow(image)ax3.imshow(attr_np, cmap='jet', alpha=0.5, interpolation='bilinear')ax3.set_title(f"AI Focus: {IMAGENET_LABELS[pred_class]}", fontsize=15, fontweight='600', color='#e0e0e0', pad=15)ax3.axis('off')# Panel 4: Bar Graphtop10_labels = [IMAGENET_LABELS[idx.item()] for idx in top10_idx[0]]top10_probs = [prob.item() * 100 for prob in top10_prob[0]]colors = ['#10b981' if i == 9 else '#3b82f6' if i >= 7 else '#8b5cf6' for i in range(10)]bars = ax4.barh(range(10), top10_probs[::-1], color=colors[::-1], edgecolor='#1a1a1a', linewidth=1.5)ax4.set_yticks(range(10))ax4.set_yticklabels(top10_labels[::-1], fontsize=12, color='#e0e0e0')ax4.set_xlabel('Confidence (%)', fontsize=13, color='#e0e0e0', fontweight='600')ax4.set_title('Top 10 Predictions', fontsize=16, fontweight='700', color='#e0e0e0', pad=15)ax4.set_xlim([0, 100])ax4.grid(axis='x', alpha=0.2, color='#404040', linestyle='--')ax4.set_facecolor('#0a0a0a')ax4.spines['top'].set_visible(False)ax4.spines['right'].set_visible(False)ax4.spines['left'].set_color('#404040')ax4.spines['bottom'].set_color('#404040')ax4.tick_params(colors='#a0a0a0', labelsize=11)# Add percentage labelsfor bar, prob in zip(bars, top10_probs[::-1]):ax4.text(prob + 1.5, bar.get_y() + bar.get_height()/2,f'{prob:.1f}%', va='center', fontsize=11, color='#e0e0e0', fontweight='600')plt.tight_layout()buf = BytesIO()plt.savefig(buf, format='png', dpi=120, bbox_inches='tight', facecolor='#0a0a0a')buf.seek(0)result_image = Image.open(buf)plt.close(fig)# Create detailed heatmap panelfig2, axes = plt.subplots(1, 3, figsize=(18, 6)) # Increased from 15x5 to 18x6fig2.patch.set_facecolor('#0a0a0a')axes[0].imshow(image)axes[0].set_title("Original", fontsize=14, fontweight='600', color='#e0e0e0', pad=12)axes[0].axis('off')im2 = axes[1].imshow(attr_np, cmap='viridis', interpolation='gaussian')axes[1].set_title("High-Res Heatmap", fontsize=14, fontweight='600', color='#e0e0e0', pad=12)axes[1].axis('off')cbar2 = plt.colorbar(im2, ax=axes[1], fraction=0.046, pad=0.04)cbar2.ax.tick_params(labelsize=10, colors='#a0a0a0')axes[2].imshow(image)axes[2].imshow(attr_np, cmap='hot', alpha=0.6, interpolation='bilinear')axes[2].contour(attr_np, levels=5, colors='white', linewidths=1.5, alpha=0.8)axes[2].set_title("Contour Analysis", fontsize=14, fontweight='600', color='#e0e0e0', pad=12)axes[2].axis('off')plt.tight_layout()buf2 = BytesIO()plt.savefig(buf2, format='png', dpi=110, bbox_inches='tight', facecolor='#0a0a0a')buf2.seek(0)detailed_heatmap = Image.open(buf2)plt.close(fig2)# Generate HTML outputbadge = "high" if confidence > 0.8 else "medium" if confidence > 0.5 else "low"badge_text = "High Confidence" if confidence > 0.8 else "Medium Confidence" if confidence > 0.5 else "Low Confidence"badge_icon = "🎯" if confidence > 0.8 else "⚡" if confidence > 0.5 else "⚠️"top5_html = "<div class='top5-grid'>"icons = ["🥇", "🥈", "🥉", "4️⃣", "5️⃣"]for i, (prob, idx) in enumerate(zip(top10_prob[0][:5], top10_idx[0][:5])):pct = prob.item() * 100top5_html += f"""<div class='top5-row'><span class='rank'>{icons[i]}</span><span class='label'>{IMAGENET_LABELS[idx.item()]}</span><div class='bar-wrap'><div class='bar' style='width:{pct}%'></div></div><span class='pct'>{pct:.2f}%</span></div>"""top5_html += "</div>"prediction_text = f"""<div class="result-card"><div class="pred-header"><h2 class="pred-label">{IMAGENET_LABELS[pred_class]}</h2><div class="badge badge-{badge}">{badge_icon} {badge_text}</div></div><div class="conf-score">{confidence*100:.2f}%</div><div class="divider"></div>{top5_html}</div>"""return prediction_text, result_image, detailed_heatmapexcept Exception as e:return f"<div class='error-msg'>⚠️ Error: {str(e)}</div>", None, None# ============================================================================# STREAMLINED CSS# ============================================================================custom_css = """@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');* { box-sizing: border-box; margin: 0; padding: 0; }body, .gradio-container { margin: 0 !important; padding: 0 !important; width: 100vw !important; min-height: 100vh !important; max-width: 100vw !important; background: linear-gradient(135deg, #0a0a0a 0%, #1a1a1a 50%, #0f0f0f 100%) !important; font-family: 'Inter', sans-serif !important; color: #e0e0e0 !important; overflow-x: hidden !important; }.gradio-container { padding: 0 !important; }.main-wrapper { padding: 1.5rem; max-width: 1920px; margin: 0 auto; position: relative; z-index: 2; }.hero-header { text-align: center; padding: 2rem 1rem 1.5rem; margin-bottom: 1.5rem; }.hero-header h1 { font-size: clamp(2rem, 5vw, 3.5rem); font-weight: 800; background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 50%, #3b82f6 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin: 0 0 0.5rem; letter-spacing: -1px; }.hero-header .subtitle { font-size: clamp(0.95rem, 2vw, 1.2rem); color: #808080; font-weight: 400; margin: 0; }.top-section { display: grid; grid-template-columns: 400px 1fr; gap: 1.25rem; margin-bottom: 1.25rem; }.upload-panel, .results-panel, .viz-section { background: rgba(20, 20, 20, 0.8); border: 1px solid rgba(255, 255, 255, 0.1); border-radius: 24px; padding: 1.5rem; backdrop-filter: blur(20px); box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4); }.section-label { font-size: 1.1rem; font-weight: 700; background: linear-gradient(135deg, #3b82f6, #8b5cf6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin: 0 0 1rem; text-align: center; letter-spacing: 0.5px; }#input-image { border: 2px dashed rgba(59, 130, 246, 0.4) !important; border-radius: 20px !important; background: rgba(10, 10, 10, 0.6) !important; height: 320px !important; transition: all 0.3s ease; }#input-image:hover { border-color: #3b82f6 !important; background: rgba(20, 20, 30, 0.8) !important; transform: scale(1.02); box-shadow: 0 0 30px rgba(59, 130, 246, 0.2); }.btn-row { display: flex; gap: 0.75rem; margin-top: 1rem; }.gr-button { border-radius: 14px !important; font-weight: 700 !important; height: 50px !important; font-size: 0.95rem !important; transition: all 0.3s ease !important; border: none !important; letter-spacing: 0.5px; text-transform: uppercase; }.gr-button-primary { background: linear-gradient(135deg, #3b82f6, #8b5cf6) !important; color: white !important; box-shadow: 0 4px 20px rgba(59, 130, 246, 0.4) !important; }.gr-button-primary:hover { transform: translateY(-3px) !important; box-shadow: 0 8px 30px rgba(59, 130, 246, 0.6) !important; }.gr-button-secondary { background: rgba(40, 40, 40, 0.8) !important; color: #a0a0a0 !important; border: 1px solid rgba(255, 255, 255, 0.1) !important; }.pred-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 1rem; margin-bottom: 0.75rem; }.pred-label { font-size: clamp(1.5rem, 3vw, 2rem); font-weight: 800; color: #ffffff; margin: 0; letter-spacing: -0.5px; }.badge { padding: 0.5rem 1.25rem; border-radius: 50px; font-size: 0.875rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3); }.badge-high { background: linear-gradient(135deg, #10b981, #059669); color: white; }.badge-medium { background: linear-gradient(135deg, #f59e0b, #d97706); color: white; }.badge-low { background: linear-gradient(135deg, #ef4444, #dc2626); color: white; }.conf-score { font-size: clamp(2rem, 5vw, 3rem); font-weight: 900; background: linear-gradient(135deg, #3b82f6, #8b5cf6); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin-bottom: 1.25rem; letter-spacing: -1px; }.divider { height: 2px; background: linear-gradient(90deg, transparent, rgba(59, 130, 246, 0.3), transparent); margin: 1.5rem 0; }.top5-grid { display: flex; flex-direction: column; gap: 0.875rem; }.top5-row { display: grid; grid-template-columns: 40px 1fr auto 80px; align-items: center; gap: 0.875rem; font-size: 0.95rem; padding: 0.5rem; border-radius: 12px; background: rgba(30, 30, 30, 0.5); transition: all 0.3s ease; }.top5-row:hover { background: rgba(40, 40, 40, 0.7); transform: translateX(5px); }.rank { font-size: 1.5rem; text-align: center; }.label { color: #e0e0e0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }.bar-wrap { background: rgba(40, 40, 40, 0.8); height: 10px; border-radius: 5px; overflow: hidden; min-width: 100px; box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3); }.bar { background: linear-gradient(90deg, #3b82f6, #8b5cf6); height: 100%; transition: width 1s ease; border-radius: 5px; box-shadow: 0 0 10px rgba(59, 130, 246, 0.5); }.pct { color: #3b82f6; font-weight: 700; font-size: 0.9rem; text-align: right; }#result-image, #detailed-heatmap { border-radius: 16px !important; overflow: hidden; width: 100%; height: auto; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5); }.placeholder { text-align: center; padding: 4rem 1.5rem; color: #606060; font-size: 1.1rem; line-height: 1.6; }.placeholder strong { color: #3b82f6; }.error-msg { color: #ef4444; background: rgba(239, 68, 68, 0.1); padding: 1.5rem; border-radius: 16px; text-align: center; border: 1px solid rgba(239, 68, 68, 0.3); }.gr-accordion { background: rgba(20, 20, 20, 0.8) !important; border: 1px solid rgba(255, 255, 255, 0.1) !important; border-radius: 20px !important; margin-top: 1.5rem; }.gr-accordion summary { color: #e0e0e0 !important; font-weight: 700 !important; padding: 1.25rem 1.5rem !important; font-size: 1.1rem !important; }footer, .footer { display: none !important; }::-webkit-scrollbar { width: 10px; }::-webkit-scrollbar-track { background: rgba(20, 20, 20, 0.5); }::-webkit-scrollbar-thumb { background: rgba(59, 130, 246, 0.5); border-radius: 5px; }@media (max-width: 768px) { .top-section { grid-template-columns: 1fr; } #input-image { height: 240px !important; } .top5-row { grid-template-columns: 35px 1fr 70px; } .bar-wrap { grid-column: 1 / -1; margin-top: 0.375rem; } }"""# ============================================================================# STREAMLINED INTERFACE# ============================================================================with gr.Blocks(css=custom_css, theme=gr.themes.Base(), title="Explainable AI") as demo:gr.HTML('<link rel="icon" href="https://res.cloudinary.com/ddn0xuwut/image/upload/v1761284764/encryption_hc0fxo.png" type="image/png">')with gr.Column(elem_classes="main-wrapper"):gr.HTML('<div class="hero-header"><h1>Explainable AI Classifier</h1><p class="subtitle">See exactly what the AI sees – powered by ResNet50 + Grad-CAM</p></div>')with gr.Row(elem_classes="top-section"):with gr.Column(scale=0, min_width=400, elem_classes="upload-panel"):gr.HTML("<div class='section-label'>📤 Upload Image</div>")input_image = gr.Image(type="pil", label=None, elem_id="input-image", show_label=False, container=False)with gr.Row(elem_classes="btn-row"):predict_btn = gr.Button("🚀 Analyze", variant="primary", size="lg", scale=2)clear_btn = gr.ClearButton([input_image], value="🗑️ Clear", size="lg", scale=1)with gr.Column(scale=1, elem_classes="results-panel"):output_text = gr.HTML('<div class="placeholder"><strong>👋 Welcome!</strong><br><br>Upload an image and click <strong>Analyze</strong></div>')with gr.Column(elem_classes="viz-section"):gr.HTML("<div class='section-label'>🎯 Visual Explainability (Includes Bar Graph)</div>")output_image = gr.Image(label=None, type="pil", show_label=False, elem_id="result-image", height=700)with gr.Column(elem_classes="viz-section"):gr.HTML("<div class='section-label'>🔬 Advanced Heatmap Analysis</div>")detailed_heatmap = gr.Image(label=None, type="pil", show_label=False, elem_id="detailed-heatmap", height=500)predict_btn.click(fn=predict_and_explain, inputs=[input_image], outputs=[output_text, output_image, detailed_heatmap])if __name__ == "__main__":demo.launch(share=False, show_error=True)
app.py+196-839