Skip to content

Prepare NeuroScan for Railway deployment: Add CORS support, environment-based config, and comprehensive deployment documentation#2

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/fix-69c6918e-d325-4334-847d-85a6f7d1b42e
Draft

Prepare NeuroScan for Railway deployment: Add CORS support, environment-based config, and comprehensive deployment documentation#2
Copilot wants to merge 5 commits intomainfrom
copilot/fix-69c6918e-d325-4334-847d-85a6f7d1b42e

Conversation

Copy link

Copilot AI commented Oct 3, 2025

Overview

This PR prepares the NeuroScan Brain Tumor Classification application for production deployment on Railway by implementing security best practices, enabling CORS for frontend-backend communication, and providing comprehensive deployment documentation.

Changes Made

🔐 Security Improvements

Replaced hardcoded API key with environment variable:

# Before:
genai.configure(api_key='Add Your Own APi Key')

# After:
api_key = os.getenv('GOOGLE_API_KEY')
if api_key:
    genai.configure(api_key=api_key)
    gemini_vision_model = genai.GenerativeModel('gemini-2.5-flash-preview-05-20')
else:
    logger.warning("GOOGLE_API_KEY environment variable not set.")
    gemini_vision_model = None

This change ensures API keys are never committed to the repository and can be managed securely through environment variables.

🌐 CORS Support

Added Flask-CORS for cross-origin requests:

from flask_cors import CORS

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes

This allows the Next.js frontend (deployed on Vercel/Netlify) to communicate with the Flask backend (deployed on Railway) without CORS errors.

⚡ Production-Ready Server

Updated Dockerfile to use Gunicorn:

# Before:
CMD ["python", "server1.py"]

# After:
CMD gunicorn --bind 0.0.0.0:${PORT:-5050} --timeout 120 server1:app

Gunicorn is a production-grade WSGI server that provides better performance, stability, and request handling compared to Flask's development server. The 120-second timeout accommodates ML model inference time.

📚 Comprehensive Documentation

Added extensive deployment documentation:

  • DEPLOYMENT.md (253 lines): Complete Railway deployment guide with step-by-step instructions, environment variable reference, troubleshooting section, and multiple storage strategies for large model files (Git LFS, Google Cloud Storage, AWS S3, Railway volumes)

  • DEPLOYMENT_CHECKLIST.md (205 lines): Interactive checklist covering pre-deployment preparation, environment configuration, deployment steps, and post-deployment testing

  • CHANGES_SUMMARY.md (204 lines): Detailed documentation of all changes made, including before/after code comparisons and impact analysis

  • .env.example: Backend environment variables template with clear documentation

    GOOGLE_API_KEY=your_gemini_api_key_here
    PORT=5050
    UPLOAD_FOLDER=Uploads
    DATASET_PATH=./Dataset
    MODEL_PATH=mobilenet_brain_tumor_classifier.h5
  • .env.local.example: Frontend environment variables template

    NEXT_PUBLIC_API_URL=https://your-railway-backend.railway.app
  • README.md enhancements: Added dedicated Railway deployment section with configuration examples and backend integration instructions

Dependencies

All required dependencies were already present in requirements.txt:

  • ✅ Flask==2.3.3
  • ✅ Flask-Cors==4.0.0
  • ✅ gunicorn==20.1.0
  • ✅ TensorFlow, NumPy, OpenCV, Pillow, Werkzeug
  • ✅ google-generativeai==0.8.5

No dependency updates needed.

Deployment Architecture

Frontend (Vercel/Netlify)
    ↓ HTTP Requests with CORS
Backend (Railway)
    ├─→ Gunicorn WSGI Server
    ├─→ TensorFlow Model (local .h5 file)
    └─→ Google Gemini API (via env variable)

Testing

The changes maintain backward compatibility:

  • Application still works locally with python server1.py
  • Gemini API is optional (app continues without it if key not set)
  • All existing endpoints remain functional
  • CORS doesn't break any existing functionality

Next Steps for Deployment

  1. Get Gemini API Key: Visit https://makersuite.google.com/app/apikey
  2. Deploy to Railway:
    • Create new project from GitHub repository
    • Set GOOGLE_API_KEY environment variable
    • Railway auto-detects Dockerfile and deploys
  3. Configure Frontend:
    • Create .env.local with NEXT_PUBLIC_API_URL pointing to Railway backend
    • Deploy to Vercel/Netlify
  4. Handle Model File: Choose storage strategy from documentation (Git LFS, cloud storage, or Railway volume)

Documentation Reference

  • Quick Start: Follow DEPLOYMENT_CHECKLIST.md for step-by-step instructions
  • Detailed Guide: See DEPLOYMENT.md for comprehensive deployment information
  • Changes Summary: Review CHANGES_SUMMARY.md for all modifications made

This PR makes the application production-ready with secure configuration, proper CORS handling, and extensive documentation to ensure smooth Railway deployment. All changes follow security best practices and maintain full backward compatibility.

Original prompt

Update server1.py to replace hardcoded Gemini API key with os.getenv('GOOGLE_API_KEY'), add flask_cors import and CORS(app) initialization, and ensure Flask uses the PORT environment variable for Railway. Update requirements.txt to add flask-cors and gunicorn, and ensure all required dependencies are present (flask, flask-cors, numpy, tensorflow, pillow, opencv-python, google-generativeai, werkzeug, gunicorn). Also, advise the user to create .env.local in frontend with NEXT_PUBLIC_API_URL set to Railway backend URL, and to upload mobilenet_brain_tumor_classifier.h5 and dataset files to storage or repo.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@vercel
Copy link

vercel bot commented Oct 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
neuro-scan-brain-tumor-classification Ready Ready Preview Comment Oct 3, 2025 8:18am

Co-authored-by: yashnaiduu <152394598+yashnaiduu@users.noreply.github.com>
Co-authored-by: yashnaiduu <152394598+yashnaiduu@users.noreply.github.com>
Co-authored-by: yashnaiduu <152394598+yashnaiduu@users.noreply.github.com>
Co-authored-by: yashnaiduu <152394598+yashnaiduu@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix Deployment for Railway Prepare NeuroScan for Railway deployment: Add CORS support, environment-based config, and comprehensive deployment documentation Oct 3, 2025
Copilot AI requested a review from yashnaiduu October 3, 2025 08:22
Copy link
Owner

@yashnaiduu yashnaiduu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants