Spaces:
Sleeping
Sleeping
| # Complete code for Hugging Face Spaces deployment | |
| # Your CSV file name: brain_tumor_dataset.csv | |
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.preprocessing import LabelEncoder | |
| import os | |
| print("Loading Brain Tumor Detection System...") | |
| # Load dataset from CSV file | |
| csv_file = 'brain_tumor_dataset.csv' | |
| if not os.path.exists(csv_file): | |
| print(f"Error: {csv_file} not found!") | |
| print("Please upload brain_tumor_dataset.csv to the current directory") | |
| else: | |
| df = pd.read_csv(csv_file) | |
| print(f"โ Dataset loaded: {len(df)} patient records") | |
| print(f"๐ Columns in CSV: {list(df.columns)}") | |
| # Check which columns exist and use only available ones | |
| available_columns = df.columns.tolist() | |
| # Define required columns | |
| required_columns = ['Age', 'Gender', 'Headache_Score', 'Memory_Loss', 'Visual_Disturbance', | |
| 'Nausea', 'Vomiting', 'Seizures', 'Weakness', 'Coordination_Issues', 'Tumor_Detected'] | |
| # Check if all required columns exist | |
| for col in required_columns: | |
| if col not in available_columns: | |
| print(f"โ ๏ธ Warning: '{col}' not found in CSV") | |
| # Use only columns that exist | |
| feature_columns = [col for col in required_columns if col in available_columns] | |
| print(f"โ Using features: {feature_columns}") | |
| # Train model with available columns | |
| le_gender = LabelEncoder() | |
| le_memory = LabelEncoder() | |
| le_visual = LabelEncoder() | |
| le_nausea = LabelEncoder() | |
| le_vomiting = LabelEncoder() | |
| le_seizures = LabelEncoder() | |
| le_weakness = LabelEncoder() | |
| le_coordination = LabelEncoder() | |
| le_tumor = LabelEncoder() | |
| # Encode categorical columns | |
| df['Gender_E'] = le_gender.fit_transform(df['Gender']) | |
| df['Memory_E'] = le_memory.fit_transform(df['Memory_Loss']) | |
| df['Visual_E'] = le_visual.fit_transform(df['Visual_Disturbance']) | |
| df['Nausea_E'] = le_nausea.fit_transform(df['Nausea']) | |
| df['Vomiting_E'] = le_vomiting.fit_transform(df['Vomiting']) | |
| df['Seizures_E'] = le_seizures.fit_transform(df['Seizures']) | |
| df['Weakness_E'] = le_weakness.fit_transform(df['Weakness']) | |
| df['Coordination_E'] = le_coordination.fit_transform(df['Coordination_Issues']) | |
| df['Tumor_E'] = le_tumor.fit_transform(df['Tumor_Detected']) | |
| # Prepare features | |
| X = df[['Age', 'Gender_E', 'Headache_Score', 'Memory_E', 'Visual_E', | |
| 'Nausea_E', 'Vomiting_E', 'Seizures_E', 'Weakness_E', 'Coordination_E']] | |
| y = df['Tumor_E'] | |
| # Train model | |
| model = RandomForestClassifier(n_estimators=200, max_depth=15, random_state=42) | |
| model.fit(X, y) | |
| print("โ Model trained successfully!") | |
| def predict_tumor(age, gender, headache, memory_loss, visual_disturbance, | |
| nausea, vomiting, seizures, weakness, coordination_issues): | |
| gender_e = 0 if gender == 'Male' else 1 | |
| memory_e = 1 if memory_loss == 'Yes' else 0 | |
| visual_e = 1 if visual_disturbance == 'Yes' else 0 | |
| nausea_e = 1 if nausea == 'Yes' else 0 | |
| vomiting_e = 1 if vomiting == 'Yes' else 0 | |
| seizures_e = 1 if seizures == 'Yes' else 0 | |
| weakness_e = 1 if weakness == 'Yes' else 0 | |
| coordination_e = 1 if coordination_issues == 'Yes' else 0 | |
| input_data = [[age, gender_e, headache, memory_e, visual_e, nausea_e, | |
| vomiting_e, seizures_e, weakness_e, coordination_e]] | |
| prediction = model.predict(input_data)[0] | |
| probability = model.predict_proba(input_data)[0][1] | |
| result = "TUMOR DETECTED" if prediction == 1 else "NO TUMOR" | |
| if prediction == 1: | |
| recommendations = """ | |
| ๐จ **URGENT RECOMMENDATIONS:** | |
| โข Consult a neurologist immediately | |
| โข Schedule brain MRI with contrast | |
| โข Get a complete neurological examination | |
| โข Do not delay treatment | |
| """ | |
| color = "#c8a2c8" | |
| icon = "โ ๏ธ" | |
| else: | |
| recommendations = """ | |
| โ **HEALTHY RECOMMENDATIONS:** | |
| โข Continue regular health checkups | |
| โข Maintain healthy lifestyle | |
| โข Monitor any persistent symptoms | |
| โข Annual medical examination | |
| """ | |
| color = "#e6e6fa" | |
| icon = "โ " | |
| return result, f"{probability*100:.1f}%", recommendations, color, icon | |
| custom_css = """ | |
| <style> | |
| @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700;900&family=Poppins:wght@300;400;600;700&display=swap'); | |
| body { | |
| background: linear-gradient(45deg, #4b0082, #9370db, #d8bfd8, #e6e6fa, #dda0dd); | |
| background-size: 400% 400%; | |
| animation: gradientShift 15s ease infinite; | |
| font-family: 'Poppins', sans-serif; | |
| } | |
| @keyframes gradientShift { | |
| 0% { background-position: 0% 50%; } | |
| 50% { background-position: 100% 50%; } | |
| 100% { background-position: 0% 50%; } | |
| } | |
| body::before { | |
| content: ''; | |
| position: fixed; | |
| top: 0; | |
| left: 0; | |
| width: 100%; | |
| height: 100%; | |
| background: radial-gradient(circle at 20% 50%, rgba(216,191,216,0.2) 0%, transparent 50%); | |
| pointer-events: none; | |
| animation: floatParticles 20s infinite; | |
| z-index: 0; | |
| } | |
| @keyframes floatParticles { | |
| 0%, 100% { transform: translate(0, 0); opacity: 0.3; } | |
| 50% { transform: translate(100px, -50px); opacity: 0.6; } | |
| } | |
| .gradio-container { | |
| background: transparent !important; | |
| max-width: 1400px !important; | |
| margin: auto !important; | |
| padding: 20px !important; | |
| } | |
| h1 { | |
| text-align: center; | |
| font-family: 'Orbitron', monospace; | |
| font-size: 3.5em !important; | |
| font-weight: 900 !important; | |
| background: linear-gradient(135deg, #4b0082, #9370db, #d8bfd8, #e6e6fa); | |
| background-size: 300% 300%; | |
| -webkit-background-clip: text; | |
| background-clip: text; | |
| color: transparent; | |
| animation: titleGlow 3s ease infinite, textShine 5s linear infinite; | |
| margin-bottom: 10px !important; | |
| } | |
| @keyframes titleGlow { | |
| 0%, 100% { filter: drop-shadow(0 0 20px rgba(75,0,130,0.5)); } | |
| 50% { filter: drop-shadow(0 0 40px rgba(147,112,219,0.8)); } | |
| } | |
| @keyframes textShine { | |
| 0% { background-position: 0% 50%; } | |
| 100% { background-position: 100% 50%; } | |
| } | |
| .subtitle { | |
| text-align: center; | |
| color: #d8bfd8; | |
| font-size: 1.2em; | |
| margin-bottom: 30px; | |
| text-shadow: 0 0 10px rgba(216,191,216,0.5); | |
| animation: pulse 2s infinite; | |
| } | |
| @keyframes pulse { | |
| 0%, 100% { opacity: 0.8; } | |
| 50% { opacity: 1; text-shadow: 0 0 20px rgba(216,191,216,0.8); } | |
| } | |
| .card { | |
| background: rgba(255,255,255,0.95); | |
| backdrop-filter: blur(10px); | |
| border-radius: 20px; | |
| padding: 25px; | |
| margin: 15px 0; | |
| box-shadow: 0 15px 35px rgba(75,0,130,0.3); | |
| transition: transform 0.3s, box-shadow 0.3s; | |
| border: 1px solid rgba(216,191,216,0.3); | |
| } | |
| .card:hover { | |
| transform: translateY(-5px); | |
| box-shadow: 0 20px 45px rgba(147,112,219,0.4); | |
| } | |
| label { | |
| font-weight: 600 !important; | |
| color: #4b0082 !important; | |
| font-size: 0.95em !important; | |
| margin-bottom: 8px !important; | |
| display: block !important; | |
| } | |
| input, select, textarea { | |
| background: white !important; | |
| border: 2px solid #d8bfd8 !important; | |
| border-radius: 12px !important; | |
| padding: 10px 15px !important; | |
| font-size: 14px !important; | |
| transition: all 0.3s !important; | |
| color: #4b0082 !important; | |
| } | |
| input:focus, select:focus, textarea:focus { | |
| border-color: #9370db !important; | |
| box-shadow: 0 0 0 3px rgba(147,112,219,0.2) !important; | |
| outline: none !important; | |
| } | |
| input[type="range"] { | |
| background: linear-gradient(90deg, #4b0082, #9370db, #d8bfd8, #e6e6fa) !important; | |
| height: 8px !important; | |
| border-radius: 10px !important; | |
| } | |
| input[type="range"]::-webkit-slider-thumb { | |
| background: #9370db !important; | |
| width: 20px !important; | |
| height: 20px !important; | |
| border-radius: 50% !important; | |
| cursor: pointer !important; | |
| box-shadow: 0 0 10px #9370db !important; | |
| } | |
| .gr-button { | |
| background: linear-gradient(135deg, #4b0082, #9370db, #d8bfd8) !important; | |
| background-size: 200% 200% !important; | |
| color: white !important; | |
| border: none !important; | |
| padding: 12px 30px !important; | |
| font-size: 1.1em !important; | |
| font-weight: bold !important; | |
| border-radius: 50px !important; | |
| cursor: pointer !important; | |
| transition: all 0.3s !important; | |
| animation: buttonGradient 3s ease infinite; | |
| } | |
| @keyframes buttonGradient { | |
| 0% { background-position: 0% 50%; } | |
| 50% { background-position: 100% 50%; } | |
| 100% { background-position: 0% 50%; } | |
| } | |
| .gr-button:hover { | |
| transform: scale(1.05); | |
| box-shadow: 0 10px 25px rgba(75,0,130,0.5); | |
| } | |
| .footer { | |
| text-align: center; | |
| padding: 20px; | |
| color: #e6e6fa; | |
| background: rgba(75,0,130,0.3); | |
| border-radius: 15px; | |
| margin-top: 30px; | |
| font-size: 0.9em; | |
| } | |
| ::-webkit-scrollbar { | |
| width: 10px; | |
| height: 10px; | |
| } | |
| ::-webkit-scrollbar-track { | |
| background: rgba(216,191,216,0.1); | |
| border-radius: 10px; | |
| } | |
| ::-webkit-scrollbar-thumb { | |
| background: linear-gradient(135deg, #4b0082, #9370db); | |
| border-radius: 10px; | |
| } | |
| input[type="radio"] { | |
| accent-color: #9370db !important; | |
| } | |
| @media (max-width: 768px) { | |
| h1 { font-size: 2em !important; } | |
| .card { padding: 15px !important; } | |
| } | |
| </style> | |
| """ | |
| with gr.Blocks(css=custom_css, title="Brain Tumor Detection System") as demo: | |
| gr.HTML(""" | |
| <h1>๐ง BRAIN TUMOR DETECTION SYSTEM</h1> | |
| <div class="subtitle"> | |
| โจ AI-Powered Medical Diagnosis | Instant Results โจ | |
| </div> | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| with gr.Group(elem_classes="card"): | |
| gr.Markdown("### ๐ PATIENT INFORMATION") | |
| age = gr.Slider( | |
| minimum=1, maximum=120, value=45, step=1, | |
| label="๐ Age (Years)", | |
| info="Patient's current age" | |
| ) | |
| gender = gr.Radio( | |
| choices=['Male', 'Female'], label="โฅ Gender", | |
| info="Biological sex", value='Male' | |
| ) | |
| headache = gr.Slider( | |
| minimum=1, maximum=60, value=30, step=1, | |
| label="๐ค Headache Severity (1-60)", | |
| info="Pain intensity scale - higher score means more severe headache" | |
| ) | |
| with gr.Group(elem_classes="card"): | |
| gr.Markdown("### ๐ง NEUROLOGICAL SYMPTOMS") | |
| memory_loss = gr.Radio( | |
| choices=['No', 'Yes'], label="๐ Memory Loss", | |
| info="Forgetting recent events", value='No' | |
| ) | |
| visual_disturbance = gr.Radio( | |
| choices=['No', 'Yes'], label="๐๏ธ Visual Disturbance", | |
| info="Blurred or double vision", value='No' | |
| ) | |
| coordination_issues = gr.Radio( | |
| choices=['No', 'Yes'], label="๐ฏ Coordination Issues", | |
| info="Difficulty walking or maintaining balance", value='No' | |
| ) | |
| with gr.Column(scale=1): | |
| with gr.Group(elem_classes="card"): | |
| gr.Markdown("### ๐ฅ PHYSICAL SYMPTOMS") | |
| nausea = gr.Radio( | |
| choices=['No', 'Yes'], label="๐คข Nausea", | |
| info="Feeling sick to stomach", value='No' | |
| ) | |
| vomiting = gr.Radio( | |
| choices=['No', 'Yes'], label="๐คฎ Vomiting", | |
| info="Throwing up, especially in morning", value='No' | |
| ) | |
| seizures = gr.Radio( | |
| choices=['No', 'Yes'], label="โก Seizures", | |
| info="Uncontrolled body movements", value='No' | |
| ) | |
| weakness = gr.Radio( | |
| choices=['No', 'Yes'], label="๐ช Weakness", | |
| info="Unexplained loss of strength", value='No' | |
| ) | |
| predict_btn = gr.Button("๐ PREDICT NOW", variant="primary", size="lg") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| with gr.Group(elem_classes="card"): | |
| gr.Markdown("### ๐ DIAGNOSIS RESULT") | |
| result_text = gr.HTML(label="Prediction") | |
| probability = gr.Textbox( | |
| label="Confidence Score", interactive=False, | |
| placeholder="Click predict to see result", | |
| lines=1, show_label=True | |
| ) | |
| recommendations = gr.Markdown("### ๐ก RECOMMENDATIONS\n\n*Click predict to see recommendations*") | |
| def handle_prediction(age, gender, headache, memory_loss, visual_disturbance, | |
| nausea, vomiting, seizures, weakness, coordination_issues): | |
| result, prob, recs, color, icon = predict_tumor( | |
| age, gender, headache, memory_loss, visual_disturbance, | |
| nausea, vomiting, seizures, weakness, coordination_issues | |
| ) | |
| styled_result = f""" | |
| <div style='background: {color}; padding: 25px; border-radius: 15px; text-align: center; border: 2px solid #9370db;'> | |
| <div style='font-size: 4em;'>{icon}</div> | |
| <div style='font-size: 2em; font-weight: bold; margin: 15px 0; color: #4b0082;'>{result}</div> | |
| <div style='font-size: 1.2em; margin-top: 10px; color: #4b0082;'>Risk Assessment Complete</div> | |
| </div> | |
| """ | |
| return styled_result, prob, recs | |
| predict_btn.click( | |
| handle_prediction, | |
| inputs=[age, gender, headache, memory_loss, visual_disturbance, | |
| nausea, vomiting, seizures, weakness, coordination_issues], | |
| outputs=[result_text, probability, recommendations] | |
| ) | |
| gr.HTML(""" | |
| <div class="footer"> | |
| <p>๐ง Brain Tumor Detection System | AI-Powered Healthcare Assistant</p> | |
| <p>โ ๏ธ Medical Disclaimer: This tool is for educational purposes. Always consult healthcare professionals.</p> | |
| <p>๐ Trained on patient records | Real-time AI Prediction</p> | |
| </div> | |
| """) | |
| demo.launch() |