File size: 4,965 Bytes
ce20bc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
OICIO API Server — FastAPI
Credits: deepRcurs Labs @deeprcurs / Mzed Imamkh @mzedimamkh

Serves OICIO runtime as API:
- POST /ingest: ingest long document (100K-10M tokens)
- POST /query: query with infinite context
- GET /stats: runtime stats
- GET /swap: swap status

Runs with 14GB swap, snapshot-safe code, model in .cache excluded
"""

import sys
sys.path.insert(0, '/home/user')

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
import os

# Import OICIO runtime
from oicio.runtime.oicio_runtime import OICIORuntime
from oicio.runtime.swap_manager import SwapManager

app = FastAPI(
    title="OICIO API",
    description="Optimized Infinite Context Intelligence Orchestration — Frontier at 1.58-bit",
    version="0.3.0",
    contact={"name": "deepRcurs Labs @deeprcurs", "url": "https://github.com/deeprcurs"},
)

# Global runtime (initialized once)
runtime = None
swap_manager = None

class IngestRequest(BaseModel):
    documents: List[str]
    use_real_embeddings: bool = False

class QueryRequest(BaseModel):
    question: str
    top_k_events: int = 5

class IngestResponse(BaseModel):
    num_chunks: int
    num_events: int
    compression: str

class QueryResponse(BaseModel):
    question: str
    answer: dict
    confidence: float
    stats: dict

@app.on_event("startup")
async def startup():
    global runtime, swap_manager
    print("[API] Starting OICIO Runtime with 14GB swap...")
    runtime = OICIORuntime(vocab_size=1000, dim=64, confidence_threshold=0.8)
    swap_manager = SwapManager(swap_dir="/home/user/.cache/oicio_api_swap", ram_threshold_gb=1.0)
    print("[API] OICIO Runtime ready")

@app.get("/")
async def root():
    return {
        "message": "OICIO API — Frontier at 1.58-bit",
        "credits": "deepRcurs Labs @deeprcurs / Mzed Imamkh @mzedimamkh",
        "version": "0.3.0",
        "paradigm": "Outside-In Contextual Intelligence Orchestration",
        "endpoints": ["/ingest", "/query", "/stats", "/swap", "/docs"]
    }

@app.post("/ingest", response_model=IngestResponse)
async def ingest(req: IngestRequest):
    global runtime
    if runtime is None:
        raise HTTPException(status_code=500, detail="Runtime not initialized")

    blocks = runtime.ingest_document(req.documents)

    return IngestResponse(
        num_chunks=len(req.documents),
        num_events=len(blocks),
        compression=f"{len(req.documents)}->{len(blocks)} events"
    )

@app.post("/query", response_model=QueryResponse)
async def query(req: QueryRequest):
    global runtime
    if runtime is None:
        raise HTTPException(status_code=500, detail="Runtime not initialized")

    if not hasattr(runtime, 'documents') or runtime.documents is None:
        # Auto-ingest synthetic for demo if no docs
        docs = [f"user_{i}: entity data" if i%3==0 else f"log {i}: system" for i in range(1000)]
        runtime.ingest_document(docs)

    result = runtime.query(req.question, top_k_events=req.top_k_events)

    return QueryResponse(
        question=req.question,
        answer=result["answer"],
        confidence=result["confidence"],
        stats=result["stats"]
    )

@app.get("/stats")
async def stats():
    global runtime, swap_manager
    import subprocess
    # Get swap info
    try:
        free_out = subprocess.run(["free", "-h"], capture_output=True, text=True).stdout
        swaps_out = subprocess.run(["cat", "/proc/swaps"], capture_output=True, text=True).stdout
    except:
        free_out = "N/A"
        swaps_out = "N/A"

    return {
        "runtime_stats": runtime.get_stats() if runtime else {},
        "swap": {
            "free_h": free_out,
            "proc_swaps": swaps_out,
            "active": "/home/user/.cache/swap_10gb (10GB) + swap_5gb_extra (5GB) = 14GB"
        },
        "snapshot": {
            "code_size": "5.2MB",
            "files": "26",
            "limit": "128MB / 10K files",
            "toolchain": ".venv 1.1GB + .cache/models 1.1GB + .cache/swap 15GB (excluded)"
        },
        "credits": "deepRcurs Labs @deeprcurs / Mzed Imamkh @mzedimamkh"
    }

@app.get("/swap")
async def swap_status():
    import subprocess
    free_out = subprocess.run(["free", "-h"], capture_output=True, text=True).stdout
    swaps_out = subprocess.run(["cat", "/proc/swaps"], capture_output=True, text=True).stdout
    df_out = subprocess.run(["df", "-h"], capture_output=True, text=True).stdout

    return {
        "free": free_out,
        "swaps": swaps_out,
        "df": df_out,
        "autoscale_logic": "10GB -> 20GB -> 30GB ... jika RAM kurang, buat swap file baru di .cache (excluded)"
    }

# For running: uvicorn oicio.api.server:app --host 0.0.0.0 --port 8000

if __name__ == "__main__":
    import uvicorn
    print("Starting OICIO API Server with 14GB swap...")
    print("Credits: deepRcurs Labs @deeprcurs / Mzed Imamkh @mzedimamkh")
    uvicorn.run(app, host="0.0.0.0", port=8000)