Spaces:
Sleeping
Sleeping
Download app.py from AbdulBasirAI/README: direct link, hf CLI and curl.
- Browser
- Download file 13.1 kB
-
https://huggingface.co/spaces/AbdulBasirAI/README/resolve/main/app.py
- Command line
-
hf download hf://spaces/AbdulBasirAI/README/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AbdulBasirAI/README/resolve/main/app.py
13.1 kB
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| import cv2 | |
| from datetime import datetime | |
| # Load model | |
| model = tf.keras.models.load_model("brain_tumor_detector.keras") | |
| # ============================================================ | |
| # COMPLETE TRI-LINGUAL TRANSLATIONS | |
| # ============================================================ | |
| T = { | |
| "en": { | |
| "title": "🧠 NeuroScan AI", | |
| "subtitle": "AI-Powered Brain Tumor Detection", | |
| "badge": "🏥 REAL AI MODEL", | |
| "accuracy": "90% Accuracy | 2,870 MRI Scans | Free & Secure", | |
| "disclaimer": "⚕️ This is an AI-assisted screening tool. Not a final medical diagnosis. Always consult a doctor.", | |
| "upload": "📤 Upload Brain MRI Scan", | |
| "analyze": "🔍 ANALYZE MRI SCAN", | |
| "analyzing": "⏳ Analyzing...", | |
| "tumor": "🔴 TUMOR DETECTED", | |
| "no_tumor": "🟢 NO TUMOR DETECTED", | |
| "confidence": "Confidence Level", | |
| "recommend": "⚠️ Please consult a neurologist immediately.", | |
| "normal": "✅ Brain scan appears normal.", | |
| "time": "Analysis Time", | |
| "specs": "🔬 Technical Specifications", | |
| "about": "👨💻 About Developer", | |
| "contact": "📞 Contact", | |
| "legal": "⚖️ Legal Rights", | |
| "language": "🌐 Language", | |
| "specs_text": """ | |
| | **Specification** | **Detail** | | |
| |---|---| | |
| | AI Model | CNN Deep Learning | | |
| | Training Data | 2,870 Real Brain MRI Scans | | |
| | Validation Accuracy | 98.3% | | |
| | Test Accuracy | 90% | | |
| | Processing Time | < 3 Seconds | | |
| | Data Privacy | No Image Storage | | |
| """, | |
| "about_text": """ | |
| ### 👨💻 Developer | |
| ## Abdul Basir Akbar | |
| **Biomedical Engineering Student** | |
| Ghazanfar Institute of Health Sciences | |
| Kabul, Afghanistan | |
| 📅 **Project Year:** 2025 | |
| 📌 **Version:** 2.0 | |
| """, | |
| "contact_text": """ | |
| ### 📞 Get In Touch | |
| - 📧 **Email:** abdulbasir.akbar123@gmail.com | |
| - 📱 **WhatsApp:** +93770068586 | |
| - 🔗 **LinkedIn:** [abdul-basir-akbar](https://www.linkedin.com/in/abdul-basir-akbar-74a255381) | |
| - 📘 **Facebook:** [Abdul Basir Akbar](https://www.facebook.com/share/18SHP5LofX/) | |
| """, | |
| "legal_text": """ | |
| ### © 2025 Abdul Basir Akbar. ALL RIGHTS RESERVED. | |
| **Institution:** Department of Biomedical Engineering | |
| Ghazanfar Institute of Health Sciences, Kabul, Afghanistan | |
| **🚫 PROHIBITED:** Unauthorized reproduction, commercial use, reverse engineering. | |
| **✅ PERMITTED:** Academic research with citation, non-commercial screening. | |
| """ | |
| }, | |
| "ps": { | |
| "title": "🧠 نیوروسکن AI", | |
| "subtitle": "د مغز تومور تشخیص د AI په مرسته", | |
| "badge": "🏥 ریښتینی AI ماډل", | |
| "accuracy": "۹۰٪ دقت | ۲,۸۷۰ MRI سکینونه | وړیا او خوندي", | |
| "disclaimer": "⚕️ دا د AI معایناتي وسیله ده. وروستی طبي تشخیص نه دی. تل له ډاکټر سره مشوره وکړئ.", | |
| "upload": "📤 د مغز MRI سکین اپلوډ کړئ", | |
| "analyze": "🔍 MRI سکین معاینه کړئ", | |
| "analyzing": "⏳ معاینه کېږي...", | |
| "tumor": "🔴 تومور وموندل شو", | |
| "no_tumor": "🟢 تومور ونه موندل شو", | |
| "confidence": "د باور کچه", | |
| "recommend": "⚠️ مهرباني وکړئ د عصبي ډاکټر سره مشوره وکړئ.", | |
| "normal": "✅ د مغز سکین نورمال ښکاري.", | |
| "time": "د تحلیل وخت", | |
| "specs": "🔬 تخنیکي مشخصات", | |
| "about": "👨💻 د جوړونکي په اړه", | |
| "contact": "📞 اړیکه", | |
| "legal": "⚖️ قانوني حقوق", | |
| "language": "🌐 ژبه", | |
| "specs_text": """ | |
| | **مشخصات** | **جزئیات** | | |
| |---|---| | |
| | AI ماډل | CNN ژورې زده کړې | | |
| | روزنیز معلومات | ۲,۸۷۰ ریښتیني MRI سکینونه | | |
| | تایید دقت | ۹۸.۳٪ | | |
| | ازموینې دقت | ۹۰٪ | | |
| | پروسس وخت | < ۳ ثانیې | | |
| | معلوماتو محرمیت | هیڅ انځور نه ساتل کېږي | | |
| """, | |
| "about_text": """ | |
| ### 👨💻 جوړونکی | |
| ## عبدالبصیر اکبر | |
| **د بایومیډیکل انجینرۍ زده کړیال** | |
| د غضنفر روغتیا علومو انسټیټیوټ | |
| کابل، افغانستان | |
| 📅 **پروژه کال:** ۲۰۲۵ | |
| 📌 **نسخه:** ۲.۰ | |
| """, | |
| "contact_text": """ | |
| ### 📞 اړیکه ونیسئ | |
| - 📧 **ایمیل:** abdulbasir.akbar123@gmail.com | |
| - 📱 **واټساپ:** +93770068586 | |
| - 🔗 **لینکډاین:** [abdul-basir-akbar](https://www.linkedin.com/in/abdul-basir-akbar-74a255381) | |
| - 📘 **فیسبوک:** [Abdul Basir Akbar](https://www.facebook.com/share/18SHP5LofX/) | |
| """, | |
| "legal_text": """ | |
| ### © ۲۰۲۵ عبدالبصیر اکبر. ټول حقوق خوندي دي. | |
| **انسټیټیوټ:** د بایومیډیکل انجینرۍ څانګه | |
| د غضنفر روغتیا علومو انسټیټیوټ، کابل، افغانستان | |
| **🚫 منع:** غیر مجاز تکثیر، سوداګریز استعمال، ریورس انجینرۍ. | |
| **✅ اجازه:** د حوالې سره اکاډمیک څیړنه، غیر تجارتي معاینه. | |
| """ | |
| }, | |
| "fa": { | |
| "title": "🧠 نورواسکن AI", | |
| "subtitle": "تشخیص تومور مغزی با هوش مصنوعی", | |
| "badge": "🏥 مدل واقعی هوش مصنوعی", | |
| "accuracy": "۹۰٪ دقت | ۲,۸۷۰ اسکن MRI | رایگان و امن", | |
| "disclaimer": "⚕️ این یک ابزار غربالگری کمکی است. تشخیص نهایی پزشکی نیست. همیشه با پزشک مشورت کنید.", | |
| "upload": "📤 اسکن MRI مغز را آپلود کنید", | |
| "analyze": "🔍 تحلیل اسکن MRI", | |
| "analyzing": "⏳ در حال تحلیل...", | |
| "tumor": "🔴 تومور تشخیص داده شد", | |
| "no_tumor": "🟢 تومور تشخیص داده نشد", | |
| "confidence": "میزان اطمینان", | |
| "recommend": "⚠️ لطفاً فوراً با متخصص مغز و اعصاب مشورت کنید.", | |
| "normal": "✅ اسکن مغز طبیعی به نظر میرسد.", | |
| "time": "زمان تحلیل", | |
| "specs": "🔬 مشخصات فنی", | |
| "about": "👨💻 درباره توسعهدهنده", | |
| "contact": "📞 تماس", | |
| "legal": "⚖️ حقوق قانونی", | |
| "language": "🌐 زبان", | |
| "specs_text": """ | |
| | **مشخصات** | **جزئیات** | | |
| |---|---| | |
| | مدل هوش مصنوعی | CNN یادگیری عمیق | | |
| | دادههای آموزشی | ۲,۸۷۰ اسکن واقعی MRI | | |
| | دقت اعتبارسنجی | ۹۸.۳٪ | | |
| | دقت آزمایش | ۹۰٪ | | |
| | زمان پردازش | کمتر از ۳ ثانیه | | |
| | حریم خصوصی | عدم ذخیره تصاویر | | |
| """, | |
| "about_text": """ | |
| ### 👨💻 توسعهدهنده | |
| ## عبدالبصیر اکبر | |
| **دانشجوی مهندسی پزشکی** | |
| انستیتیوت علوم صحی غضنفر | |
| کابل، افغانستان | |
| 📅 **سال پروژه:** ۲۰۲۵ | |
| 📌 **نسخه:** ۲.۰ | |
| """, | |
| "contact_text": """ | |
| ### 📞 تماس بگیرید | |
| - 📧 **ایمیل:** abdulbasir.akbar123@gmail.com | |
| - 📱 **واتساپ:** +93770068586 | |
| - 🔗 **لینکداین:** [abdul-basir-akbar](https://www.linkedin.com/in/abdul-basir-akbar-74a255381) | |
| - 📘 **فیسبوک:** [Abdul Basir Akbar](https://www.facebook.com/share/18SHP5LofX/) | |
| """, | |
| "legal_text": """ | |
| ### © ۲۰۲۵ عبدالبصیر اکبر. تمام حقوق محفوظ است. | |
| **انستیتیوت:** بخش مهندسی پزشکی | |
| انستیتیوت علوم صحی غضنفر، کابل، افغانستان | |
| **🚫 ممنوع:** تکثیر غیرمجاز، استفاده تجاری، مهندسی معکوس. | |
| **✅ مجاز:** تحقیقات دانشگاهی با ذکر منبع، غربالگری غیرتجاری. | |
| """ | |
| } | |
| } | |
| # ============================================================ | |
| # PREDICTION FUNCTION | |
| # ============================================================ | |
| def predict_tumor(image, lang): | |
| if image is None: | |
| return "⚠️ Please upload an MRI scan." | |
| try: | |
| img_array = np.array(image) | |
| if len(img_array.shape) == 3: | |
| img_gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY) | |
| else: | |
| img_gray = img_array | |
| img_resized = cv2.resize(img_gray, (128, 128)) / 255.0 | |
| img_input = img_resized.reshape(1, 128, 128, 1) | |
| prediction = model.predict(img_input, verbose=0)[0][0] | |
| tx = T.get(lang, T["en"]) | |
| now = datetime.now().strftime("%d %B %Y, %I:%M %p") | |
| if prediction > 0.5: | |
| return ( | |
| f"{tx['tumor']}\n" | |
| f"{'─'*35}\n" | |
| f"{tx['confidence']}: {prediction*100:.1f}%\n" | |
| f"{tx['time']}: {now}\n" | |
| f"{'─'*35}\n" | |
| f"{tx['recommend']}\n\n" | |
| f"_{tx['disclaimer']}_" | |
| ) | |
| else: | |
| return ( | |
| f"{tx['no_tumor']}\n" | |
| f"{'─'*35}\n" | |
| f"{tx['confidence']}: {(1-prediction)*100:.1f}%\n" | |
| f"{tx['time']}: {now}\n" | |
| f"{'─'*35}\n" | |
| f"{tx['normal']}\n\n" | |
| f"_{tx['disclaimer']}_" | |
| ) | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| # ============================================================ | |
| # BUILD MODERN TRI-LINGUAL INTERFACE | |
| # ============================================================ | |
| with gr.Blocks( | |
| theme=gr.themes.Soft(primary_hue="blue"), | |
| title="NeuroScan AI", | |
| css=""" | |
| footer { visibility: hidden; } | |
| .dev-badge { | |
| background: linear-gradient(135deg, #1a73e8, #0d47a1); | |
| color: white; padding: 10px 20px; border-radius: 25px; | |
| text-align: center; margin: 15px 0; font-size: 14px; | |
| } | |
| """ | |
| ) as app: | |
| # State | |
| current_lang = gr.State("en") | |
| # Header | |
| gr.Markdown(""" | |
| <div style="text-align:center; padding:20px;"> | |
| <h1 style="font-size:30px; margin:0;">🧠 NeuroScan AI</h1> | |
| <p style="font-size:14px; color:#666;">AI-Powered Brain Tumor Detection</p> | |
| <p style="font-size:14px; color:#666;">د مغز تومور تشخیص | تشخیص تومور مغزی</p> | |
| <span style="background:#1a73e8; color:white; padding:5px 16px; border-radius:20px; font-size:12px; font-weight:700;"> | |
| 🏥 REAL AI MODEL | 90% ACCURACY | |
| </span> | |
| </div> | |
| """) | |
| # Language selector | |
| gr.Markdown("### 🌐 Language | ژبه | زبان") | |
| with gr.Row(): | |
| btn_en = gr.Button("🇬🇧 English", variant="primary", size="sm") | |
| btn_ps = gr.Button("🇦🇫 پښتو", size="sm") | |
| btn_fa = gr.Button("🇮🇷 فارسی", size="sm") | |
| # Content | |
| tx = T["en"] | |
| gr.Markdown(f""" | |
| <div style="background:#fff3cd; padding:12px; border-radius:10px; border-left:4px solid #ffc107; margin:10px 0; font-size:12px;"> | |
| {tx['disclaimer']} | |
| </div> | |
| """) | |
| input_image = gr.Image(type="pil", label=tx['upload'], height=280) | |
| analyze_btn = gr.Button(tx['analyze'], variant="primary", size="lg") | |
| result_display = gr.Textbox(label="📊 Result", lines=10) | |
| gr.Markdown(""" | |
| <div class="dev-badge"> | |
| 👨💻 Developed by <strong>Abdul Basir Akbar</strong> | GIHS | Kabul, Afghanistan | © 2025 | |
| </div> | |
| """) | |
| with gr.Accordion(tx['specs'], open=False): | |
| gr.Markdown(tx['specs_text']) | |
| with gr.Accordion(tx['about'], open=False): | |
| gr.Markdown(tx['about_text']) | |
| with gr.Accordion(tx['contact'], open=False): | |
| gr.Markdown(tx['contact_text']) | |
| with gr.Accordion(tx['legal'], open=False): | |
| gr.Markdown(tx['legal_text']) | |
| # ============================================================ | |
| # LANGUAGE UPDATE | |
| # ============================================================ | |
| def update_ui(lang): | |
| tx = T[lang] | |
| return ( | |
| gr.update(value=tx['upload']), | |
| gr.update(value=tx['analyze']) | |
| ) | |
| btn_en.click(lambda: "en", outputs=current_lang).then( | |
| fn=lambda: update_ui("en"), outputs=[input_image, analyze_btn] | |
| ) | |
| btn_ps.click(lambda: "ps", outputs=current_lang).then( | |
| fn=lambda: update_ui("ps"), outputs=[input_image, analyze_btn] | |
| ) | |
| btn_fa.click(lambda: "fa", outputs=current_lang).then( | |
| fn=lambda: update_ui("fa"), outputs=[input_image, analyze_btn] | |
| ) | |
| # Analyze | |
| analyze_btn.click( | |
| fn=lambda img, lang: predict_tumor(img, lang), | |
| inputs=[input_image, current_lang], | |
| outputs=result_display | |
| ) | |
| # ============================================================ | |
| # LAUNCH - FIXED FOR GRADIO 5+ | |
| # ============================================================ | |
| app.launch(ssr_mode=False) |