Geolive / main.py
Adedoyinjames's picture
Update main.py
2d54210 verified
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)