LATE.IO2 / backend /app.py
AIEONE
Initial commit syncing local server with Hugging Face Space
490ec84
from fastapi import FastAPI, UploadFile, File, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import pandas as pd
import os, json
# ----------------- Setup -----------------
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
BASE_DIR = os.path.dirname(__file__)
DATA_DIR = os.path.join(BASE_DIR, "data")
FRONTEND_DIST = os.getenv('STATIC_FILES_DIR', '/app/static')
#FRONTEND_DIST = os.path.join(BASE_DIR, "../frontend/dist/agentic-dashboard")
CREDENTIALS_PATH = os.path.join(DATA_DIR, "user_credentials.json")
ADMIN_ENV_PATH = os.path.join(BASE_DIR, "../.env")
AGENTS_FILE = os.path.join(DATA_DIR, "Agentic_Deployment_Master_Plan_v3.2.csv")
# ----------------- Models -----------------
class SimulationInput(BaseModel):
asset: str
capital: float
risk: int
class IntakeData(BaseModel):
role: str
github: str = None
docker: str = None
digitalocean: str = None
google: str = None
notion: str = None
custom: str = None
# ----------------- Routes -----------------
@app.get("/")
def serve_frontend():
index_file = os.path.join(FRONTEND_DIST, "index.html")
return FileResponse(index_file)
app.mount("/static", StaticFiles(directory=FRONTEND_DIST), name="static")
@app.get("/agent_status")
def agent_status():
try:
df = pd.read_csv(AGENTS_FILE)
enriched = [
{
"id": f"agent{idx}",
"name": row.get("name", f"Agent {idx}"),
"traits": row.get("traits", "unspecified"),
"risk": int(row.get("risk", 3)),
"status": "active",
"memory_kb": 0,
"endpoints": ["Google", "Notion", "Shodan"]
}
for idx, row in df.iterrows()
]
return {"active_agents": len(enriched), "agents": enriched}
except Exception as e:
return {"error": str(e)}
@app.post("/simulate")
def simulate(input: SimulationInput):
try:
projected = input.capital * (1 + input.risk / 10)
return {
"projected_yield": projected,
"agent_notes": f"Simulated projection for {input.asset}"
}
except Exception as e:
return {"error": str(e)}
@app.post("/upload_csv")
def upload_csv(file: UploadFile = File(...)):
try:
contents = file.file.read()
path = os.path.join(DATA_DIR, file.filename)
with open(path, "wb") as f:
f.write(contents)
return {"status": "uploaded", "path": path}
except Exception as e:
return {"error": str(e)}
@app.post("/intake")
def handle_intake(data: IntakeData):
try:
if data.role == "admin":
creds = {
"GITHUB_TOKEN": data.github,
"DOCKER_TOKEN": data.docker,
"DO_API_KEY": data.digitalocean
}
with open(ADMIN_ENV_PATH, "a") as f:
for k, v in creds.items():
if v:
f.write(f"{k}={v}\n")
return {"message": "Admin credentials saved to .env."}
elif data.role == "user":
session_data = {
"google": data.google,
"notion": data.notion,
"custom": data.custom
}
with open(CREDENTIALS_PATH, "w") as f:
json.dump(session_data, f)
return {"message": "User session credentials stored."}
return {"message": "Invalid role or missing data."}
except Exception as e:
return {"error": str(e)}
@app.post("/upload_credentials")
def upload_cred_file(file: UploadFile = File(...)):
try:
content = file.file.read()
with open(CREDENTIALS_PATH, "wb") as f:
f.write(content)
return {"message": "Credential file uploaded and stored."}
except Exception as e:
return {"error": str(e)}
@app.get("/credentials")
def view_creds():
try:
with open(CREDENTIALS_PATH) as f:
return json.load(f)
except FileNotFoundError:
return {"error": "No credentials file found."}
# ----------------- Routers -----------------
from routes.reddit_auth import router as reddit_router
from routes.discord_auth import router as discord_router
from routes.github_auth import router as github_router
from routes.huggingface_auth import router as hf_router
from routes.wayback_lookup import router as wayback_router
from routes.dork_search import router as dork_router
from routes.exploitdb_lookup import router as exploitdb_router
from routes.spreadsheet_intake import router as spreadsheet_router
from routes.neurocircuit_api import router as neurocircuit_router
from routes.mindmap_api import router as mindmap_router
app.include_router(reddit_router)
app.include_router(discord_router)
app.include_router(github_router)
app.include_router(hf_router)
app.include_router(wayback_router)
app.include_router(dork_router)
app.include_router(exploitdb_router)
app.include_router(spreadsheet_router)
app.include_router(neurocircuit_router)
app.include_router(mindmap_router)