organ-store/organ_store.py
2026-02-25 00:53:36 +00:00

96 lines
3.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Organ Store — AI Model Component Marketplace
Part of the Inference-X Ecosystem
# SALKA ELMADANI | inference-x.com | BSL-1.1
Copyright (C) 2024-2026 Salka Elmadani. BSL-1.1.
https://git.inference-x.com/inference-x-community/organ-store
Neural networks have anatomy.
Extract. Share. Transplant.
The future of open AI.
"""
from fastapi import FastAPI, Request, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import sqlite3, json, time, os
app = FastAPI(title="Organ Store", version="1.0.0")
app.add_middleware(CORSMiddleware, allow_origins=["*"],
allow_methods=["*"], allow_headers=["*"])
DB = os.environ.get("ORGAN_DB", "./organ_store.db")
ORGAN_TYPES = ["attention_head","ffn_block","expert_block","embedding","lm_head","custom"]
def db():
conn = sqlite3.connect(DB); conn.row_factory = sqlite3.Row; return conn
def init_db():
with db() as c:
c.execute("""CREATE TABLE IF NOT EXISTS organs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, type TEXT NOT NULL,
source_model TEXT, description TEXT,
capability TEXT, download_url TEXT,
size_mb REAL DEFAULT 0, downloads INTEGER DEFAULT 0,
author TEXT DEFAULT 'anonymous', created_at INTEGER,
verified INTEGER DEFAULT 0
)""")
c.commit()
init_db()
@app.get("/catalog")
async def catalog(type_filter: str = None, limit: int = 50):
"""Browse available organs in the store."""
with db() as c:
q = "SELECT * FROM organs ORDER BY downloads DESC LIMIT ?"
params = [limit]
if type_filter and type_filter in ORGAN_TYPES:
q = "SELECT * FROM organs WHERE type=? ORDER BY downloads DESC LIMIT ?"
params = [type_filter, limit]
rows = c.execute(q, params).fetchall()
return {"organs":[dict(r) for r in rows],"total":len(rows),"types":ORGAN_TYPES}
@app.post("/publish")
async def publish(request: Request):
"""Publish an organ to the community store."""
data = await request.json()
if not data.get("name") or not data.get("type"):
return JSONResponse({"error":"name and type required"}, status_code=400)
if data["type"] not in ORGAN_TYPES:
return JSONResponse({"error":f"type must be one of {ORGAN_TYPES}"}, status_code=400)
with db() as c:
c.execute("""INSERT INTO organs (name,type,source_model,description,capability,
download_url,size_mb,author,created_at)
VALUES (?,?,?,?,?,?,?,?,?)""",
(data["name"], data["type"], data.get("source_model",""),
data.get("description",""), data.get("capability",""),
data.get("download_url",""), float(data.get("size_mb",0)),
data.get("author","anonymous"), int(time.time())))
organ_id = c.lastrowid
c.commit()
return {"status":"ok","organ_id":organ_id,"message":"Organ published to the community store."}
@app.get("/organ/{organ_id}")
async def get_organ(organ_id: int):
with db() as c:
organ = c.execute("SELECT * FROM organs WHERE id=?", (organ_id,)).fetchone()
if organ:
c.execute("UPDATE organs SET downloads=downloads+1 WHERE id=?", (organ_id,))
c.commit()
if not organ:
return JSONResponse({"error":"Organ not found"}, status_code=404)
return dict(organ)
@app.get("/health")
async def health():
return {"status":"ok","service":"Organ Store","author":"Salka Elmadani"}
if __name__ == "__main__":
import uvicorn
print("Organ Store — AI Model Component Marketplace")
print("Extract. Share. Transplant.")
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT","7939")))