Spaces:
Running
Running
File size: 1,608 Bytes
6dad1de afe1607 6dad1de |
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 |
from fastapi import APIRouter, HTTPException, BackgroundTasks, Depends
from typing import List, Optional, Dict
from pydantic import BaseModel
from datetime import datetime
from api.dependencies import simulation_manager
from tinytroupe.simulation_manager import SimulationConfig
router = APIRouter()
class CreateSimulationRequest(BaseModel):
name: str
persona_count: int = 10
network_type: str = "scale_free"
class RunSimulationRequest(BaseModel):
content: str
class SimulationResponse(BaseModel):
id: str
name: str
status: str
persona_count: int
created_at: datetime
@router.post("/", response_model=SimulationResponse)
async def create_simulation(request: CreateSimulationRequest):
config = SimulationConfig(
name=request.name,
persona_count=request.persona_count,
network_type=request.network_type
)
sim = simulation_manager.create_simulation(config)
return SimulationResponse(
id=sim.id,
name=sim.config.name,
status=sim.status,
persona_count=len(sim.personas),
created_at=sim.created_at
)
@router.post("/{simulation_id}/run")
async def run_simulation(simulation_id: str, request: RunSimulationRequest):
if simulation_id not in simulation_manager.simulations:
raise HTTPException(status_code=404, detail="Simulation not found")
result = simulation_manager.run_simulation(simulation_id, request.content)
return {
"simulation_id": simulation_id,
"total_reach": result.total_reach,
"engagement_count": len(result.engagements)
}
|