# Flask API Integration Guide for Next.js ## ๐Ÿš€ Flask API Server Your Flask API server is now ready and running on **`http://localhost:5001`** ### ๐Ÿ”Œ Available Endpoints | Method | Endpoint | Description | |--------|----------|-------------| | `GET` | `/api/health` | Health check endpoint | | `GET` | `/api/weather?lat=X&lon=Y` | Get weather conditions | | `POST` | `/api/analyze-crashes` | Analyze crash patterns at location | | `POST` | `/api/find-safe-route` | Find safest route between points | | `POST` | `/api/get-single-route` | Get single route with safety analysis | --- ## ๐Ÿ“ฆ Starting the Server ```bash cd /path/to/VTHacks13/llm python api/flask_server.py ``` The server will start on `http://localhost:5001` with the following services: - โœ… MongoDB connection to crash database - โœ… Route safety analysis - โœ… Weather API integration (Open-Meteo) - โœ… Gemini AI for safety recommendations --- ## ๐Ÿ”ง Next.js Integration ### 1. Install Dependencies ```bash npm install axios # or use fetch API ``` ### 2. Create API Client Create `lib/api-client.js`: ```javascript const API_BASE_URL = 'http://localhost:5001/api'; // Health Check export async function checkAPIHealth() { const response = await fetch(`${API_BASE_URL}/health`); return response.json(); } // Weather API export async function getWeather(lat, lon) { const response = await fetch(`${API_BASE_URL}/weather?lat=${lat}&lon=${lon}`); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to fetch weather'); } return data; } // Crash Analysis export async function analyzeCrashes(lat, lon, radius = 1.0) { const response = await fetch(`${API_BASE_URL}/analyze-crashes`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ lat, lon, radius }) }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to analyze crashes'); } return data; } // Safe Route Finding export async function findSafeRoute(startLat, startLon, endLat, endLon) { const response = await fetch(`${API_BASE_URL}/find-safe-route`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_lat: startLat, start_lon: startLon, end_lat: endLat, end_lon: endLon }) }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to find safe route'); } return data; } // Single Route export async function getSingleRoute(startLat, startLon, endLat, endLon, profile = 'driving') { const response = await fetch(`${API_BASE_URL}/get-single-route`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ start_lat: startLat, start_lon: startLon, end_lat: endLat, end_lon: endLon, profile }) }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to get route'); } return data; } ``` ### 3. Example React Component Create `components/SafetyAnalysis.jsx`: ```jsx import { useState } from 'react'; import { analyzeCrashes, findSafeRoute, getWeather } from '../lib/api-client'; export default function SafetyAnalysis() { const [loading, setLoading] = useState(false); const [results, setResults] = useState(null); const [error, setError] = useState(null); const handleAnalyze = async () => { setLoading(true); setError(null); try { // Example: Analyze crashes around Virginia Tech const lat = 37.2284; const lon = -80.4234; const radius = 2.0; // Get crash analysis const crashData = await analyzeCrashes(lat, lon, radius); // Get safe route (Virginia Tech to Downtown Blacksburg) const routeData = await findSafeRoute(lat, lon, 37.2297, -80.4139); // Get weather const weatherData = await getWeather(lat, lon); setResults({ crashes: crashData, route: routeData, weather: weatherData }); } catch (err) { setError(err.message); } finally { setLoading(false); } }; return (

Safety Analysis

{error && (
Error: {error}
)} {results && (
{/* Crash Analysis Results */}

Crash Analysis

Total crashes: {results.crashes.crash_summary.total_crashes}

Total casualties: {results.crashes.crash_summary.total_casualties}

Weather: {results.crashes.weather.summary}

{/* Route Results */}

Safe Route

Distance: {results.route.recommended_route.distance_km.toFixed(1)} km

Duration: {results.route.recommended_route.duration_min.toFixed(0)} minutes

Crashes nearby: {results.route.recommended_route.crashes_nearby}

Safety score: {results.route.recommended_route.safety_score.toFixed(3)}

{/* Weather Results */}

Current Weather

{results.weather.summary}

)}
); } ``` ### 4. Mapbox Integration For route visualization: ```jsx import { useEffect, useRef } from 'react'; import mapboxgl from 'mapbox-gl'; export default function RouteMap({ routeData }) { const mapContainer = useRef(null); const map = useRef(null); useEffect(() => { if (!routeData || map.current) return; map.current = new mapboxgl.Map({ container: mapContainer.current, style: 'mapbox://styles/mapbox/streets-v12', center: [routeData.recommended_route.coordinates[0][0], routeData.recommended_route.coordinates[0][1]], zoom: 13 }); // Add route to map map.current.on('load', () => { map.current.addSource('route', { 'type': 'geojson', 'data': { 'type': 'Feature', 'properties': {}, 'geometry': routeData.recommended_route.geometry } }); map.current.addLayer({ 'id': 'route', 'type': 'line', 'source': 'route', 'layout': { 'line-join': 'round', 'line-cap': 'round' }, 'paint': { 'line-color': '#3887be', 'line-width': 5, 'line-opacity': 0.75 } }); }); }, [routeData]); return
; } ``` --- ## ๐Ÿ“Š Response Formats ### Crash Analysis Response ```json { "success": true, "location": {"lat": 37.2284, "lon": -80.4234}, "radius_km": 2.0, "crash_summary": { "total_crashes": 5, "avg_distance_km": 1.2, "severity_breakdown": {"Minor": 3, "Major": 2}, "total_casualties": 8 }, "weather": { "summary": "Clear sky, precipitation 0.0mm/h, wind 5.2 km/h, day" }, "safety_analysis": "AI-generated safety report..." } ``` ### Safe Route Response ```json { "success": true, "recommended_route": { "coordinates": [[lon, lat], [lon, lat], ...], "distance_km": 1.5, "duration_min": 4.2, "geometry": {...}, // GeoJSON for Mapbox "safety_score": 0.234, "crashes_nearby": 2 }, "safety_analysis": "AI-generated route safety report...", "weather_summary": "Current weather conditions...", "alternative_routes": [...] } ``` --- ## ๐Ÿงช Testing the API Run the test script: ```bash cd llm/api python test_api.py ``` Or test individual endpoints with curl: ```bash # Health check curl http://localhost:5001/api/health # Weather curl "http://localhost:5001/api/weather?lat=37.2284&lon=-80.4234" # Crash analysis curl -X POST http://localhost:5001/api/analyze-crashes \ -H "Content-Type: application/json" \ -d '{"lat": 37.2284, "lon": -80.4234, "radius": 1.0}' ``` --- ## ๐ŸŽฏ Next Steps 1. **Start Flask Server**: `cd llm && python api/flask_server.py` 2. **Test Endpoints**: Use the provided test scripts 3. **Integrate with Next.js**: Use the API client code above 4. **Add to Your Components**: Import and use the API functions 5. **Visualize Routes**: Use Mapbox with the route coordinates Your Flask API is ready to bridge your Python AI/safety analysis with your Next.js frontend! ๐Ÿš€