# SPARKNET Phase 3: Production Web UI Implementation Guide ## ๐ Phase 3 Progress: Backend Complete! **Status**: FastAPI Backend โ COMPLETE | Frontend ๐ง IN PROGRESS --- ## โ Completed: FastAPI Backend ### Files Created 1. **`api/main.py`** (~150 lines) - FastAPI application with lifecycle management - CORS middleware for frontend integration - Auto-initialization of SPARKNET components - Health check endpoints - OpenAPI documentation at `/api/docs` 2. **`api/routes/patents.py`** (~200 lines) - POST `/api/patents/upload` - Upload patent PDF - GET `/api/patents/{id}` - Get patent metadata - GET `/api/patents/` - List all patents with pagination - DELETE `/api/patents/{id}` - Delete patent - GET `/api/patents/{id}/download` - Download original PDF 3. **`api/routes/workflows.py`** (~300 lines) - POST `/api/workflows/execute` - Start Patent Wake-Up workflow - GET `/api/workflows/{id}` - Get workflow status - WS `/api/workflows/{id}/stream` - WebSocket for real-time updates - GET `/api/workflows/` - List all workflows - GET `/api/workflows/{id}/brief/download` - Download valorization brief 4. **`api/requirements.txt`** - FastAPI, Uvicorn, WebSockets, Pydantic dependencies --- ## ๐ Quick Start: Test the API ### Step 1: Install Dependencies ```bash cd /home/mhamdan/SPARKNET # Activate conda environment conda activate agentic-ai # Install FastAPI dependencies pip install fastapi uvicorn python-multipart websockets ``` ### Step 2: Start the API Server ```bash # Make sure Ollama is running # (Should already be running from background processes) # Start FastAPI python -m api.main ``` The API will be available at: - **API**: http://localhost:8000 - **Docs**: http://localhost:8000/api/docs (Interactive OpenAPI documentation) - **Health**: http://localhost:8000/api/health ### Step 3: Test with curl ```bash # Health check curl http://localhost:8000/api/health # Upload a patent curl -X POST http://localhost:8000/api/patents/upload \ -F "file=@Dataset/your_patent.pdf" # Start workflow (replace PATENT_ID) curl -X POST http://localhost:8000/api/workflows/execute \ -H "Content-Type: application/json" \ -d '{"patent_id": "PATENT_ID"}' # Check workflow status (replace WORKFLOW_ID) curl http://localhost:8000/api/workflows/WORKFLOW_ID ``` --- ## ๐ API Endpoints Reference ### Patents Endpoints | Method | Endpoint | Description | |--------|----------|-------------| | POST | `/api/patents/upload` | Upload patent PDF (max 50MB) | | GET | `/api/patents/{id}` | Get patent metadata | | GET | `/api/patents/` | List all patents (supports pagination) | | DELETE | `/api/patents/{id}` | Delete patent | | GET | `/api/patents/{id}/download` | Download original PDF | **Example Upload Response**: ```json { "patent_id": "550e8400-e29b-41d4-a716-446655440000", "filename": "ai_drug_discovery.pdf", "size": 2457600, "uploaded_at": "2025-11-04T20:00:00.000Z", "message": "Patent uploaded successfully" } ``` ### Workflows Endpoints | Method | Endpoint | Description | |--------|----------|-------------| | POST | `/api/workflows/execute` | Start Patent Wake-Up workflow | | GET | `/api/workflows/{id}` | Get workflow status and results | | WS | `/api/workflows/{id}/stream` | Real-time WebSocket updates | | GET | `/api/workflows/` | List all workflows (supports pagination) | | GET | `/api/workflows/{id}/brief/download` | Download valorization brief PDF | **Example Workflow Response**: ```json { "id": "workflow-uuid", "patent_id": "patent-uuid", "status": "running", "progress": 45, "current_step": "market_analysis", "started_at": "2025-11-04T20:01:00.000Z", "completed_at": null, "result": null } ``` **Workflow States**: - `queued` - Waiting to start - `running` - Currently executing - `completed` - Successfully finished - `failed` - Error occurred --- ## ๐ WebSocket Real-Time Updates The WebSocket endpoint provides live progress updates: ```javascript // JavaScript example const ws = new WebSocket('ws://localhost:8000/api/workflows/{workflow_id}/stream'); ws.onmessage = (event) => { const data = JSON.parse(event.data); console.log(`Status: ${data.status}, Progress: ${data.progress}%`); if (data.status === 'completed') { // Workflow finished, display results console.log('Results:', data.result); } }; ``` --- ## ๐จ Next Steps: Frontend Implementation ### Option 1: Build Next.js Frontend (Recommended) **Technologies**: - Next.js 14 with App Router - TypeScript for type safety - Tailwind CSS for styling - shadcn/ui for components - Framer Motion for animations **Setup Commands**: ```bash # Create Next.js app cd /home/mhamdan/SPARKNET npx create-next-app@latest frontend --typescript --tailwind --app cd frontend # Install dependencies npm install @radix-ui/react-dialog @radix-ui/react-progress npm install framer-motion recharts lucide-react npm install class-variance-authority clsx tailwind-merge # Install shadcn/ui npx shadcn-ui@latest init npx shadcn-ui@latest add button card input progress badge tabs dialog ``` **Key Pages to Build**: 1. **Home Page** (`app/page.tsx`) - Landing page with features 2. **Upload Page** (`app/upload/page.tsx`) - Drag-and-drop patent upload 3. **Workflow Page** (`app/workflow/[id]/page.tsx`) - Live progress tracking 4. **Results Page** (`app/results/[id]/page.tsx`) - Beautiful result displays ### Option 2: Simple HTML + JavaScript Frontend For quick testing, create a simple HTML interface: ```html