Spaces:
Running
Running
File size: 15,010 Bytes
7b61a48 |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
"""
CyberForge AI - Hugging Face API Client
Backend integration for fetching models and running inference from Hugging Face Spaces
"""
import os
import json
import logging
import asyncio
from typing import Dict, List, Any, Optional
from datetime import datetime
import httpx
from pathlib import Path
try:
from gradio_client import Client
GRADIO_CLIENT_AVAILABLE = True
except ImportError:
GRADIO_CLIENT_AVAILABLE = False
try:
from huggingface_hub import HfApi, hf_hub_download, InferenceClient
HF_HUB_AVAILABLE = True
except ImportError:
HF_HUB_AVAILABLE = False
logger = logging.getLogger(__name__)
class HuggingFaceClient:
"""
Client for interacting with CyberForge AI Hugging Face Space
Provides model inference, training requests, and model management
"""
def __init__(
self,
space_id: str = "Che237/cyberforge",
hf_token: Optional[str] = None,
models_repo: Optional[str] = None
):
self.space_id = space_id
self.hf_token = hf_token or os.getenv("HF_TOKEN")
self.models_repo = models_repo or f"{space_id.split('/')[0]}/cyberforge-models"
self.space_url = f"https://{space_id.replace('/', '-')}.hf.space"
self._client = None
self._hf_api = None
self._inference_client = None
# Local model cache
self.models_cache_dir = Path("./models_cache")
self.models_cache_dir.mkdir(exist_ok=True)
# Initialize clients
self._init_clients()
def _init_clients(self):
"""Initialize Hugging Face and Gradio clients"""
try:
if GRADIO_CLIENT_AVAILABLE:
self._client = Client(self.space_id, hf_token=self.hf_token)
logger.info(f"✅ Connected to Gradio Space: {self.space_id}")
except Exception as e:
logger.warning(f"Could not connect to Gradio Space: {e}")
try:
if HF_HUB_AVAILABLE:
self._hf_api = HfApi(token=self.hf_token)
logger.info("✅ Connected to Hugging Face Hub API")
except Exception as e:
logger.warning(f"Could not connect to HF Hub API: {e}")
# =========================================================================
# INFERENCE METHODS
# =========================================================================
async def predict(
self,
model_id: str,
features: Dict[str, Any],
timeout: float = 30.0
) -> Dict[str, Any]:
"""
Run inference on a model deployed in the Space
Args:
model_id: ID of the trained model
features: Dictionary of feature values
timeout: Request timeout in seconds
Returns:
Prediction result with confidence scores
"""
try:
if self._client:
# Use Gradio client
result = self._client.predict(
model_id,
json.dumps([features]),
api_name="/run_inference"
)
return json.loads(result)
else:
# Fall back to HTTP API
return await self._http_predict(model_id, features, timeout)
except Exception as e:
logger.error(f"Prediction failed: {e}")
return {"error": str(e), "model_id": model_id}
async def batch_predict(
self,
model_id: str,
batch_features: List[Dict[str, Any]],
timeout: float = 60.0
) -> List[Dict[str, Any]]:
"""
Run batch inference on multiple samples
Args:
model_id: ID of the trained model
batch_features: List of feature dictionaries
timeout: Request timeout in seconds
Returns:
List of prediction results
"""
try:
if self._client:
result = self._client.predict(
model_id,
json.dumps(batch_features),
api_name="/run_inference"
)
return json.loads(result)
else:
return await self._http_batch_predict(model_id, batch_features, timeout)
except Exception as e:
logger.error(f"Batch prediction failed: {e}")
return [{"error": str(e)} for _ in batch_features]
async def _http_predict(
self,
model_id: str,
features: Dict[str, Any],
timeout: float
) -> Dict[str, Any]:
"""HTTP fallback for predictions"""
async with httpx.AsyncClient(timeout=timeout) as client:
response = await client.post(
f"{self.space_url}/api/predict",
json={
"data": [model_id, json.dumps([features])],
"fn_index": 1 # Index of run_inference function
}
)
response.raise_for_status()
result = response.json()
return json.loads(result.get("data", [{}])[0])
async def _http_batch_predict(
self,
model_id: str,
batch_features: List[Dict[str, Any]],
timeout: float
) -> List[Dict[str, Any]]:
"""HTTP fallback for batch predictions"""
async with httpx.AsyncClient(timeout=timeout) as client:
response = await client.post(
f"{self.space_url}/api/predict",
json={
"data": [model_id, json.dumps(batch_features)],
"fn_index": 1
}
)
response.raise_for_status()
result = response.json()
return json.loads(result.get("data", [{}])[0])
# =========================================================================
# MODEL MANAGEMENT
# =========================================================================
async def list_models(self) -> List[Dict[str, Any]]:
"""Get list of available trained models"""
try:
if self._client:
result = self._client.predict(api_name="/list_trained_models")
return self._parse_models_list(result)
else:
return await self._http_list_models()
except Exception as e:
logger.error(f"Failed to list models: {e}")
return []
def _parse_models_list(self, markdown_result: str) -> List[Dict[str, Any]]:
"""Parse markdown model list into structured data"""
models = []
current_model = {}
for line in markdown_result.split('\n'):
if line.startswith('### '):
if current_model:
models.append(current_model)
current_model = {"id": line.replace('### ', '').strip()}
elif '**Created:**' in line:
current_model["created_at"] = line.split('**Created:**')[1].strip()
elif '**Accuracy:**' in line:
try:
current_model["accuracy"] = float(line.split('**Accuracy:**')[1].strip())
except:
pass
elif '**F1 Score:**' in line:
try:
current_model["f1_score"] = float(line.split('**F1 Score:**')[1].strip())
except:
pass
elif '**Status:**' in line:
current_model["status"] = line.split('**Status:**')[1].strip()
if current_model:
models.append(current_model)
return models
async def _http_list_models(self) -> List[Dict[str, Any]]:
"""HTTP fallback for listing models"""
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
f"{self.space_url}/api/predict",
json={"fn_index": 2} # Index of list_trained_models
)
response.raise_for_status()
result = response.json()
return self._parse_models_list(result.get("data", [""])[0])
async def download_model(
self,
model_id: str,
local_path: Optional[str] = None
) -> str:
"""
Download a trained model from Hugging Face Hub
Args:
model_id: Model identifier
local_path: Optional local path to save model
Returns:
Path to downloaded model
"""
try:
if not HF_HUB_AVAILABLE:
raise ImportError("huggingface_hub not installed")
model_filename = f"{model_id}_model.pkl"
scaler_filename = f"{model_id}_scaler.pkl"
model_path = hf_hub_download(
repo_id=self.models_repo,
filename=model_filename,
token=self.hf_token,
cache_dir=str(self.models_cache_dir)
)
try:
scaler_path = hf_hub_download(
repo_id=self.models_repo,
filename=scaler_filename,
token=self.hf_token,
cache_dir=str(self.models_cache_dir)
)
except:
scaler_path = None
logger.info(f"✅ Downloaded model: {model_id}")
return model_path
except Exception as e:
logger.error(f"Failed to download model: {e}")
raise
# =========================================================================
# TRAINING REQUESTS
# =========================================================================
async def request_training(
self,
dataset_url: str,
task_type: str,
model_type: str,
target_column: str,
model_name: str,
test_size: float = 0.2,
callback_url: Optional[str] = None
) -> Dict[str, Any]:
"""
Request model training on the Space
Args:
dataset_url: URL to download dataset
task_type: Type of security task
model_type: ML model type
target_column: Target column name
model_name: Name for trained model
test_size: Test split ratio
callback_url: Optional webhook for training completion
Returns:
Training job status
"""
try:
# Note: This would need custom implementation in the Space
# to support remote dataset URLs and callbacks
logger.info(f"Requesting training for {model_name}")
return {
"status": "submitted",
"model_name": model_name,
"task_type": task_type,
"message": "Training request submitted. Check Space for status."
}
except Exception as e:
logger.error(f"Training request failed: {e}")
return {"error": str(e)}
# =========================================================================
# HEALTH & STATUS
# =========================================================================
async def health_check(self) -> Dict[str, Any]:
"""Check if the Space is healthy and responsive"""
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(f"{self.space_url}")
return {
"status": "healthy" if response.status_code == 200 else "unhealthy",
"space_id": self.space_id,
"url": self.space_url,
"response_code": response.status_code,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {
"status": "error",
"error": str(e),
"space_id": self.space_id,
"timestamp": datetime.now().isoformat()
}
async def get_space_info(self) -> Dict[str, Any]:
"""Get information about the Space"""
try:
if HF_HUB_AVAILABLE and self._hf_api:
info = self._hf_api.space_info(self.space_id)
return {
"id": info.id,
"author": info.author,
"sdk": info.sdk,
"status": info.runtime.stage if info.runtime else "unknown",
"hardware": info.runtime.hardware if info.runtime else "unknown",
}
return {"space_id": self.space_id}
except Exception as e:
return {"error": str(e)}
# ============================================================================
# CONVENIENCE FUNCTIONS FOR BACKEND
# ============================================================================
# Global client instance
_hf_client: Optional[HuggingFaceClient] = None
def get_hf_client() -> HuggingFaceClient:
"""Get or create the global HF client"""
global _hf_client
if _hf_client is None:
_hf_client = HuggingFaceClient()
return _hf_client
async def predict_threat(model_id: str, features: Dict[str, Any]) -> Dict[str, Any]:
"""Convenience function for threat prediction"""
client = get_hf_client()
return await client.predict(model_id, features)
async def batch_predict_threats(
model_id: str,
batch_features: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
"""Convenience function for batch threat prediction"""
client = get_hf_client()
return await client.batch_predict(model_id, batch_features)
async def get_available_models() -> List[Dict[str, Any]]:
"""Get list of available models"""
client = get_hf_client()
return await client.list_models()
# ============================================================================
# EXAMPLE USAGE
# ============================================================================
if __name__ == "__main__":
async def main():
# Initialize client
client = HuggingFaceClient(
space_id="Che237/cyberforge",
hf_token=os.getenv("HF_TOKEN")
)
# Health check
health = await client.health_check()
print(f"Health: {health}")
# List models
models = await client.list_models()
print(f"Available models: {models}")
# Example prediction
if models:
model_id = models[0]["id"]
features = {"feature1": 0.5, "feature2": 1.2, "feature3": 0.8}
result = await client.predict(model_id, features)
print(f"Prediction: {result}")
asyncio.run(main())
|