Ashoka74's picture
Deploy current work to HF Space (slim)
a1aef88
|
Raw
History Blame Contribute Delete
11.9 kB

A newer version of the Streamlit SDK is available: 1.59.1

Upgrade

Implementation Plan: React + FastAPI Architecture

Overview

Convert the Streamlit-based UAP Data Analysis Tool to a modern React frontend with FastAPI backend, maintaining all existing functionality.

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     React Frontend                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ Parsing β”‚ β”‚Analysis β”‚ β”‚ Search  β”‚ β”‚Magnetic β”‚ β”‚  Map  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”¬β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚          β”‚          β”‚          β”‚          β”‚
        β–Ό          β–Ό          β–Ό          β–Ό          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    FastAPI Backend                          β”‚
β”‚  /api/parse  /api/analyze  /api/search  /api/magnetic /api/map β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Existing Python Services                       β”‚
β”‚  UAPParser  UAPAnalyzer  UAPVisualizer  Cohere  InterMagnet β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

File Structure

UAP-Data-Analysis-Tool/
β”œβ”€β”€ api/                          # FastAPI backend
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py                   # FastAPI app entry point
β”‚   β”œβ”€β”€ config.py                 # Settings and API keys
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ upload.py             # File upload endpoints
β”‚   β”‚   β”œβ”€β”€ parse.py              # OpenAI parsing endpoints
β”‚   β”‚   β”œβ”€β”€ analyze.py            # UMAP/HDBSCAN/XGBoost endpoints
β”‚   β”‚   β”œβ”€β”€ search.py             # Cohere rerank endpoints
β”‚   β”‚   β”œβ”€β”€ magnetic.py           # InterMagnet correlation endpoints
β”‚   β”‚   └── map.py                # Geospatial data endpoints
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ parser_service.py     # Wraps UAPParser
β”‚   β”‚   β”œβ”€β”€ analyzer_service.py   # Wraps UAPAnalyzer
β”‚   β”‚   β”œβ”€β”€ visualizer_service.py # Wraps UAPVisualizer
β”‚   β”‚   β”œβ”€β”€ search_service.py     # Cohere rerank logic
β”‚   β”‚   β”œβ”€β”€ magnetic_service.py   # InterMagnet API + DTW
β”‚   β”‚   └── map_service.py        # Kepler.gl data prep
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ schemas.py            # Pydantic request/response models
β”‚   β”‚   └── jobs.py               # Background job tracking
β”‚   └── utils/
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ file_handler.py       # CSV/Excel/HDF5 handling
β”‚       └── serialization.py      # NumPy/DataFrame serialization
β”‚
β”œβ”€β”€ frontend/                     # React frontend
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ tailwind.config.js
β”‚   β”œβ”€β”€ vite.config.js
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ main.jsx
β”‚   β”‚   β”œβ”€β”€ App.jsx
β”‚   β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”‚   └── client.js         # Axios/fetch wrapper
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ layout/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Navbar.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Sidebar.jsx
β”‚   β”‚   β”‚   β”‚   └── Layout.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ common/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ FileUpload.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ DataTable.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ LoadingSpinner.jsx
β”‚   β”‚   β”‚   β”‚   └── ErrorBoundary.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ charts/
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Treemap.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Histogram.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ScatterPlot.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ Heatmap.jsx
β”‚   β”‚   β”‚   β”‚   └── ConfusionMatrix.jsx
β”‚   β”‚   β”‚   └── map/
β”‚   β”‚   β”‚       └── KeplerMap.jsx
β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”‚   β”œβ”€β”€ Home.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Parsing.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Analysis.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Search.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Magnetic.jsx
β”‚   β”‚   β”‚   └── Map.jsx
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   β”œβ”€β”€ useFileUpload.js
β”‚   β”‚   β”‚   β”œβ”€β”€ useAnalysis.js
β”‚   β”‚   β”‚   └── useWebSocket.js
β”‚   β”‚   β”œβ”€β”€ store/
β”‚   β”‚   β”‚   └── index.js          # Zustand or React Context
β”‚   β”‚   └── styles/
β”‚   β”‚       └── globals.css
β”‚   └── public/
β”‚       └── assets/

Implementation Steps

Phase 1: FastAPI Backend Setup

Step 1.1: Create API structure and main entry point

  • Create api/ directory structure
  • Set up FastAPI app with CORS middleware
  • Configure settings from environment/secrets
  • Add health check endpoint

Step 1.2: Create Pydantic schemas

  • Define request models for each endpoint
  • Define response models with proper typing
  • Create job status models for async operations

Step 1.3: Implement file upload endpoint

  • POST /api/upload - Accept CSV/Excel files
  • Store uploaded files temporarily
  • Return file ID and column list
  • Support chunked uploads for large files

Step 1.4: Implement parsing endpoints

  • POST /api/parse/start - Start async parsing job
  • GET /api/parse/status/{job_id} - Check job status
  • GET /api/parse/result/{job_id} - Get parsed results
  • Wrap existing UAPParser with proper error handling

Step 1.5: Implement analysis endpoints

  • POST /api/analyze/start - Start clustering analysis
  • GET /api/analyze/status/{job_id} - Check status
  • GET /api/analyze/clusters/{job_id} - Get cluster data
  • GET /api/analyze/embeddings/{job_id} - Get 2D embeddings for visualization
  • GET /api/analyze/predictions/{job_id} - Get XGBoost results

Step 1.6: Implement search endpoints

  • POST /api/search/rerank - Cohere rerank search
  • Return ranked results with relevance scores

Step 1.7: Implement magnetic endpoints

  • GET /api/magnetic/stations - List InterMagnet stations
  • POST /api/magnetic/correlate - Run DTW correlation
  • Return correlation results and time series data

Step 1.8: Implement map endpoints

  • GET /api/map/sightings - Get sighting GeoJSON
  • GET /api/map/bases - Get military bases data
  • GET /api/map/plants - Get nuclear facilities data
  • GET /api/map/config - Get Kepler.gl config

Phase 2: React Frontend Setup

Step 2.1: Initialize React project

  • Create Vite + React project in frontend/
  • Install dependencies: react-router, axios, recharts/plotly, kepler.gl
  • Configure Tailwind CSS
  • Set up project structure

Step 2.2: Create layout components

  • Navbar with navigation links
  • Sidebar for feature options
  • Main layout wrapper
  • Responsive design

Step 2.3: Create common components

  • FileUpload with drag-and-drop
  • DataTable with sorting/filtering
  • LoadingSpinner and progress indicators
  • Error boundary and toast notifications

Step 2.4: Create chart components

  • Treemap using Plotly.js
  • Histogram using Recharts
  • ScatterPlot for embeddings visualization
  • Heatmap for Cramer's V and confusion matrix
  • Feature importance bar chart

Step 2.5: Create Kepler.gl map component

  • Integrate kepler.gl React component
  • Handle data layers dynamically
  • Support filtering by attributes

Phase 3: Feature Pages

Step 3.1: Parsing page

  • File upload interface
  • Column selector
  • Custom JSON schema editor (optional)
  • Progress indicator for parsing
  • Results table with download option

Step 3.2: Analysis page

  • Dataset loader (upload or use parsed data)
  • Column multi-selector for analysis
  • Visualization tabs: Embeddings, Clusters, Predictions, Correlations
  • Interactive charts with tooltips

Step 3.3: Search page

  • Dataset display
  • Query input
  • Column selector for search scope
  • Ranked results with relevance scores
  • Click-to-expand details

Step 3.4: Magnetic page

  • Date range selector
  • Location input (lat/lon or from dataset)
  • Station selector
  • Correlation results with time series chart

Step 3.5: Map page

  • Full-screen Kepler.gl map
  • Layer toggles (sightings, bases, plants)
  • Filter controls
  • Export functionality

Phase 4: Integration and Polish

Step 4.1: State management

  • Set up Zustand store for global state
  • Persist uploaded data across pages
  • Handle authentication state (API keys)

Step 4.2: WebSocket for long-running tasks

  • Add WebSocket endpoint for job progress
  • Real-time updates during parsing/analysis

Step 4.3: Error handling

  • Consistent error responses from API
  • User-friendly error messages in frontend
  • Retry logic for failed requests

Step 4.4: Testing

  • API endpoint tests with pytest
  • Component tests with React Testing Library

Key API Endpoints Summary

Method Endpoint Description
POST /api/upload Upload CSV/Excel file
POST /api/parse/start Start parsing job
GET /api/parse/status/{job_id} Get parsing status
GET /api/parse/result/{job_id} Get parsed data
POST /api/analyze/start Start analysis job
GET /api/analyze/clusters/{job_id} Get cluster results
GET /api/analyze/embeddings/{job_id} Get 2D embeddings
POST /api/search/rerank Semantic search
GET /api/magnetic/stations List stations
POST /api/magnetic/correlate Run correlation
GET /api/map/sightings Get sighting GeoJSON
GET /api/map/bases Get bases GeoJSON

Dependencies to Add

Backend (add to pyproject.toml)

fastapi
uvicorn[standard]
python-multipart
aiofiles
websockets

Frontend (package.json)

{
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.x",
    "axios": "^1.x",
    "plotly.js": "^2.x",
    "react-plotly.js": "^2.x",
    "kepler.gl": "^3.x",
    "react-dropzone": "^14.x",
    "@tanstack/react-table": "^8.x",
    "zustand": "^4.x",
    "react-hot-toast": "^2.x"
  },
  "devDependencies": {
    "vite": "^5.x",
    "tailwindcss": "^3.x",
    "autoprefixer": "^10.x",
    "postcss": "^8.x"
  }
}

Running the Application

# Terminal 1: Start FastAPI backend
cd api && uvicorn main:app --reload --port 8000

# Terminal 2: Start React frontend
cd frontend && npm run dev

Notes

  • Long-running tasks (parsing, analysis) use background jobs with polling or WebSocket updates
  • Embeddings are stored server-side and referenced by job_id to avoid large payloads
  • Visualizations are generated as Plotly JSON for interactive frontend rendering
  • The existing uap_analyzer.py and utils/ modules are reused as services