Skip to content

Commit 9e04613

Browse files
add tests and some refactoring
1 parent abf074e commit 9e04613

15 files changed

+1122
-615
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ docs/old_redis_model_store.ipynb
22
docs/test.py
33

44
__pycache__/
5-
.mypy_cache/
5+
.mypy_cache/
6+
.pytest_cache/
7+
.coverage
8+
htmlcov/
9+
10+
dump.rdb

README.md

Lines changed: 89 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,120 @@
1-
# Redis Model Store
1+
# 🧠 Redis Model Store
22

33
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
44
![Language](https://img.shields.io/github/languages/top/redis-applied-ai/redis-model-store)
55
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
66
![GitHub last commit](https://img.shields.io/github/last-commit/redis-applied-ai/redis-model-store)
77
[![pypi](https://badge.fury.io/py/redisvl.svg)](https://pypi.org/project/redis-model-store/)
88

9-
`redis-model-store` is a simple Python library designed to handle versioning and serialization of AI/ML models into Redis. It provides a streamlined way to manage your machine learning models in Redis.
9+
Store, version, and manage your ML models in Redis with ease. `redis-model-store` provides a simple yet powerful interface for handling machine learning model artifacts in Redis.
1010

11-
## Features
11+
## Features
1212

13-
- **Pluggable Serialization**: Serialize/deserialize any Python object (Numpy arrays, Scikit-Learn, PyTorch, TensorFlow models, etc.).
14-
- **Sharding for Large Models**: Splits large serialized payloads into manageable chunks to optimize Redis storage.
15-
- **Version Management**: Automatically manages model versions in Redis, allowing you to store and retrieve specific versions.
13+
- **🔄 Automatic Versioning**: Track and manage multiple versions of your models
14+
- **📦 Smart Storage**: Large models are automatically sharded for optimal storage
15+
- **🔌 Pluggable Serialization**: Works with any Python object (NumPy, PyTorch, TensorFlow, etc.)
16+
- **🏃‍♂️ High Performance**: Efficient storage and retrieval using Redis pipelining
17+
- **🛡️ Safe Operations**: Atomic operations with automatic cleanup on failures
1618

19+
## 🚀 Quick Start
20+
21+
### Installation
1722

18-
## Installation
1923
```bash
24+
# Using pip
2025
pip install redis-model-store
26+
27+
# Or using poetry
28+
poetry add redis-model-store
2129
```
2230

23-
## Usage
31+
### Basic Usage
2432

25-
See the fully detailed [example notebook](docs/redis_model_store.ipynb) for more assistance in getting started.
33+
Here's a simple example using scikit-learn:
2634

27-
### Init the ModelStore
2835
```python
29-
from model_store import ModelStore
3036
from redis import Redis
37+
from model_store import ModelStore
38+
from sklearn.ensemble import RandomForestClassifier
39+
40+
# Connect to Redis and initialize store
41+
redis = Redis(host="localhost", port=6379)
42+
store = ModelStore(redis)
43+
44+
# Train your model
45+
model = RandomForestClassifier()
46+
model.fit(X_train, y_train)
3147

32-
# Initialize the Redis client
33-
redis_client = Redis.from_url("redis://localhost:6379")
48+
# Save model with version tracking
49+
version = store.save_model(
50+
model,
51+
name="my-classifier",
52+
description="Random forest trained on dataset v1"
53+
)
54+
55+
# List available models
56+
models = store.list_models()
57+
print(f"Available models: {models}")
58+
59+
# Load latest version
60+
model = store.load_model("my-classifier")
3461

35-
# Initialize the ModelStore with (optional) shard size
36-
model_store = ModelStore(redis_client, shard_size=1012 * 100)
62+
# Load specific version
63+
model = store.load_model("my-classifier", version=version)
64+
65+
# View all versions
66+
versions = store.get_all_versions("my-classifier")
67+
for v in versions:
68+
print(f"Version: {v.version}, Created: {v.created_at}")
3769
```
3870

39-
### Store a model
40-
You can store any serializable Python object.
41-
```python
42-
from sklearn.ensemble import RandomForestClassifier
43-
from sklearn.datasets import load_iris
44-
from sklearn.model_selection import train_test_split
71+
## 🛠️ Contributing
4572

46-
# Load sample data
47-
iris = load_iris()
48-
X_train, X_test, y_train, y_test = train_test_split(
49-
iris.data, iris.target, test_size=0.2, random_state=42
50-
)
73+
We welcome contributions! Here's how to get started:
5174

52-
# Train a simple RandomForest model
53-
model = RandomForestClassifier()
54-
model.fit(X_train, y_train)
75+
### Development Setup
5576

56-
# Save the model to Redis
57-
version = model_store.save_model(model, name="random_forest", description="Random forest classifier model")
77+
1. Clone the repository:
78+
```bash
79+
git clone https://github.com/redis-applied-ai/redis-model-store.git
80+
cd redis-model-store
5881
```
5982

60-
### Load models
61-
```python
62-
# Grab the latest model
63-
model = model_store.load_model(name="random_forest")
83+
2. Install poetry if you haven't:
84+
```bash
85+
curl -sSL https://install.python-poetry.org | python3 -
86+
```
6487

65-
# Grab a specific model version
66-
model = model_store.load_model(name="random_forest", version=version)
88+
3. Install dependencies:
89+
```bash
90+
poetry install --all-extras
91+
```
92+
93+
### Linting and Tests
94+
95+
```bash
96+
poetry run format
97+
poetry run check-mypy
98+
poetry run test
99+
poetry run test-verbose
67100
```
68101

69-
##
102+
### Making Changes
103+
104+
1. Create a new branch:
105+
```bash
106+
git checkout -b feat/your-feature-name
107+
```
108+
109+
2. Make your changes and ensure:
110+
- All tests pass (covering new functionality)
111+
- Code is formatted
112+
- Type hints are valid
113+
- Examples/docs added as notebooks to the `docs/` directory.
114+
115+
3. Push changes and open a PR
116+
117+
118+
## 📚 Documentation
119+
120+
For more usage examples check out tbhis [Example Notebook](docs/redis_model_store.ipynb).

conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import pytest
3+
4+
from testcontainers.compose import DockerCompose
5+
6+
7+
@pytest.fixture(scope="session", autouse=True)
8+
def redis_container():
9+
# Set the default Redis version if not already set
10+
os.environ.setdefault("REDIS_VERSION", "edge")
11+
12+
compose = DockerCompose("tests", compose_file_name="docker-compose.yml", pull=True)
13+
compose.start()
14+
15+
redis_host, redis_port = compose.get_service_host_and_port("redis", 6379)
16+
redis_url = f"redis://{redis_host}:{redis_port}"
17+
os.environ["REDIS_URL"] = redis_url
18+
19+
yield compose
20+
21+
compose.stop()

0 commit comments

Comments
 (0)