| """ |
| ARF OSS v3.3.9 - Enterprise Lead Generation Engine (API Only) |
| Compatible with Pydantic V2 |
| """ |
|
|
| import os |
| import sys |
| import json |
| import uuid |
| import hashlib |
| import logging |
| import sqlite3 |
| import requests |
| import fcntl |
| from datetime import datetime |
| from typing import Dict, List, Optional, Any, Tuple |
| from contextlib import contextmanager |
| from enum import Enum |
|
|
| |
| from fastapi import FastAPI, HTTPException, Depends, status |
| from fastapi.middleware.cors import CORSMiddleware |
| from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials |
| from pydantic import BaseModel, Field, field_validator |
| from pydantic_settings import BaseSettings, SettingsConfigDict |
|
|
| |
| from infrastructure import ( |
| AzureInfrastructureSimulator, |
| RegionAllowedPolicy, |
| CostThresholdPolicy, |
| ProvisionResourceIntent, |
| DeployConfigurationIntent, |
| GrantAccessIntent, |
| ResourceType, |
| Environment, |
| RecommendedAction, |
| ) |
| import yaml |
|
|
| |
| PORT = int(os.environ.get('PORT', 7860)) |
| LOCK_FILE = f'/tmp/arf_app_{PORT}.lock' |
| try: |
| lock_fd = open(LOCK_FILE, 'w') |
| fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| except (IOError, OSError): |
| print(f"Another instance is already running on port {PORT}. Exiting.") |
| sys.exit(1) |
| |
|
|
| |
| class Settings(BaseSettings): |
| """Centralized configuration using Pydantic Settings V2""" |
| |
| |
| hf_space_id: str = Field(default='local', alias='SPACE_ID') |
| hf_token: str = Field(default='', alias='HF_TOKEN') |
| |
| |
| data_dir: str = Field( |
| default='/data' if os.path.exists('/data') else './data', |
| alias='DATA_DIR' |
| ) |
| |
| |
| lead_email: str = "petter2025us@outlook.com" |
| calendly_url: str = "https://calendly.com/petter2025us/arf-demo" |
| |
| |
| slack_webhook: str = Field(default='', alias='SLACK_WEBHOOK') |
| sendgrid_api_key: str = Field(default='', alias='SENDGRID_API_KEY') |
| |
| |
| api_key: str = Field( |
| default_factory=lambda: str(uuid.uuid4()), |
| alias='ARF_API_KEY' |
| ) |
| |
| |
| default_confidence_threshold: float = 0.9 |
| default_max_risk: str = "MEDIUM" |
| |
| |
| model_config = SettingsConfigDict( |
| populate_by_name=True, |
| extra='ignore', |
| env_prefix='', |
| case_sensitive=False |
| ) |
|
|
| def __init__(self, **kwargs): |
| super().__init__(**kwargs) |
| os.makedirs(self.data_dir, exist_ok=True) |
|
|
| settings = Settings() |
|
|
| |
| logging.basicConfig( |
| level=logging.INFO, |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| handlers=[ |
| logging.FileHandler(f'{settings.data_dir}/arf.log'), |
| logging.StreamHandler() |
| ] |
| ) |
| logger = logging.getLogger('arf.oss') |
|
|
| |
| class RiskLevel(str, Enum): |
| LOW = "LOW" |
| MEDIUM = "MEDIUM" |
| HIGH = "HIGH" |
| CRITICAL = "CRITICAL" |
|
|
| class ExecutionLevel(str, Enum): |
| AUTONOMOUS_LOW = "AUTONOMOUS_LOW" |
| AUTONOMOUS_HIGH = "AUTONOMOUS_HIGH" |
| SUPERVISED = "SUPERVISED" |
| OPERATOR_REVIEW = "OPERATOR_REVIEW" |
|
|
| class LeadSignal(str, Enum): |
| HIGH_RISK_BLOCKED = "high_risk_blocked" |
| NOVEL_ACTION = "novel_action" |
| POLICY_VIOLATION = "policy_violation" |
| CONFIDENCE_LOW = "confidence_low" |
| REPEATED_FAILURE = "repeated_failure" |
|
|
| |
| |
| |
| |
| |
|
|
| |
| security = HTTPBearer() |
|
|
| async def verify_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)): |
| if credentials.credentials != settings.api_key: |
| raise HTTPException( |
| status_code=status.HTTP_403_FORBIDDEN, |
| detail="Invalid API key" |
| ) |
| return credentials.credentials |
|
|
| |
| |
|
|
| |
| class InfrastructureIntentRequest(BaseModel): |
| intent_type: str |
| resource_type: Optional[str] = None |
| region: Optional[str] = None |
| size: Optional[str] = None |
| environment: str = "PROD" |
| requester: str |
| |
| config_content: Optional[Dict[str, Any]] = None |
| |
| permission: Optional[str] = None |
| target: Optional[str] = None |
|
|
| class InfrastructureEvaluationResponse(BaseModel): |
| recommended_action: str |
| justification: str |
| policy_violations: List[str] |
| estimated_cost: Optional[float] |
| risk_score: float |
| confidence_score: float |
| evaluation_details: Dict[str, Any] |
|
|
| |
| app = FastAPI( |
| title="ARF OSS Real Engine (API Only)", |
| version="3.3.9", |
| description="Real ARF OSS components for enterprise lead generation β backend API only.", |
| contact={ |
| "name": "ARF Sales", |
| "email": settings.lead_email, |
| } |
| ) |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| |
| |
|
|
| |
| |
| _default_policy = RegionAllowedPolicy(regions={"eastus", "westeurope"}) & CostThresholdPolicy(500.0) |
| infra_simulator = AzureInfrastructureSimulator( |
| policy=_default_policy, |
| pricing_file="pricing.yml" if os.path.exists("pricing.yml") else None |
| ) |
|
|
| |
|
|
| @app.get("/") |
| async def root(): |
| return {"service": "ARF OSS API", "version": "3.3.9", "status": "operational", "docs": "/docs"} |
|
|
| @app.get("/health") |
| async def health_check(): |
| return { |
| "status": "healthy", |
| "version": "3.3.9", |
| "edition": "OSS", |
| "memory_entries": 0, |
| "timestamp": datetime.utcnow().isoformat() |
| } |
|
|
| |
|
|
| |
| @app.post("/api/v1/infrastructure/evaluate", dependencies=[Depends(verify_api_key)], response_model=InfrastructureEvaluationResponse) |
| async def evaluate_infrastructure_intent(request: InfrastructureIntentRequest): |
| """ |
| Evaluate an infrastructure change intent against policies, cost, and risk. |
| """ |
| try: |
| |
| if request.intent_type == "provision": |
| if not all([request.resource_type, request.region, request.size]): |
| raise HTTPException(400, "Missing fields for provision intent") |
| intent = ProvisionResourceIntent( |
| resource_type=ResourceType(request.resource_type.lower()), |
| region=request.region, |
| size=request.size, |
| requester=request.requester, |
| environment=Environment(request.environment.lower()) |
| ) |
| elif request.intent_type == "deploy": |
| intent = DeployConfigurationIntent( |
| service_name=request.resource_type or "unknown", |
| change_scope="canary", |
| deployment_target=Environment(request.environment.lower()), |
| configuration=request.config_content or {}, |
| requester=request.requester |
| ) |
| elif request.intent_type == "grant": |
| intent = GrantAccessIntent( |
| principal=request.requester, |
| permission_level=request.permission or "read", |
| resource_scope=request.target or "/", |
| justification="Requested via API" |
| ) |
| else: |
| raise HTTPException(400, f"Unknown intent type: {request.intent_type}") |
|
|
| |
| healing_intent = infra_simulator.evaluate(intent) |
|
|
| |
| return InfrastructureEvaluationResponse( |
| recommended_action=healing_intent.recommended_action.value, |
| justification=healing_intent.justification, |
| policy_violations=healing_intent.policy_violations, |
| estimated_cost=healing_intent.cost_projection, |
| risk_score=healing_intent.risk_score or 0.0, |
| confidence_score=healing_intent.confidence_score, |
| evaluation_details=healing_intent.evaluation_details |
| ) |
| except HTTPException: |
| raise |
| except Exception as e: |
| logger.error(f"Infrastructure evaluation failed: {e}", exc_info=True) |
| raise HTTPException(500, detail=str(e)) |
|
|
| |
| if __name__ == "__main__": |
| import uvicorn |
|
|
| port = int(os.environ.get('PORT', 7860)) |
|
|
| logger.info("="*60) |
| logger.info("π ARF OSS v3.3.9 (API Only) Starting") |
| logger.info(f"π Data directory: {settings.data_dir}") |
| logger.info(f"π§ Lead email: {settings.lead_email}") |
| logger.info(f"π API Key: {settings.api_key[:8]}... (set in HF secrets)") |
| logger.info(f"π Serving API at: http://0.0.0.0:{port}") |
| logger.info("="*60) |
|
|
| uvicorn.run( |
| "hf_demo:app", |
| host="0.0.0.0", |
| port=port, |
| log_level="info", |
| reload=False |
| ) |