File size: 22,157 Bytes
b190b45 |
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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
#!/usr/bin/env python3
"""
Real AI Models Service - ZERO MOCK DATA
All AI predictions use REAL models from HuggingFace
"""
import logging
from typing import Dict, Any, Optional
from datetime import datetime
import asyncio
import time
import hashlib
logger = logging.getLogger(__name__)
# Try to import transformers - if not available, use HF API
try:
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
TRANSFORMERS_AVAILABLE = True
except ImportError:
TRANSFORMERS_AVAILABLE = False
logger.warning("β Transformers not available, will use HF API")
import httpx
from backend.services.real_api_clients import RealAPIConfiguration
class RealAIModelsRegistry:
"""
Real AI Models Registry using HuggingFace models
NO MOCK PREDICTIONS - Only real model inference
"""
def __init__(self):
self.models = {}
self.loaded = False
import os
# Strip whitespace from token to avoid "Illegal header value" errors
token_raw = os.getenv("HF_API_TOKEN") or os.getenv("HF_TOKEN") or RealAPIConfiguration.HF_API_TOKEN or ""
token = str(token_raw).strip() if token_raw else ""
self.hf_api_token = token if token else None
self.hf_api_url = "https://router.huggingface.co/models"
# Simple in-memory cache to reduce repeated HF Inference calls
# key -> {"time": float, "data": Any}
self._cache: Dict[str, Dict[str, Any]] = {}
# Model configurations - REAL HuggingFace models with fallback chain
# Each task has at least 3 fallback models
self.model_configs = {
"sentiment_crypto": {
"model_id": "ElKulako/cryptobert",
"task": "sentiment-analysis",
"description": "CryptoBERT for crypto sentiment analysis",
"fallbacks": [
"kk08/CryptoBERT",
"ProsusAI/finbert",
"cardiffnlp/twitter-roberta-base-sentiment-latest",
"distilbert-base-uncased-finetuned-sst-2-english"
]
},
"sentiment_twitter": {
"model_id": "cardiffnlp/twitter-roberta-base-sentiment-latest",
"task": "sentiment-analysis",
"description": "Twitter sentiment analysis",
"fallbacks": [
"cardiffnlp/twitter-roberta-base-sentiment",
"ProsusAI/finbert",
"distilbert-base-uncased-finetuned-sst-2-english",
"nlptown/bert-base-multilingual-uncased-sentiment"
]
},
"sentiment_financial": {
"model_id": "ProsusAI/finbert",
"task": "sentiment-analysis",
"description": "FinBERT for financial sentiment",
"fallbacks": [
"yiyanghkust/finbert-tone",
"mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis",
"cardiffnlp/twitter-roberta-base-sentiment-latest",
"distilbert-base-uncased-finetuned-sst-2-english"
]
},
"text_generation": {
# Use a widely-available, lightweight text generation model as primary
# to avoid "model not found / gated / gone" failures during deploy.
"model_id": "gpt2",
"task": "text-generation",
"description": "Text generation (lightweight)",
"fallbacks": [
"distilgpt2",
"EleutherAI/gpt-neo-125M"
]
},
"trading_signals": {
# Keep signals reliable; prompt will be crypto-specific.
"model_id": "gpt2",
"task": "text-generation",
"description": "Trading signals (prompted text generation)",
"fallbacks": [
"distilgpt2",
"EleutherAI/gpt-neo-125M"
]
},
"summarization": {
"model_id": "facebook/bart-large-cnn",
"task": "summarization",
"description": "BART for news summarization",
"fallbacks": [
"sshleifer/distilbart-cnn-12-6",
"google/pegasus-xsum",
"facebook/bart-large",
"FurkanGozukara/Crypto-Financial-News-Summarizer",
"facebook/mbart-large-50"
]
}
}
async def load_models(self):
"""
Load REAL models from HuggingFace
"""
if self.loaded:
return {"status": "already_loaded", "models": len(self.models)}
logger.info("π€ Loading REAL AI models from HuggingFace...")
if TRANSFORMERS_AVAILABLE:
# Load models locally using transformers
for model_key, config in self.model_configs.items():
try:
if config["task"] == "sentiment-analysis":
self.models[model_key] = pipeline(
config["task"],
model=config["model_id"],
truncation=True,
max_length=512
)
logger.info(f"β
Loaded local model: {config['model_id']}")
# For text generation, we'll use API to avoid heavy downloads
except Exception as e:
logger.warning(f"β Could not load {model_key} locally: {e}")
self.loaded = True
return {
"status": "loaded",
"models_local": len(self.models),
"models_api": len(self.model_configs) - len(self.models),
"total": len(self.model_configs)
}
async def predict_sentiment(
self,
text: str,
model_key: str = "sentiment_crypto"
) -> Dict[str, Any]:
"""
Run REAL sentiment analysis using HuggingFace models
NO FAKE PREDICTIONS
"""
try:
# Check if model is loaded locally
if model_key in self.models:
# Use local model
result = self.models[model_key](text)[0]
return {
"success": True,
"label": result["label"],
"score": result["score"],
"model": model_key,
"source": "local",
"timestamp": datetime.utcnow().isoformat()
}
else:
# Use HuggingFace API
return await self._predict_via_api(text, model_key)
except Exception as e:
logger.error(f"β Sentiment prediction failed: {e}")
raise Exception(f"Failed to predict sentiment: {str(e)}")
async def generate_text(
self,
prompt: str,
model_key: str = "text_generation",
max_length: int = 200
) -> Dict[str, Any]:
"""
Generate REAL text using HuggingFace models
NO FAKE GENERATION
"""
try:
return await self._generate_via_api(prompt, model_key, max_length)
except Exception as e:
logger.error(f"β Text generation failed: {e}")
raise Exception(f"Failed to generate text: {str(e)}")
async def get_trading_signal(
self,
symbol: str,
context: Optional[str] = None
) -> Dict[str, Any]:
"""
Get REAL trading signal using HF text-generation (prompted)
NO FAKE SIGNALS
"""
try:
# Prepare prompt for trading model
prompt = f"Trading signal for {symbol}."
if context:
prompt += f" Context: {context}"
result = await self._generate_via_api(
prompt,
"trading_signals",
max_length=100
)
# Parse trading signal from generated text
generated_text = result.get("generated_text", "").upper()
# Determine signal type
if "BUY" in generated_text or "BULLISH" in generated_text:
signal_type = "BUY"
score = 0.75
elif "SELL" in generated_text or "BEARISH" in generated_text:
signal_type = "SELL"
score = 0.75
else:
signal_type = "HOLD"
score = 0.60
return {
"success": True,
"symbol": symbol,
"signal": signal_type,
"score": score,
"explanation": result.get("generated_text", ""),
"model": "trading_signals",
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
logger.error(f"β Trading signal failed: {e}")
raise Exception(f"Failed to get trading signal: {str(e)}")
async def summarize_news(
self,
text: str
) -> Dict[str, Any]:
"""
Summarize REAL news using BART
NO FAKE SUMMARIES
"""
try:
return await self._summarize_via_api(text)
except Exception as e:
logger.error(f"β News summarization failed: {e}")
raise Exception(f"Failed to summarize news: {str(e)}")
async def _predict_via_api(
self,
text: str,
model_key: str
) -> Dict[str, Any]:
"""
Run REAL inference via HuggingFace API with fallback chain
Tries at least 3 models before failing
"""
config = self.model_configs.get(model_key)
if not config:
raise ValueError(f"Unknown model: {model_key}")
# Build fallback chain: primary model + fallbacks
models_to_try = [config["model_id"]] + config.get("fallbacks", [])
last_error = None
for model_id in models_to_try[:5]: # Try up to 5 models
try:
logger.info(f"π Trying sentiment model: {model_id}")
async with httpx.AsyncClient(timeout=30.0) as client:
_headers = {"Content-Type": "application/json"}
if self.hf_api_token:
_headers["Authorization"] = f"Bearer {self.hf_api_token}"
response = await client.post(
f"{self.hf_api_url}/{model_id}",
headers=_headers,
json={"inputs": text[:512]} # Limit input length
)
response.raise_for_status()
result = response.json()
# Parse result based on task type
if isinstance(result, list) and len(result) > 0:
if isinstance(result[0], list):
result = result[0]
if isinstance(result[0], dict):
top_result = result[0]
label = top_result.get("label", "neutral")
score = top_result.get("score", 0.0)
# Normalize label
label_upper = label.upper()
if "POSITIVE" in label_upper or "LABEL_2" in label_upper:
normalized_label = "positive"
elif "NEGATIVE" in label_upper or "LABEL_0" in label_upper:
normalized_label = "negative"
else:
normalized_label = "neutral"
logger.info(f"β
Sentiment analysis succeeded with {model_id}: {normalized_label} ({score})")
return {
"success": True,
"label": normalized_label,
"score": score,
"confidence": score,
"model": model_id,
"source": "hf_api",
"fallback_used": model_id != config["model_id"],
"timestamp": datetime.utcnow().isoformat()
}
# If we got here, result format is unexpected but not an error
return {
"success": True,
"result": result,
"model": model_id,
"source": "hf_api",
"fallback_used": model_id != config["model_id"],
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
logger.warning(f"β οΈ Sentiment model {model_id} failed: {e}")
last_error = e
continue
logger.error(f"β All sentiment models failed. Last error: {last_error}")
raise Exception(f"Failed to predict sentiment: All models failed. Tried: {models_to_try[:5]}")
async def _generate_via_api(
self,
prompt: str,
model_key: str,
max_length: int = 200
) -> Dict[str, Any]:
"""
Generate REAL text via HuggingFace API with fallback chain
"""
config = self.model_configs.get(model_key)
if not config:
raise ValueError(f"Unknown model: {model_key}")
# Cache key
cache_key_raw = f"gen:{model_key}:{max_length}:{prompt}".encode("utf-8", errors="ignore")
cache_key = hashlib.sha256(cache_key_raw).hexdigest()
cached = self._cache.get(cache_key)
if cached and (time.time() - float(cached.get("time", 0))) < 45:
data = cached.get("data")
if isinstance(data, dict):
return data
models_to_try = [config["model_id"]] + config.get("fallbacks", [])
last_error = None
for model_id in models_to_try[:5]:
try:
logger.info(f"π Trying generation model: {model_id}")
result = await self._post_hf_inference(
model_id=model_id,
payload={
"inputs": prompt[:2000],
"parameters": {
# Some endpoints prefer max_new_tokens; keep both to be safe.
"max_new_tokens": max(16, min(max_length, 256)),
"max_length": max_length,
"temperature": 0.7,
"top_p": 0.9,
"do_sample": True,
"return_full_text": True,
},
},
timeout_seconds=60.0,
)
generated = self._extract_generated_text(result)
if not generated or not generated.strip():
raise ValueError("Empty generation result")
out = {
"success": True,
"generated_text": generated,
"model": model_id,
"source": "hf_api",
"fallback_used": model_id != config["model_id"],
"prompt": prompt,
"timestamp": datetime.utcnow().isoformat(),
}
self._cache[cache_key] = {"time": time.time(), "data": out}
return out
except Exception as e:
logger.warning(f"β οΈ Generation model {model_id} failed: {e}")
last_error = e
continue
raise Exception(f"Failed to generate text: All models failed. Tried: {models_to_try[:5]}. Last error: {last_error}")
async def _post_hf_inference(
self,
model_id: str,
payload: Dict[str, Any],
timeout_seconds: float = 30.0,
) -> Any:
"""
Shared HF inference helper with minimal retry for loading (503) responses.
"""
_headers = {"Content-Type": "application/json"}
if self.hf_api_token:
_headers["Authorization"] = f"Bearer {self.hf_api_token}"
url = f"{self.hf_api_url}/{model_id}"
async with httpx.AsyncClient(timeout=timeout_seconds) as client:
# Try twice: initial + one retry after estimated loading time (if provided)
for attempt in range(2):
response = await client.post(url, headers=_headers, json=payload)
if response.status_code == 503:
try:
body = response.json()
except Exception:
body = {}
estimated = body.get("estimated_time")
if attempt == 0 and isinstance(estimated, (int, float)):
await asyncio.sleep(min(float(estimated), 10.0))
continue
response.raise_for_status()
return response.json()
def _extract_generated_text(self, result: Any) -> str:
"""
Normalize various HF text-generation return formats.
"""
if isinstance(result, list) and result:
item = result[0]
if isinstance(item, dict):
return (
item.get("generated_text")
or item.get("summary_text")
or item.get("text")
or ""
)
if isinstance(item, str):
return item
if isinstance(result, dict):
return (
result.get("generated_text")
or result.get("summary_text")
or result.get("text")
or str(result)
)
return str(result)
async def _summarize_via_api(
self,
text: str
) -> Dict[str, Any]:
"""
Summarize REAL text via HuggingFace API with fallback chain
Tries at least 3 models before failing
"""
config = self.model_configs["summarization"]
models_to_try = [config["model_id"]] + config.get("fallbacks", [])
last_error = None
for model_id in models_to_try[:5]: # Try up to 5 models
try:
logger.info(f"π Trying summarization model: {model_id}")
async with httpx.AsyncClient(timeout=30.0) as client:
_headers = {"Content-Type": "application/json"}
if self.hf_api_token:
_headers["Authorization"] = f"Bearer {self.hf_api_token}"
response = await client.post(
f"{self.hf_api_url}/{model_id}",
headers=_headers,
json={
"inputs": text[:1024], # Limit input length
"parameters": {
"max_length": 130,
"min_length": 30,
"do_sample": False
}
}
)
response.raise_for_status()
result = response.json()
# Parse result
if isinstance(result, list) and len(result) > 0:
summary = result[0].get("summary_text", "")
else:
summary = result.get("summary_text", str(result))
if summary and len(summary.strip()) > 0:
logger.info(f"β
Summarization succeeded with {model_id}")
return {
"success": True,
"summary": summary,
"model": model_id,
"source": "hf_api",
"fallback_used": model_id != config["model_id"],
"timestamp": datetime.utcnow().isoformat()
}
except Exception as e:
logger.warning(f"β οΈ Summarization model {model_id} failed: {e}")
last_error = e
continue
logger.error(f"β All summarization models failed. Last error: {last_error}")
raise Exception(f"Failed to summarize news: All models failed. Tried: {models_to_try[:5]}")
def get_models_list(self) -> Dict[str, Any]:
"""
Get list of available REAL models
"""
models_list = []
for key, config in self.model_configs.items():
models_list.append({
"key": key,
"model_id": config["model_id"],
"task": config["task"],
"description": config["description"],
"loaded_locally": key in self.models,
"available": True
})
return {
"success": True,
"models": models_list,
"total": len(models_list),
"loaded_locally": len(self.models),
"timestamp": datetime.utcnow().isoformat()
}
# Global instance
ai_registry = RealAIModelsRegistry()
# Export
__all__ = ["RealAIModelsRegistry", "ai_registry"]
|