PETEROCVILLE35 commited on
Commit
edf6ffa
Β·
verified Β·
1 Parent(s): c006197

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile_B +72 -0
  2. download_model.py +32 -0
  3. server_B.py +11 -43
  4. start_B.sh +32 -0
Dockerfile_B ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ═══════════════════════════════════════════════════════════════════════════════
2
+ # Dockerfile β€” Cerebro B β€” VibeEngine v10.0 (BitNet REAL β€” ik_llama.cpp)
3
+ # Arquitectura: MATH(<2ms) + BitNet-2B-1.58bit (100%% inferencias) + FastAPI
4
+ # Motor: ik_llama.cpp AVX512_VNNI_VBMI β€” compatible GGUF i2_s nativo
5
+ # Gaussian+Donchian+ADX+SuperTrend+CHoCH
6
+ # ═══════════════════════════════════════════════════════════════════════════════
7
+
8
+ # ── Etapa 1: Descargar modelo BitNet 2B ──────────────────────────────────────
9
+ FROM python:3.11-slim AS model-fetcher
10
+
11
+ RUN apt-get update && apt-get install -y --no-install-recommends \
12
+ ca-certificates wget \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ RUN pip install --no-cache-dir huggingface_hub
16
+
17
+ COPY download_model.py /tmp/download_model.py
18
+ RUN python3 /tmp/download_model.py
19
+
20
+ # ── Etapa 2: Runtime ─────────────────────────────────────────────────────────
21
+ FROM python:3.11-slim AS runtime
22
+
23
+ RUN apt-get update && apt-get install -y --no-install-recommends \
24
+ curl ca-certificates libgomp1 libstdc++6 unzip wget \
25
+ && rm -rf /var/lib/apt/lists/*
26
+
27
+ # ik_llama.cpp precompilado β€” soporte GGUF i2_s nativo (BitNet ternario)
28
+ # AVX512_VNNI_VBMI: Γ³ptimo para Intel Xeon Platinum de HF Spaces
29
+ RUN wget -q --user-agent="Mozilla/5.0" -O /tmp/ik.zip \
30
+ "https://github.com/Thireus/ik_llama.cpp/releases/download/main-b4801-b275691/ik_llama-main-b4801-b275691-bin-ubuntu-x64-avx512_vnni_vbmi.zip" \
31
+ && mkdir -p /app && unzip -q /tmp/ik.zip -d /app && rm /tmp/ik.zip \
32
+ && BINARY=$(find /app -name "llama-server" -type f | head -1) \
33
+ && ln -sf "$BINARY" /usr/local/bin/llama-server && chmod +x "$BINARY"
34
+
35
+ ENV PATH="/app:${PATH}"
36
+ ENV LD_LIBRARY_PATH="/app:${LD_LIBRARY_PATH}"
37
+
38
+ WORKDIR /app
39
+
40
+ COPY --from=model-fetcher /models /models
41
+
42
+ RUN pip install --no-cache-dir \
43
+ fastapi==0.111.0 \
44
+ "uvicorn[standard]==0.30.1" \
45
+ httpx==0.27.0 \
46
+ numpy==1.26.4 \
47
+ scipy==1.13.0 \
48
+
49
+ # Variables de entorno β€” compatibles con start.sh y server.py
50
+ ENV MODEL_PATH="/models/ggml-model-i2_s.gguf"
51
+ ENV N_CTX=2048
52
+ ENV N_THREADS=2
53
+ ENV BITNET_PORT=8080
54
+ ENV PORT=7860
55
+ ENV CEREBRO_ID=B
56
+ ENV BITNET_TIMEOUT=45.0
57
+ ENV DONCHIAN_PERIOD=20
58
+ ENV SUPERTREND_MULT=3.0
59
+ ENV SUPERTREND_ATR=10
60
+ ENV ADX_PERIOD=14
61
+ ENV CACHE_TTL=45.0
62
+
63
+ COPY SCRIPT_VibeEngine ./
64
+ COPY start_B.sh ./start.sh
65
+ RUN chmod +x start.sh
66
+
67
+ EXPOSE 7860
68
+
69
+ HEALTHCHECK --interval=30s --timeout=15s --start-period=300s --retries=5 \
70
+ CMD curl -f http://localhost:7860/health || exit 1
71
+
72
+ CMD ["./start.sh"]
download_model.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ download_model.py β€” Descarga ggml-model-i2_s.gguf (BitNet 2B)
3
+ Compartido entre todos los Cerebros APEX (B, C, E, F, G, H).
4
+ """
5
+ import os
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ REPO = "microsoft/bitnet-b1.58-2B-4T-gguf"
9
+ FILE = "ggml-model-i2_s.gguf"
10
+ DEST = "/models"
11
+
12
+ def download():
13
+ os.makedirs(DEST, exist_ok=True)
14
+ out = os.path.join(DEST, FILE)
15
+ if os.path.exists(out) and os.path.getsize(out) > 100_000_000:
16
+ print(f"[FETCHER] Ya existe: {out} ({os.path.getsize(out)//1024//1024} MB)")
17
+ return
18
+
19
+ print(f"[FETCHER] Descargando {REPO}/{FILE}...")
20
+ try:
21
+ hf_hub_download(
22
+ repo_id=REPO, filename=FILE,
23
+ local_dir=DEST, local_dir_use_symlinks=False,
24
+ )
25
+ size_mb = os.path.getsize(out) // 1024 // 1024
26
+ print(f"[FETCHER] OK: {out} ({size_mb} MB)")
27
+ except Exception as e:
28
+ print(f"[FETCHER] ERROR: {e}")
29
+ raise
30
+
31
+ if __name__ == "__main__":
32
+ download()
server_B.py CHANGED
@@ -53,10 +53,9 @@ TELEMETRÍA: [B/MATH] ms | [B/LLM] ms | [B/TOTAL] ms
53
  """
54
 
55
  import os, json, re, time, threading, math, asyncio
56
- from concurrent.futures import ThreadPoolExecutor
57
  from fastapi import FastAPI, Request
58
  from fastapi.responses import JSONResponse
59
- from llama_cpp import Llama
60
 
61
  # numpy obligatorio β€” implementaciΓ³n de indicadores vectorizada
62
  try:
@@ -71,31 +70,8 @@ app = FastAPI(title="Cerebro B β€” VibeEngine v10.0 NUMPY+LLM")
71
  # ── Config ────────────────────────────────────────────────────────────────────
72
  CEREBRO_ID = "B"
73
  VERSION = "10.0"
74
- MODEL_PATH = os.environ.get("MODEL_PATH", "/models/qwen2.5-1.5b-instruct-q4_k_m.gguf")
75
- N_CTX = int(os.environ.get("N_CTX", "128")) # ultra-corto: 2-3 tokens salida
76
- N_THREADS = int(os.environ.get("N_THREADS", "2"))
77
- N_BATCH = int(os.environ.get("N_BATCH", "64"))
78
- CACHE_TTL = float(os.environ.get("CACHE_TTL", "45.0"))
79
-
80
- # ParΓ‘metros de indicadores (configurables)
81
- DONCHIAN_PERIOD = int(os.environ.get("DONCHIAN_PERIOD", "20"))
82
- SUPERTREND_MULT = float(os.environ.get("SUPERTREND_MULT", "3.0"))
83
- SUPERTREND_ATR = int(os.environ.get("SUPERTREND_ATR", "10"))
84
- ADX_PERIOD = int(os.environ.get("ADX_PERIOD", "14"))
85
-
86
- print(f"[B] Cargando modelo: {MODEL_PATH}")
87
- print(f"[B] numpy={'βœ…' if _NP_OK else '⚠️ fallback'} | Indicadores vectorizados")
88
- llm = Llama(
89
- model_path=MODEL_PATH,
90
- n_ctx=N_CTX,
91
- n_threads=N_THREADS,
92
- n_batch=N_BATCH,
93
- n_gpu_layers=0,
94
- verbose=False,
95
- )
96
- _llm_lock = threading.Lock()
97
- executor = ThreadPoolExecutor(max_workers=1)
98
- print(f"[B] βœ… VibeEngine v10.0 NUMPY+LLM β€” n_ctx={N_CTX}")
99
 
100
  # ── Cache ──────────────────────────────────────────────────────────────────────
101
  _B_CACHE: dict = {}
@@ -467,7 +443,7 @@ def _math_full_analysis(prompt: str, sym: str) -> dict:
467
  # FASE LLM β€” Visto Bueno Final con sesgo BULL/BEAR/NEUTRAL
468
  # ══════════════════════════════════════════════════════════════════════════════
469
 
470
- def _llm_bias_final(math_data: dict, sym: str) -> dict:
471
  """
472
  El LLM actΓΊa como ComitΓ© de DirecciΓ³n Visual:
473
  Recibe el panel de control destilado por MATH y emite el sesgo final.
@@ -497,19 +473,11 @@ def _llm_bias_final(math_data: dict, sym: str) -> dict:
497
  )
498
 
499
  try:
500
- with _llm_lock:
501
- raw_out = llm(
502
- llm_prompt,
503
- max_tokens=28,
504
- temperature=0.05,
505
- top_p=0.95,
506
- top_k=5,
507
- stop=["<|im_end|>", "\n\n", "<|im_start|>"],
508
- echo=False,
509
- repeat_penalty=1.0,
510
- )
511
  llm_ms = (time.perf_counter() - t0_llm) * 1000
512
- raw = "{" + raw_out["choices"][0]["text"]
513
 
514
  m = re.search(r'\{[^{}]*"bias"\s*:\s*"(BULL|BEAR|NEUTRAL)"[^{}]*\}', raw, re.IGNORECASE)
515
  if m:
@@ -549,7 +517,7 @@ def _llm_bias_final(math_data: dict, sym: str) -> dict:
549
  # Solo se omite en modo cached o si el anΓ‘lisis es trivialmente claro.
550
  # ══════════════════════════════════════════════════════════════════════════════
551
 
552
- def _run_inference(agent: str, prompt: str) -> dict:
553
  t0_total = time.perf_counter()
554
  sym = _parse_symbol(prompt)
555
 
@@ -584,7 +552,7 @@ def _run_inference(agent: str, prompt: str) -> dict:
584
  print(f"[B/MATH-CLEAR] {sym}: bias={bias} (signal claro, skip LLM) | {math_ms:.1f}ms")
585
  else:
586
  # Zona ambigua β†’ invocar LLM
587
- llm_data = _llm_bias_final(math_data, sym)
588
  bias = llm_data["llm_bias"]
589
  llm_ms = llm_data["_llm_ms"]
590
 
@@ -624,7 +592,7 @@ def root():
624
  "status": "online",
625
  "cerebro": CEREBRO_ID,
626
  "version": VERSION,
627
- "model": MODEL_PATH.split("/")[-1],
628
  "agents": ["VibeEngine"],
629
  "features": [
630
  "Gaussian Noise Filter (numpy convolution)",
 
53
  """
54
 
55
  import os, json, re, time, threading, math, asyncio
 
56
  from fastapi import FastAPI, Request
57
  from fastapi.responses import JSONResponse
58
+ import httpx # BitNet v6.0 β€” ik_llama.cpp via HTTP
59
 
60
  # numpy obligatorio β€” implementaciΓ³n de indicadores vectorizada
61
  try:
 
70
  # ── Config ────────────────────────────────────────────────────────────────────
71
  CEREBRO_ID = "B"
72
  VERSION = "10.0"
73
+
74
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  # ── Cache ──────────────────────────────────────────────────────────────────────
77
  _B_CACHE: dict = {}
 
443
  # FASE LLM β€” Visto Bueno Final con sesgo BULL/BEAR/NEUTRAL
444
  # ══════════════════════════════════════════════════════════════════════════════
445
 
446
+ async def _llm_bias_final(math_data: dict, sym: str) -> dict:
447
  """
448
  El LLM actΓΊa como ComitΓ© de DirecciΓ³n Visual:
449
  Recibe el panel de control destilado por MATH y emite el sesgo final.
 
473
  )
474
 
475
  try:
476
+ result = await _bitnet_infer(llm_prompt)
477
+ raw_out_text = result.get("raw", "")
478
+
 
 
 
 
 
 
 
 
479
  llm_ms = (time.perf_counter() - t0_llm) * 1000
480
+ raw = "{" + raw_out_text
481
 
482
  m = re.search(r'\{[^{}]*"bias"\s*:\s*"(BULL|BEAR|NEUTRAL)"[^{}]*\}', raw, re.IGNORECASE)
483
  if m:
 
517
  # Solo se omite en modo cached o si el anΓ‘lisis es trivialmente claro.
518
  # ══════════════════════════════════════════════════════════════════════════════
519
 
520
+ async def _run_inference(agent: str, prompt: str) -> dict:
521
  t0_total = time.perf_counter()
522
  sym = _parse_symbol(prompt)
523
 
 
552
  print(f"[B/MATH-CLEAR] {sym}: bias={bias} (signal claro, skip LLM) | {math_ms:.1f}ms")
553
  else:
554
  # Zona ambigua β†’ invocar LLM
555
+ llm_data = await _llm_bias_final(math_data, sym)
556
  bias = llm_data["llm_bias"]
557
  llm_ms = llm_data["_llm_ms"]
558
 
 
592
  "status": "online",
593
  "cerebro": CEREBRO_ID,
594
  "version": VERSION,
595
+ "model": "BitNet-b1.58-2B-4T-i2_s (ik_llama.cpp)", "bitnet_server": BITNET_BASE,
596
  "agents": ["VibeEngine"],
597
  "features": [
598
  "Gaussian Noise Filter (numpy convolution)",
start_B.sh ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e
3
+ MODEL_PATH="${MODEL_PATH:-/models/ggml-model-i2_s.gguf}"
4
+ BITNET_PORT="${BITNET_PORT:-8080}"
5
+ N_CTX="${N_CTX:-2048}"
6
+ N_THREADS="${N_THREADS:-2}"
7
+ PORT="${PORT:-7860}"
8
+
9
+ echo "[START] Cerebro VibeEngine v10.0 (BitNet REAL)"
10
+ [ ! -f "$MODEL_PATH" ] && echo "[START] FATAL: modelo no encontrado" && exit 1
11
+ echo "[START] Modelo: $(du -h $MODEL_PATH | cut -f1)"
12
+
13
+ BITNET_LOG="/tmp/bitnet.log"
14
+ > "$BITNET_LOG"
15
+
16
+ llama-server \
17
+ --model "$MODEL_PATH" --host 127.0.0.1 --port "$BITNET_PORT" \
18
+ --ctx-size "$N_CTX" --threads "$N_THREADS" --gpu-layers 0 \
19
+ 2>&1 | tee "$BITNET_LOG" | sed 's/^/[BITNET] /' &
20
+ LLAMA_PID=$!
21
+
22
+ MAX_WAIT=240; ELAPSED=0
23
+ while [ $ELAPSED -lt $MAX_WAIT ]; do
24
+ ! kill -0 $LLAMA_PID 2>/dev/null && echo "[START] FATAL: llama-server muriΓ³" && tail -20 "$BITNET_LOG" && exit 1
25
+ curl -sf "http://127.0.0.1:${BITNET_PORT}/health" > /dev/null 2>&1 && break
26
+ sleep 5; ELAPSED=$((ELAPSED+5))
27
+ [ $((ELAPSED%30)) -eq 0 ] && echo "[START] ${ELAPSED}/${MAX_WAIT}s cargando..."
28
+ done
29
+ [ $ELAPSED -ge $MAX_WAIT ] && echo "[START] FATAL: timeout" && exit 1
30
+ echo "[START] BitNet ONLINE β€” iniciando FastAPI..."
31
+
32
+ exec uvicorn server_B:app --host 0.0.0.0 --port "$PORT" --workers 1 --log-level info