File size: 2,355 Bytes
e1c7ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import asyncio
import threading
import joblib
from fastapi import FastAPI, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
import socketio

# Import individual agent routers
from API.agent1_routes import router as agent1
from API.agent2_routes import router as agent2
from API.agent3_routes import router as agent3
from API.agent4_routes import router as agent4
from API.agent5_routes import router as agent5

# Import Premium Logic
from agents.agent1_premium import run_agent1_premium

app = FastAPI(title="ESG Multi-Agent Monitoring System")

# 1. Setup Socket.IO Server
sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins="*")
socket_app = socketio.ASGIApp(sio, app)

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

# Global Model Loading
MODEL_PATH = "models/risk_model.pkl"
app.state.model = joblib.load(MODEL_PATH) if os.path.exists(MODEL_PATH) else None

# Register Agent Routers
app.include_router(agent1)
app.include_router(agent2)
app.include_router(agent3)
app.include_router(agent4)
app.include_router(agent5)

# --- 💎 PREMIUM ROUTE (Fixed) ---
@app.post("/api/premium/agent1/start")
async def activate_premium_agent1(company_id: str = "company3"):
    """

    Starts the Agent 1 Premium background thread safely.

    """
    try:
        # 1. Main loop ka reference lenge
        main_loop = asyncio.get_running_loop()

        # 2. Corrected Callback: Thread-safe tareeke se emit karega
        def socket_callback(event, data):
            # Bina naya loop banaye, main loop par task schedule karega
            asyncio.run_coroutine_threadsafe(sio.emit(event, data), main_loop)

        # 3. Start the watcher thread
        thread = threading.Thread(
            target=run_agent1_premium, 
            args=(socket_callback, company_id)
        )
        thread.daemon = True
        thread.start()

        return {
            "status": "Success", 
            "message": f" Agent 1 Premium streaming started for {company_id}"
        }
    except Exception as e:
        return {"status": "Error", "message": str(e)}

@app.get("/")
def home():
    return {"message": "ESG Multi-Agent API Running Successfully"}