Metadata-Version: 2.4
Name: tinylmtune
Version: 0.0.3
Summary: A lightweight Python library that automates TinyBERT fine-tuning with Genetic Algorithm hyperparameter optimisation
Author: Manish Agrawal / Priyanka Chakraborty
Author-email: Manish Agrawal <manishagrawal.datascience@gmail.com>, Priyanka Chakraborty <priyanka08993@gmail.com>
License: Copyright (c) 2018 The Python Packaging Authority
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/manishagrawal-datascience/TinyLMTune
Project-URL: Repository, https://github.com/manishagrawal-datascience/TinyLMTune
Project-URL: Bug Tracker, https://github.com/manishagrawal-datascience/TinyLMTune/issues
Keywords: tinybert,fine-tuning,genetic-algorithm,nlp,transformers,hyperparameter-optimization
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0
Requires-Dist: transformers>=4.30
Requires-Dist: datasets>=2.0
Requires-Dist: scikit-learn>=1.3
Requires-Dist: numpy>=1.24
Requires-Dist: sentencepiece>=0.1.99
Requires-Dist: matplotlib>=3.7
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: build; extra == "dev"
Dynamic: author
Dynamic: license-file
Dynamic: requires-python

# tinyLMTune

**Genetic-Algorithm-Optimised TinyBERT Fine-Tuning in one function call.**

tinyLMTune automates the full pipeline: 
(1) dataset building  
(2) GA hyperparameter search 
(3) TinyBERT fine-tuning 
(4) model export 

Bring your own data or let it generate synthetic training data automatically.

## Installation

```bash
pip install tinylmtune
```

## Quick Start

### Option 1 — Bring your own data (recommended)

```python
from tinylmtune import optimize_slm, TinyInference

my_data = [
    {"text": "Loved this product!", "label": "positive"},
    {"text": "Broke after one day.", "label": "negative"},
    {"text": "Works fine, nothing special.", "label": "neutral"},
    # ... at least 50+ records recommended
]

best = optimize_slm(
    task="classification",
    user_data=my_data,
    output_dir="my_model",
)

model = TinyInference("my_model")
print(model.predict("Absolutely amazing quality!"))
```

### Option 2 — Use a JSONL file

```python
best = optimize_slm(
    task="classification",
    corpus_path="my_data.jsonl",   # one JSON object per line
    output_dir="my_model",
)
```

### Option 3 — Auto-generate synthetic data (default fallback)

If neither `user_data` nor `corpus_path` is provided, tinyLMTune generates
synthetic training data via a local Ollama/Mistral model:

```python
best = optimize_slm(
    task="classification",
    corpus_prompt="Generate movie-review sentiment examples",
    n_examples=200,
    output_dir="my_model",
)
```

## Data Format

Each record is a dict (or a JSON line in a `.jsonl` file). The required keys depend on the task:

| Task | Required keys | Example |
|------|---------------|---------|
| `classification` | `text`, `label` | `{"text": "Great film!", "label": "positive"}` |
| `summarization` | `text`, `summary` | `{"text": "Long article...", "summary": "Short version."}` |
| `qna` | `question`, `answer` | `{"question": "What is X?", "answer": "X is..."}` |
| `generation` | `prompt`, `completion` | `{"prompt": "Once upon a", "completion": "time there was..."}` |
| `ner` | `text`, `entities` | `{"text": "John in NYC", "entities": [{"text": "John", "label": "PER", "start": 0, "end": 4}]}` |

Records missing required keys are skipped with a warning. If all records are invalid, an error is raised showing the expected format.

## Public API

Only two symbols are exported:

| Symbol | Purpose |
|--------|---------|
| `optimize_slm()` | Full train pipeline — returns best config |
| `TinyInference` | Load & predict with a saved model |

All internal modules live in `tinylmtune._internal` and are **not** part of the public API.

## Project Structure

```
tinylmtune/
├── __init__.py              # Public API: optimize_slm, TinyInference
├── _internal/               # Private — do not import directly
│   ├── __init__.py
│   ├── constants.py         # Model names, task mappings, GA search space
│   ├── cleaner.py           # Text cleaning utilities
│   ├── corpus_gen.py        # Synthetic data generation (Ollama/Mistral)
│   ├── dataset.py           # list[dict] / JSONL → HuggingFace Dataset
│   ├── model_builder.py     # TinyBERT model instantiation
│   ├── trainer.py           # HuggingFace Trainer wrapper
│   ├── ga_optimizer.py      # Genetic algorithm hyperparameter search
│   ├── inference.py         # TinyInference class + save_best_model
│   └── pipeline.py          # optimize_slm() orchestrator
├── setup.py
└── README.md
```
