File size: 5,553 Bytes
e465928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215df55
 
 
 
e465928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215df55
 
 
 
 
 
 
 
 
e465928
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215df55
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
# ====================================================================================
#
#   M A T R I X - A I  :::  C O N T R O L   P R O G R A M
#   "Know thyself."
#
#   Access programs with:  make help
#
# ====================================================================================

# System & Colors
BRIGHT_GREEN  := $(shell tput -T screen setaf 10)
DIM_GREEN     := $(shell tput -T screen setaf 2)
RESET         := $(shell tput -T screen sgr0)

# Python / Venv
SYS_PYTHON := python3
VENV_DIR   := .venv
PYTHON     := $(VENV_DIR)/bin/python
PIP        := $(PYTHON) -m pip

# App
APP_MODULE := app.main:app
PORT       := 7860

# Docker / HF Spaces
IMG_NAME   := matrix-ai:local
SPACE_URL  ?= https://huggingface.co/spaces/ruslanmv/matrix-ai

# Files & Dirs
REQ        := requirements.txt
TEST_DIR   := tests

.DEFAULT_GOAL := help

# ---------------------------------------------------------------------------
# Help
# ---------------------------------------------------------------------------
help:
	@echo
	@echo "$(BRIGHT_GREEN)M A T R I X - A I ::: C O N T R O L   P R O G R A M$(RESET)"
	@echo
	@printf "$(BRIGHT_GREEN)  %-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "PROGRAM" "DESCRIPTION"
	@printf "$(BRIGHT_GREEN)  %-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "----------------------" "--------------------------------------------------------"
	@echo
	@echo "$(BRIGHT_GREEN)Environment$(RESET)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "venv" "Create virtualenv (.venv)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "install" "Install deps into venv (incremental)"
	@echo
	@echo "$(BRIGHT_GREEN)Quality$(RESET)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "lint" "ruff check"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "fmt" "black + ruff fix"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "test" "pytest"
	@echo
	@echo "$(BRIGHT_GREEN)Run$(RESET)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "run" "Run uvicorn (PORT=$(PORT))"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "run-hot" "Run with --reload"
	@echo
	@echo "$(BRIGHT_GREEN)RAG / Knowledge Base$(RESET)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "kb" "Build/refresh KB from GitHub + local docs (writes data/kb.jsonl)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "kb-force" "Force rebuild KB (deletes existing data/kb.jsonl)"
	@echo
	@echo "$(BRIGHT_GREEN)Docker$(RESET)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "docker-build" "Build local image ($(IMG_NAME))"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "docker-run" "Run local container (maps $(PORT))"
	@echo
	@echo "$(BRIGHT_GREEN)HF Spaces helpers$(RESET)"
	@printf "  $(BRIGHT_GREEN)%-22s$(RESET) $(DIM_GREEN)%s$(RESET)\n" "space-url" "Echo the Space URL (set SPACE_URL=...)"
	@echo

# ---------------------------------------------------------------------------
# Env
# ---------------------------------------------------------------------------
$(VENV_DIR)/bin/activate:
	@test -d $(VENV_DIR) || $(SYS_PYTHON) -m venv $(VENV_DIR)

venv: $(VENV_DIR)/bin/activate
	@echo "$(DIM_GREEN)-> Upgrading pip/setuptools/wheel$(RESET)"
	@$(PIP) install -U pip setuptools wheel >/dev/null

install: venv
	@echo "$(DIM_GREEN)-> Installing deps$(RESET)"
	@$(PIP) install -r $(REQ)
	@echo "$(BRIGHT_GREEN)OK$(RESET)"

# ---------------------------------------------------------------------------
# Quality
# ---------------------------------------------------------------------------
lint: venv
	@$(PYTHON) -m ruff check app tests || true

fmt: venv
	@$(PYTHON) -m black app tests || true
	@$(PYTHON) -m ruff check --fix app tests || true

test: venv
	@$(PYTHON) -m pytest -q --disable-warnings --maxfail=1 || true

# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------
run: install
	@PORT=$(PORT) $(VENV_DIR)/bin/uvicorn $(APP_MODULE) --host 0.0.0.0 --port $(PORT)

run-hot: install
	@PORT=$(PORT) $(VENV_DIR)/bin/uvicorn $(APP_MODULE) --host 0.0.0.0 --port $(PORT) --reload

# ---------------------------------------------------------------------------
# RAG / Knowledge Base
# ---------------------------------------------------------------------------
kb: install
	@PYTHONPATH=. $(PYTHON) scripts/build_kb.py --config configs/rag_sources.yaml --out data/kb.jsonl

kb-force: install
	@rm -f data/kb.jsonl && PYTHONPATH=. $(PYTHON) scripts/build_kb.py --config configs/rag_sources.yaml --out data/kb.jsonl

# ---------------------------------------------------------------------------
# Docker
# ---------------------------------------------------------------------------
docker-build:
	@docker build -t $(IMG_NAME) .

docker-run:
	@docker run --rm -it -p $(PORT):$(PORT) -e PORT=$(PORT) $(IMG_NAME)

# ---------------------------------------------------------------------------
# HF Helpers
# ---------------------------------------------------------------------------
space-url:
	@echo "Space: $(SPACE_URL)"

# ---------------------------------------------------------------------------
# Clean
# ---------------------------------------------------------------------------
clean:
	@rm -rf .venv __pycache__ .pytest_cache .ruff_cache .mypy_cache dist build *.egg-info

.PHONY: help venv install lint fmt test run run-hot kb kb-force docker-build docker-run space-url clean