Skip to content

Latest commit

 

History

History
290 lines (245 loc) · 9.23 KB

File metadata and controls

290 lines (245 loc) · 9.23 KB

TransPolymer SvelteKit Frontend Development Workflow

Current Status

State: COMPLETE - SvelteKit Frontend & FastAPI Backend Ready

Project Overview

TransPolymer is a RoBERTa-based transformer model for polymer property predictions. Currently deployed as a Streamlit app, we're now creating a modern SvelteKit frontend with a separate FastAPI backend for better scalability and user experience.

Current Architecture:

  • Streamlit app with embedded PyTorch model (app.py)
  • Direct model inference in Python
  • Single-file deployment

Target Architecture:

  • SvelteKit frontend (TypeScript + TailwindCSS)
  • FastAPI backend with model serving
  • Separated frontend/backend deployment
  • Modern, responsive UI with better UX

Plan

Phase 1: Backend API Development (FastAPI)

1.1: Extract Model Logic

  • Create backend/main.py with FastAPI application
  • Extract model loading logic from app.py into reusable service
  • Implement model caching and singleton pattern
  • Add CORS middleware for SvelteKit integration

1.2: API Endpoints Implementation

// API Structure
POST /api/predict
  Body: {smiles: string, property: string}
  Response: {prediction: number, units: string, valid: boolean, error?: string}

POST /api/validate  
  Body: {smiles: string}
  Response: {valid: boolean, message: string}

POST /api/batch
  Body: FormData (CSV file + property type)
  Response: CSV file with predictions

GET /api/properties
  Response: {[key: string]: PropertyInfo}

GET /api/health
  Response: {status: "ok", model_loaded: boolean}

1.3: Backend File Structure

backend/
├── main.py              # FastAPI app
├── models/              
│   ├── __init__.py
│   ├── transpolymer.py  # Model loading/inference
│   └── validation.py    # SMILES validation
├── routers/
│   ├── __init__.py
│   ├── predict.py       # Prediction endpoints
│   └── batch.py         # Batch processing
├── requirements.txt     # Backend dependencies
└── Dockerfile          # Container setup

Phase 2: SvelteKit Frontend Development

2.1: Project Initialization

  • Initialize SvelteKit project with TypeScript
  • Setup TailwindCSS for styling
  • Configure Vite for development
  • Setup ESLint and Prettier

2.2: Component Architecture

src/
├── lib/
│   ├── components/
│   │   ├── PropertySelector.svelte    # Property dropdown with info
│   │   ├── SmilesInput.svelte         # SMILES input with validation
│   │   ├── ResultsDisplay.svelte      # Prediction results
│   │   ├── BatchProcessor.svelte      # File upload/batch processing
│   │   ├── LoadingSpinner.svelte      # Loading states
│   │   ├── ErrorAlert.svelte          # Error handling
│   │   └── Navigation.svelte          # Site navigation
│   ├── stores/
│   │   ├── api.ts                     # API service layer
│   │   ├── property.ts                # Property state management
│   │   └── prediction.ts              # Prediction state
│   ├── types/
│   │   ├── api.ts                     # API type definitions
│   │   └── polymer.ts                 # Polymer-specific types
│   └── utils/
│       ├── validation.ts              # Client-side validation
│       └── formatting.ts              # Number/unit formatting
├── routes/
│   ├── +layout.svelte                 # Main layout
│   ├── +page.svelte                   # Home - single predictions
│   ├── batch/
│   │   └── +page.svelte               # Batch processing page
│   └── about/
│       └── +page.svelte               # Model information
└── app.html                           # HTML template

2.3: Key Features Implementation

  • Real-time SMILES validation with visual feedback
  • Property selection with detailed information panels
  • Responsive design for mobile/desktop
  • Progress tracking for batch operations
  • File download functionality
  • Error handling with user-friendly messages
  • Loading states for all async operations

Phase 3: Integration & Polish

3.1: API Integration

  • Implement service layer for backend communication
  • Add proper error handling and retry logic
  • Configure environment variables for API endpoints
  • Add request/response type safety

3.2: UI/UX Enhancements

  • Implement modern chemical structure visualization
  • Add tooltips and help text
  • Create responsive grid layouts
  • Add animations and micro-interactions
  • Implement dark/light theme support

3.3: Testing & Documentation

  • Unit tests for components
  • API integration tests
  • E2E testing with Playwright
  • Component documentation with Storybook
  • Deployment documentation

Technology Stack

Frontend:

  • SvelteKit 2.0+ (TypeScript)
  • TailwindCSS for styling
  • Vite for build tooling
  • Playwright for testing

Backend:

  • FastAPI (Python 3.8+)
  • PyTorch for model inference
  • Uvicorn ASGI server
  • Pydantic for data validation

Deployment:

  • Frontend: Vercel/Netlify
  • Backend: Docker + AWS ECS/Railway
  • Database: None (stateless)

File Structure Overview

TransPolymer/
├── backend/                 # FastAPI backend
│   ├── main.py
│   ├── models/
│   ├── routers/
│   ├── requirements.txt
│   └── Dockerfile
├── frontend/                # SvelteKit frontend  
│   ├── src/
│   ├── package.json
│   ├── svelte.config.js
│   ├── tailwind.config.js
│   └── vite.config.ts
├── app.py                   # Original Streamlit app (keep for reference)
├── ckpt/                    # Model checkpoints
├── data/                    # Datasets
└── README.md               # Updated documentation

Benefits of New Architecture

Improved User Experience:

  • Modern, responsive design
  • Real-time validation feedback
  • Better error handling
  • Faster loading times

Developer Experience:

  • Type safety with TypeScript
  • Component-based architecture
  • Hot reloading during development
  • Easier testing and maintenance

Scalability:

  • Separated frontend/backend
  • Independent deployment
  • Better caching strategies
  • API can serve multiple clients

Deployment Flexibility:

  • Frontend CDN deployment
  • Backend containerization
  • Environment-specific configurations
  • Easy scaling options

Next Steps

Please review this plan and confirm approval to proceed. Once approved, I'll begin implementation with:

  1. Backend API development (FastAPI)
  2. SvelteKit project setup
  3. Component implementation
  4. Integration and testing

Log

  • Analyzed existing Streamlit application structure
  • Designed separated frontend/backend architecture
  • Created comprehensive implementation plan for SvelteKit frontend
  • Defined API specifications and component architecture
  • Ready for user approval to begin development

Phase 1: Backend API Development ✅ COMPLETE

  • Created FastAPI application structure (backend/)
  • Extracted model logic from app.py into TransPolymerService
  • Implemented prediction, validation, batch processing, and properties endpoints
  • Added CORS middleware and proper error handling
  • Created requirements.txt and Dockerfile for containerization

Phase 2: SvelteKit Frontend Development ✅ COMPLETE

  • Initialized SvelteKit project with TypeScript
  • Setup TailwindCSS with custom theme and components
  • Created TypeScript type definitions (api.ts, polymer.ts)
  • Implemented API service layer with full backend integration
  • Created Svelte stores for state management (property, prediction)
  • Implemented core Svelte components:
    • PropertySelector.svelte (property selection with info display)
    • SmilesInput.svelte (SMILES input with real-time validation)
    • ResultsDisplay.svelte (prediction results with formatting)
  • Created main application page with full integration
  • Added utility functions (debounce, formatting)
  • Responsive design with modern UI/UX

Phase 3: Integration & Testing ✅ READY

Both backend and frontend are complete and ready for testing:

To run the backend (FastAPI):

Option 1 - Automated Install (Recommended for Python 3.12):

cd backend
python install_requirements.py
python main.py

Option 2 - Manual Install:

cd backend
pip install -r requirements.txt
python main.py

If you encounter PyTorch build errors with Python 3.12:

# Use CPU-only PyTorch for compatibility
pip install torch==2.2.0+cpu --index-url https://download.pytorch.org/whl/cpu
pip install -r requirements.txt

Backend will run on http://localhost:8000 with API docs at http://localhost:8000/docs

To run the frontend (SvelteKit):

cd frontend
npm install
npm run dev
# Frontend will run on http://localhost:5173

Features implemented:

  • ✅ Real-time SMILES validation
  • ✅ Property selection with detailed information
  • ✅ Prediction with loading states and error handling
  • ✅ Responsive design for desktop/mobile
  • ✅ Modern UI with TailwindCSS
  • ✅ Type-safe API integration
  • ✅ State management with Svelte stores
  • ✅ Copy to clipboard functionality
  • ✅ Auto-resizing text areas
  • ✅ Debounced validation
  • ✅ Scientific notation formatting
  • ✅ Example SMILES functionality