Update hf_demo.py
Browse files- hf_demo.py +16 -18
hf_demo.py
CHANGED
|
@@ -4,7 +4,6 @@ Compatible with Pydantic V2
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
| 7 |
-
# 🔥 CRITICAL: Ensure we use the correct port from environment
|
| 8 |
import sys
|
| 9 |
import json
|
| 10 |
import uuid
|
|
@@ -12,7 +11,6 @@ import hashlib
|
|
| 12 |
import logging
|
| 13 |
import sqlite3
|
| 14 |
import requests
|
| 15 |
-
import fcntl
|
| 16 |
from datetime import datetime
|
| 17 |
from typing import Dict, List, Optional, Any, Tuple
|
| 18 |
from contextlib import contextmanager
|
|
@@ -21,21 +19,11 @@ from enum import Enum
|
|
| 21 |
# FastAPI and Pydantic
|
| 22 |
from fastapi import FastAPI, HTTPException, Depends, status
|
| 23 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 24 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 25 |
from pydantic import BaseModel, Field, field_validator
|
| 26 |
from pydantic_settings import BaseSettings, SettingsConfigDict
|
| 27 |
|
| 28 |
-
# ============== SINGLE INSTANCE LOCK (per port) ==============
|
| 29 |
-
PORT = int(os.environ.get('PORT', 7860))
|
| 30 |
-
LOCK_FILE = f'/tmp/arf_app_{PORT}.lock'
|
| 31 |
-
try:
|
| 32 |
-
lock_fd = open(LOCK_FILE, 'w')
|
| 33 |
-
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
| 34 |
-
except (IOError, OSError):
|
| 35 |
-
print(f"Another instance is already running on port {PORT}. Exiting.")
|
| 36 |
-
sys.exit(1)
|
| 37 |
-
# ==============================================================
|
| 38 |
-
|
| 39 |
# ============== CONFIGURATION (Pydantic V2) ==============
|
| 40 |
class Settings(BaseSettings):
|
| 41 |
"""Centralized configuration using Pydantic Settings V2"""
|
|
@@ -50,7 +38,7 @@ class Settings(BaseSettings):
|
|
| 50 |
alias='DATA_DIR'
|
| 51 |
)
|
| 52 |
|
| 53 |
-
# Lead generation
|
| 54 |
lead_email: str = "petter2025us@outlook.com"
|
| 55 |
calendly_url: str = "https://calendly.com/petter2025us/arf-demo"
|
| 56 |
|
|
@@ -113,9 +101,8 @@ class LeadSignal(str, Enum):
|
|
| 113 |
CONFIDENCE_LOW = "confidence_low"
|
| 114 |
REPEATED_FAILURE = "repeated_failure"
|
| 115 |
|
| 116 |
-
# ============== BAYESIAN ENGINE
|
| 117 |
class BayesianRiskEngine:
|
| 118 |
-
# ... (keep the full class as before) ...
|
| 119 |
def __init__(self):
|
| 120 |
self.prior_alpha = 2.0
|
| 121 |
self.prior_beta = 5.0
|
|
@@ -269,7 +256,7 @@ class BayesianRiskEngine:
|
|
| 269 |
except sqlite3.Error as e:
|
| 270 |
logger.error(f"Failed to record outcome: {e}")
|
| 271 |
|
| 272 |
-
# ============== POLICY ENGINE
|
| 273 |
class PolicyEngine:
|
| 274 |
def __init__(self):
|
| 275 |
self.config = {
|
|
@@ -381,7 +368,7 @@ class PolicyEngine:
|
|
| 381 |
return True
|
| 382 |
return False
|
| 383 |
|
| 384 |
-
# ============== RAG MEMORY
|
| 385 |
class RAGMemory:
|
| 386 |
def __init__(self):
|
| 387 |
self.db_path = f"{settings.data_dir}/memory.db"
|
|
@@ -666,6 +653,17 @@ risk_engine = BayesianRiskEngine()
|
|
| 666 |
policy_engine = PolicyEngine()
|
| 667 |
memory = RAGMemory()
|
| 668 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
# ============== API ENDPOINTS ==============
|
| 670 |
|
| 671 |
@app.get("/health")
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 7 |
import sys
|
| 8 |
import json
|
| 9 |
import uuid
|
|
|
|
| 11 |
import logging
|
| 12 |
import sqlite3
|
| 13 |
import requests
|
|
|
|
| 14 |
from datetime import datetime
|
| 15 |
from typing import Dict, List, Optional, Any, Tuple
|
| 16 |
from contextlib import contextmanager
|
|
|
|
| 19 |
# FastAPI and Pydantic
|
| 20 |
from fastapi import FastAPI, HTTPException, Depends, status
|
| 21 |
from fastapi.middleware.cors import CORSMiddleware
|
| 22 |
+
from fastapi.responses import JSONResponse
|
| 23 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 24 |
from pydantic import BaseModel, Field, field_validator
|
| 25 |
from pydantic_settings import BaseSettings, SettingsConfigDict
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# ============== CONFIGURATION (Pydantic V2) ==============
|
| 28 |
class Settings(BaseSettings):
|
| 29 |
"""Centralized configuration using Pydantic Settings V2"""
|
|
|
|
| 38 |
alias='DATA_DIR'
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# Lead generation
|
| 42 |
lead_email: str = "petter2025us@outlook.com"
|
| 43 |
calendly_url: str = "https://calendly.com/petter2025us/arf-demo"
|
| 44 |
|
|
|
|
| 101 |
CONFIDENCE_LOW = "confidence_low"
|
| 102 |
REPEATED_FAILURE = "repeated_failure"
|
| 103 |
|
| 104 |
+
# ============== BAYESIAN ENGINE ==============
|
| 105 |
class BayesianRiskEngine:
|
|
|
|
| 106 |
def __init__(self):
|
| 107 |
self.prior_alpha = 2.0
|
| 108 |
self.prior_beta = 5.0
|
|
|
|
| 256 |
except sqlite3.Error as e:
|
| 257 |
logger.error(f"Failed to record outcome: {e}")
|
| 258 |
|
| 259 |
+
# ============== POLICY ENGINE ==============
|
| 260 |
class PolicyEngine:
|
| 261 |
def __init__(self):
|
| 262 |
self.config = {
|
|
|
|
| 368 |
return True
|
| 369 |
return False
|
| 370 |
|
| 371 |
+
# ============== RAG MEMORY ==============
|
| 372 |
class RAGMemory:
|
| 373 |
def __init__(self):
|
| 374 |
self.db_path = f"{settings.data_dir}/memory.db"
|
|
|
|
| 653 |
policy_engine = PolicyEngine()
|
| 654 |
memory = RAGMemory()
|
| 655 |
|
| 656 |
+
# ============== ROOT ENDPOINT (for health checks) ==============
|
| 657 |
+
@app.get("/")
|
| 658 |
+
async def root():
|
| 659 |
+
"""Root endpoint for platform health checks"""
|
| 660 |
+
return {
|
| 661 |
+
"service": "ARF OSS API",
|
| 662 |
+
"version": "3.3.9",
|
| 663 |
+
"status": "operational",
|
| 664 |
+
"docs": "/docs"
|
| 665 |
+
}
|
| 666 |
+
|
| 667 |
# ============== API ENDPOINTS ==============
|
| 668 |
|
| 669 |
@app.get("/health")
|