File size: 2,447 Bytes
49cfc80
 
2d54210
 
 
49cfc80
2d54210
 
 
 
 
 
49cfc80
 
 
2d54210
49cfc80
 
 
 
 
 
 
 
2d54210
49cfc80
 
2d54210
49cfc80
 
 
2d54210
49cfc80
 
2d54210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49cfc80
2d54210
 
 
 
 
 
 
 
 
 
 
 
49cfc80
2d54210
49cfc80
2d54210
 
 
 
49cfc80
2d54210
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
from typing import List, Optional
import uvicorn
import os
import random
import json
from datetime import datetime

# Import detection logic (mocked or real)
# from detection import run_detection

app = FastAPI(title="Nora Research Lab Engine")

# Enable CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class DetectionRequest(BaseModel):
    lat: float
    lon: float
    radius: float = 1.0

@app.get("/")
def health_check():
    return {"status": "online", "service": "Nora Research Lab Engine"}

@app.post("/detect")
async def detect_hotspots(request: DetectionRequest):
    print(f"Received detection request: {request}")
    
    # In a real scenario, this would call the STAC API
    # For MVP/Demo in Replit, we simulate the processing or implement a light version
    
    # Simulating processing delay
    # import time
    # time.sleep(2)
    
    # MOCK LOGIC for demo (STAC requires credentials/complex env)
    # If we were to implement the full STAC logic here, we'd need 'pystac-client' installed
    # and access to the DE Africa catalog.
    
    # Generating mock hotspots around the center
    hotspots = []
    
    # 50% chance of finding something
    if True: # Always find something for demo
        for _ in range(random.randint(2, 5)):
            offset_lat = random.uniform(-0.02, 0.02)
            offset_lon = random.uniform(-0.02, 0.02)
            
            hotspots.append({
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [request.lon + offset_lon, request.lat + offset_lat]
                },
                "properties": {
                    "type": random.choice(["illegal_mining", "deforestation"]),
                    "confidence": round(random.uniform(0.7, 0.99), 2),
                    "ndvi_drop": f"{random.randint(30, 80)}%",
                    "bsi_increase": f"{random.randint(20, 50)}%",
                    "description": "Detected significant vegetation loss and soil exposure."
                }
            })

    return {
        "type": "FeatureCollection",
        "features": hotspots
    }

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)