Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ for module_name, pip_name in REQUIRED_PACKAGES.items():
|
|
| 20 |
print(f"✅ {pip_name} установлен!")
|
| 21 |
|
| 22 |
# ============================================
|
| 23 |
-
# 👑 TOMIRIS SPACE 24 v2.
|
| 24 |
# ============================================
|
| 25 |
import os, time, json, logging, asyncio
|
| 26 |
from typing import Dict, Any, List, Optional
|
|
@@ -36,7 +36,6 @@ logger = logging.getLogger("Space24_EcosystemAggregator")
|
|
| 36 |
# ================= КОНФИГУРАЦИЯ =================
|
| 37 |
SYMBOLS = ["XAU/USD", "ETH/USD", "SOL/USD"]
|
| 38 |
|
| 39 |
-
# Реальные URL'ы экосистемных Space'ов
|
| 40 |
ECOSYSTEM_SPACES = {
|
| 41 |
"space_19_sol": {
|
| 42 |
"url": "https://tomirisai80-tomirisanal.hf.space",
|
|
@@ -76,7 +75,6 @@ ECOSYSTEM_SPACES = {
|
|
| 76 |
}
|
| 77 |
|
| 78 |
HUB_URL = os.getenv("SPACE17_URL", "https://tomiris-ai-name5-5.hf.space")
|
| 79 |
-
ARBITER_URL = os.getenv("SPACE18_URL", "https://tomiris-ai-name6-6.hf.space")
|
| 80 |
SPACE31_URL = os.getenv("SPACE31_URL", "https://nuxotetotmailsvoboden-tomiris-perf.hf.space")
|
| 81 |
|
| 82 |
# ================= HTTP КЛИЕНТ =================
|
|
@@ -107,8 +105,6 @@ def breaker_record(name: str, success: bool):
|
|
| 107 |
|
| 108 |
# ================= ДИНАМИЧЕСКИЕ ВЕСА =================
|
| 109 |
async def fetch_dynamic_weights() -> Dict[str, float]:
|
| 110 |
-
"""Пытается получить веса из Space 31, затем из Space 17. Если нет — базовые."""
|
| 111 |
-
# Space 31
|
| 112 |
if SPACE31_URL:
|
| 113 |
try:
|
| 114 |
r = await http_client.get(f"{SPACE31_URL}/weights", timeout=8)
|
|
@@ -118,7 +114,6 @@ async def fetch_dynamic_weights() -> Dict[str, float]:
|
|
| 118 |
return {k: v.get("weight", 0.1) for k, v in data.items() if k in ECOSYSTEM_SPACES}
|
| 119 |
except:
|
| 120 |
pass
|
| 121 |
-
# Space 17 (analyst_metrics)
|
| 122 |
try:
|
| 123 |
r = await http_client.get(f"{HUB_URL}/metrics", timeout=8)
|
| 124 |
if r.status_code == 200:
|
|
@@ -133,7 +128,6 @@ async def fetch_dynamic_weights() -> Dict[str, float]:
|
|
| 133 |
return weights
|
| 134 |
except:
|
| 135 |
pass
|
| 136 |
-
# Базовые
|
| 137 |
return {name: cfg["weight"] for name, cfg in ECOSYSTEM_SPACES.items()}
|
| 138 |
|
| 139 |
# ================= ОПРОС ОДНОГО SPACE =================
|
|
@@ -216,7 +210,6 @@ async def aggregate_ecosystem(symbol: str) -> Dict[str, Any]:
|
|
| 216 |
}
|
| 217 |
|
| 218 |
if total_weight == 0:
|
| 219 |
-
# Ни одного активного Space
|
| 220 |
return {
|
| 221 |
"ecosystem_score": 50.0,
|
| 222 |
"direction": "WAIT",
|
|
@@ -227,12 +220,10 @@ async def aggregate_ecosystem(symbol: str) -> Dict[str, Any]:
|
|
| 227 |
"votes": {"LONG": 0.0, "SHORT": 0.0, "WAIT": 0.0}
|
| 228 |
}
|
| 229 |
|
| 230 |
-
# Баланс голосов
|
| 231 |
bias = (long_votes - short_votes) / total_weight
|
| 232 |
ecosystem_score = 50.0 + bias * 50.0
|
| 233 |
ecosystem_score = max(0, min(100, ecosystem_score))
|
| 234 |
|
| 235 |
-
# Определение направления с учётом WAIT
|
| 236 |
wait_ratio = wait_votes / total_weight
|
| 237 |
if wait_ratio > 0.6:
|
| 238 |
direction = "WAIT"
|
|
@@ -247,7 +238,6 @@ async def aggregate_ecosystem(symbol: str) -> Dict[str, Any]:
|
|
| 247 |
direction = "WAIT"
|
| 248 |
confidence = max(long_votes, short_votes) / total_weight
|
| 249 |
|
| 250 |
-
# Снижаем confidence при малом числе активных Space
|
| 251 |
active_ratio = len(signals) / len(space_names) if space_names else 0
|
| 252 |
confidence *= 0.5 + 0.5 * active_ratio
|
| 253 |
|
|
@@ -265,12 +255,29 @@ async def aggregate_ecosystem(symbol: str) -> Dict[str, Any]:
|
|
| 265 |
}
|
| 266 |
}
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
# ================= ГЛАВНЫЙ СИГНАЛ =================
|
| 269 |
async def get_ecosystem_aggregate(symbol: str = "XAU/USD") -> Dict[str, Any]:
|
| 270 |
start = time.time()
|
| 271 |
agg = await aggregate_ecosystem(symbol)
|
| 272 |
latency = int((time.time() - start) * 1000)
|
| 273 |
|
|
|
|
|
|
|
|
|
|
| 274 |
result = {
|
| 275 |
"space": "space_24_ecosystem_aggregator",
|
| 276 |
"timestamp": int(time.time()),
|
|
@@ -289,22 +296,11 @@ async def get_ecosystem_aggregate(symbol: str = "XAU/USD") -> Dict[str, Any]:
|
|
| 289 |
"latency_ms": latency
|
| 290 |
}
|
| 291 |
|
| 292 |
-
# Отправка в Arbiter
|
| 293 |
-
try:
|
| 294 |
-
await http_client.post(f"{ARBITER_URL}/log_signal", json={
|
| 295 |
-
"space": "space_24_ecosystem",
|
| 296 |
-
"symbol": symbol,
|
| 297 |
-
"signal": result["signal"],
|
| 298 |
-
"ecosystem_details": result["ecosystem_analysis"]
|
| 299 |
-
})
|
| 300 |
-
except:
|
| 301 |
-
pass
|
| 302 |
-
|
| 303 |
logger.info(f"🌐 Ecosystem Agg: {symbol} {agg['direction']} conf={agg['confidence']:.3f} score={agg['ecosystem_score']:.1f} active={agg['active_spaces']}")
|
| 304 |
return result
|
| 305 |
|
| 306 |
# ================= FASTAPI =================
|
| 307 |
-
app = FastAPI(title="Tomiris Space 24 v2.
|
| 308 |
|
| 309 |
@app.on_event("startup")
|
| 310 |
async def startup():
|
|
@@ -318,11 +314,10 @@ async def shutdown():
|
|
| 318 |
async def health():
|
| 319 |
return {
|
| 320 |
"status": "operational",
|
| 321 |
-
"version": "2.
|
| 322 |
"symbols": SYMBOLS,
|
| 323 |
"ecosystem_spaces": list(ECOSYSTEM_SPACES.keys()),
|
| 324 |
-
"
|
| 325 |
-
"async": True
|
| 326 |
}
|
| 327 |
|
| 328 |
@app.get("/consilium")
|
|
@@ -347,4 +342,4 @@ if __name__ == "__main__":
|
|
| 347 |
import uvicorn
|
| 348 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 349 |
|
| 350 |
-
print("🚀 SPACE 24 v2.
|
|
|
|
| 20 |
print(f"✅ {pip_name} установлен!")
|
| 21 |
|
| 22 |
# ============================================
|
| 23 |
+
# 👑 TOMIRIS SPACE 24 v2.1 — ECOSYSTEM AGGREGATOR PRO (Hub-Connected)
|
| 24 |
# ============================================
|
| 25 |
import os, time, json, logging, asyncio
|
| 26 |
from typing import Dict, Any, List, Optional
|
|
|
|
| 36 |
# ================= КОНФИГУРАЦИЯ =================
|
| 37 |
SYMBOLS = ["XAU/USD", "ETH/USD", "SOL/USD"]
|
| 38 |
|
|
|
|
| 39 |
ECOSYSTEM_SPACES = {
|
| 40 |
"space_19_sol": {
|
| 41 |
"url": "https://tomirisai80-tomirisanal.hf.space",
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
HUB_URL = os.getenv("SPACE17_URL", "https://tomiris-ai-name5-5.hf.space")
|
|
|
|
| 78 |
SPACE31_URL = os.getenv("SPACE31_URL", "https://nuxotetotmailsvoboden-tomiris-perf.hf.space")
|
| 79 |
|
| 80 |
# ================= HTTP КЛИЕНТ =================
|
|
|
|
| 105 |
|
| 106 |
# ================= ДИНАМИЧЕСКИЕ ВЕСА =================
|
| 107 |
async def fetch_dynamic_weights() -> Dict[str, float]:
|
|
|
|
|
|
|
| 108 |
if SPACE31_URL:
|
| 109 |
try:
|
| 110 |
r = await http_client.get(f"{SPACE31_URL}/weights", timeout=8)
|
|
|
|
| 114 |
return {k: v.get("weight", 0.1) for k, v in data.items() if k in ECOSYSTEM_SPACES}
|
| 115 |
except:
|
| 116 |
pass
|
|
|
|
| 117 |
try:
|
| 118 |
r = await http_client.get(f"{HUB_URL}/metrics", timeout=8)
|
| 119 |
if r.status_code == 200:
|
|
|
|
| 128 |
return weights
|
| 129 |
except:
|
| 130 |
pass
|
|
|
|
| 131 |
return {name: cfg["weight"] for name, cfg in ECOSYSTEM_SPACES.items()}
|
| 132 |
|
| 133 |
# ================= ОПРОС ОДНОГО SPACE =================
|
|
|
|
| 210 |
}
|
| 211 |
|
| 212 |
if total_weight == 0:
|
|
|
|
| 213 |
return {
|
| 214 |
"ecosystem_score": 50.0,
|
| 215 |
"direction": "WAIT",
|
|
|
|
| 220 |
"votes": {"LONG": 0.0, "SHORT": 0.0, "WAIT": 0.0}
|
| 221 |
}
|
| 222 |
|
|
|
|
| 223 |
bias = (long_votes - short_votes) / total_weight
|
| 224 |
ecosystem_score = 50.0 + bias * 50.0
|
| 225 |
ecosystem_score = max(0, min(100, ecosystem_score))
|
| 226 |
|
|
|
|
| 227 |
wait_ratio = wait_votes / total_weight
|
| 228 |
if wait_ratio > 0.6:
|
| 229 |
direction = "WAIT"
|
|
|
|
| 238 |
direction = "WAIT"
|
| 239 |
confidence = max(long_votes, short_votes) / total_weight
|
| 240 |
|
|
|
|
| 241 |
active_ratio = len(signals) / len(space_names) if space_names else 0
|
| 242 |
confidence *= 0.5 + 0.5 * active_ratio
|
| 243 |
|
|
|
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
+
# ================= ОТПРАВКА В HUB =================
|
| 259 |
+
async def send_signal_to_hub(symbol: str, direction: str, confidence: float):
|
| 260 |
+
try:
|
| 261 |
+
await http_client.post(f"{HUB_URL}/signal", json={
|
| 262 |
+
"space": "space_24_eco_agg",
|
| 263 |
+
"symbol": symbol,
|
| 264 |
+
"direction": direction,
|
| 265 |
+
"confidence": confidence,
|
| 266 |
+
"raw": json.dumps({"source": "space_24_eco_agg"})
|
| 267 |
+
})
|
| 268 |
+
logger.info(f"📤 {symbol}: {direction} conf={confidence:.3f} отправлен в Hub")
|
| 269 |
+
except Exception as e:
|
| 270 |
+
logger.error(f"Ошибка отправки в Hub: {e}")
|
| 271 |
+
|
| 272 |
# ================= ГЛАВНЫЙ СИГНАЛ =================
|
| 273 |
async def get_ecosystem_aggregate(symbol: str = "XAU/USD") -> Dict[str, Any]:
|
| 274 |
start = time.time()
|
| 275 |
agg = await aggregate_ecosystem(symbol)
|
| 276 |
latency = int((time.time() - start) * 1000)
|
| 277 |
|
| 278 |
+
# Отправка агрегированного сигнала в Hub
|
| 279 |
+
await send_signal_to_hub(symbol, agg['direction'], agg['confidence'])
|
| 280 |
+
|
| 281 |
result = {
|
| 282 |
"space": "space_24_ecosystem_aggregator",
|
| 283 |
"timestamp": int(time.time()),
|
|
|
|
| 296 |
"latency_ms": latency
|
| 297 |
}
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
logger.info(f"🌐 Ecosystem Agg: {symbol} {agg['direction']} conf={agg['confidence']:.3f} score={agg['ecosystem_score']:.1f} active={agg['active_spaces']}")
|
| 300 |
return result
|
| 301 |
|
| 302 |
# ================= FASTAPI =================
|
| 303 |
+
app = FastAPI(title="Tomiris Space 24 v2.1 — Ecosystem Aggregator Pro (Hub)")
|
| 304 |
|
| 305 |
@app.on_event("startup")
|
| 306 |
async def startup():
|
|
|
|
| 314 |
async def health():
|
| 315 |
return {
|
| 316 |
"status": "operational",
|
| 317 |
+
"version": "2.1",
|
| 318 |
"symbols": SYMBOLS,
|
| 319 |
"ecosystem_spaces": list(ECOSYSTEM_SPACES.keys()),
|
| 320 |
+
"hub_connected": True
|
|
|
|
| 321 |
}
|
| 322 |
|
| 323 |
@app.get("/consilium")
|
|
|
|
| 342 |
import uvicorn
|
| 343 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 344 |
|
| 345 |
+
print("🚀 SPACE 24 v2.1 — ECOSYSTEM AGGREGATOR PRO (Hub-Connected) ЗАПУЩЕН!")
|