# π§ AI Lab β Transformers CLI Playground
> A **pedagogical and technical project** designed for AI practitioners and students to explore **Hugging Face Transformers** through an **interactive Command-Line Interface (CLI)** or a **REST API**.
> This playground provides ready-to-use NLP pipelines β including **Sentiment Analysis**, **Named Entity Recognition**, **Text Generation**, **Fill-Mask**, **Question Answering (QA)**, **Moderation**, and more β in a modular, extensible, and educational codebase.
---
---
## π Table of Contents
- [π Overview](#-overview)
- [ποΈ Project Structure](#οΈ-project-structure)
- [βοΈ Installation](#οΈ-installation)
- [π§Ύ Option 1 β Poetry (Recommended)](#-option-1--poetry-recommended)
- [π¦ Option 2 β Pip + Requirements](#-option-2--pip--requirements)
- [βΆοΈ Usage](#οΈ-usage)
- [π₯οΈ CLI Mode](#οΈ-cli-mode)
- [π API Mode](#-api-mode)
- [π‘ API Endpoints](#-api-endpoints)
- [π₯οΈ CLI Examples](#οΈ-cli-examples)
- [π§ Architecture Overview](#-architecture-overview)
- [βοΈ Configuration](#οΈ-configuration)
- [π§© Extending the Playground](#-extending-the-playground)
- [π§° Troubleshooting](#-troubleshooting)
- [π§ Development Guidelines](#-development-guidelines)
- [π§± Roadmap](#-roadmap)
- [π License](#-license)
---
## π Overview
The **AI Lab β Transformers CLI Playground** enables users to explore **multiple NLP tasks directly from the terminal or via HTTP APIs**.
Each task (sentiment, NER, text generation, etc.) is implemented as a **Command Module** that communicates with a **Pipeline Module** powered by Hugging Faceβs `transformers` library.
The project demonstrates **clean ML code architecture** with strict separation between:
- Configuration
- Pipelines
- CLI logic
- Display formatting
Itβs a great educational resource for learning **how to structure ML applications** professionally.
---
## ποΈ Project Structure
```text
src/
βββ main.py # CLI entry point
β
βββ cli/
β βββ base.py # CLICommand base class & interactive shell
β βββ display.py # Console formatting utilities (colors, tables, results)
β
βββ commands/ # User-facing commands wrapping pipeline logic
β βββ sentiment.py # Sentiment analysis command
β βββ fillmask.py # Masked token prediction
β βββ textgen.py # Text generation
β βββ ner.py # Named Entity Recognition
β βββ qa.py # Question Answering (extractive)
β βββ moderation.py # Content moderation / toxicity detection
β
βββ pipelines/ # ML logic based on Hugging Face pipelines
β βββ template.py # Blueprint for creating new pipelines
β βββ sentiment.py
β βββ fillmask.py
β βββ textgen.py
β βββ ner.py
β βββ qa.py
β βββ moderation.py
β
βββ api/
β βββ app.py # FastAPI app and endpoints
β βββ models.py # Pydantic schemas
β βββ config.py # API configuration
β
βββ config/
βββ settings.py # Global configuration (models, params)
```
---
## βοΈ Installation
### π§Ύ Option 1 β Poetry (Recommended)
> Poetry is the main dependency manager for this project.
```bash
poetry shell
poetry install
```
This installs all dependencies defined in `pyproject.toml` (including `transformers`, `torch`, and `fastapi`).
Run the app:
```bash
# CLI mode
poetry run python src/main.py --mode cli
# API mode
poetry run python src/main.py --mode api
```
---
### π¦ Option 2 β Pip + requirements.txt
If you prefer manual dependency management:
```bash
python -m venv .venv
source .venv/bin/activate # Linux/macOS
.venv\Scripts\Activate.ps1 # Windows
pip install -r requirements.txt
```
---
## βΆοΈ Usage
### π₯οΈ CLI Mode
Run the interactive CLI:
```bash
python -m src.main --mode cli
```
Interactive menu:
```
Welcome to AI Lab - Transformers CLI Playground
Available commands:
β’ sentiment β Analyze the sentiment of a text
β’ fillmask β Predict masked words in a sentence
β’ textgen β Generate text from a prompt
β’ ner β Extract named entities from text
β’ qa β Answer questions from a context
β’ moderation β Detect toxic or unsafe content
```
---
### π API Mode
Run FastAPI server:
```bash
python -m src.main --mode api
# Custom config
python -m src.main --mode api --host 0.0.0.0 --port 8000 --reload
```
API Docs:
- **Swagger** β http://localhost:8000/docs
- **ReDoc** β http://localhost:8000/redoc
- **OpenAPI** β http://localhost:8000/openapi.json
---
## π‘ API Endpoints
### Core Endpoints
| Method | Endpoint | Description |
| ------ | --------- | ------------------------- |
| `GET` | `/` | Health check and API info |
| `GET` | `/health` | Detailed health status |
### Individual Processing
| Method | Endpoint | Description |
| ------ | ------------- | ---------------------- |
| `POST` | `/sentiment` | Analyze text sentiment |
| `POST` | `/fillmask` | Predict masked words |
| `POST` | `/textgen` | Generate text |
| `POST` | `/ner` | Extract named entities |
| `POST` | `/qa` | Question answering |
| `POST` | `/moderation` | Content moderation |
### Batch Processing
| Method | Endpoint | Description |
| ------ | ------------------- | -------------------------- |
| `POST` | `/sentiment/batch` | Process multiple texts |
| `POST` | `/fillmask/batch` | Fill multiple masked texts |
| `POST` | `/textgen/batch` | Generate from prompts |
| `POST` | `/ner/batch` | Extract entities in batch |
| `POST` | `/qa/batch` | Answer questions in batch |
| `POST` | `/moderation/batch` | Moderate multiple texts |
---
## π₯οΈ CLI Examples
### πΉ Sentiment Analysis
```text
π¬ Enter text: I absolutely love this project!
β Sentiment: POSITIVE (score: 0.998)
```
### πΉ Fill-Mask
```text
π¬ Enter text: The capital of France is [MASK].
β Predictions:
1) Paris score: 0.87
2) Lyon score: 0.04
```
### πΉ Text Generation
```text
π¬ Prompt: Once upon a time
β Output: Once upon a time there was a young AI learning to code...
```
### πΉ NER
```text
π¬ Enter text: Elon Musk founded SpaceX in California.
β Entities:
- Elon Musk (PERSON)
- SpaceX (ORG)
- California (LOC)
```
### πΉ QA (Question Answering)
```text
π¬ Enter question: What is the capital of France?
π¬ Enter context: France is a country in Europe. Its capital is Paris.
β Answer: The capital of France is Paris.
```
### πΉ Moderation
```text
π¬ Enter text: I hate everything!
β Result: FLAGGED (toxic content detected)
```
---
## π§ Architecture Overview
Both CLI and API share the **same pipeline layer**, ensuring code reusability and consistency.
### CLI Architecture
```text
InteractiveCLI β Command Layer β Pipeline Layer β Display Layer
```
### API Architecture
```text
FastAPI App β Pydantic Models β Pipeline Layer β JSON Response
```
| Layer | Description |
| ------------ | ---------------------------------------------- |
| **CLI** | Manages user input/output and navigation. |
| **API** | Exposes endpoints with automatic OpenAPI docs. |
| **Command** | Encapsulates user-facing operations. |
| **Pipeline** | Wraps Hugging Faceβs pipelines. |
| **Models** | Validates requests/responses. |
| **Display** | Formats console output. |
---
## βοΈ Configuration
All configuration is centralized in `src/config/settings.py`:
```python
class Config:
DEFAULT_MODELS = {
"sentiment": "distilbert-base-uncased-finetuned-sst-2-english",
"fillmask": "bert-base-uncased",
"textgen": "gpt2",
"ner": "dslim/bert-base-NER",
"qa": "distilbert-base-cased-distilled-squad",
"moderation":"unitary/toxic-bert",
}
MAX_LENGTH = 512
BATCH_SIZE = 8
```
---
## π§© Extending the Playground
To add a new NLP experiment (e.g., keyword extraction):
1. Duplicate `src/pipelines/template.py` β `src/pipelines/keywords.py`
2. Create a command: `src/commands/keywords.py`
3. Register it in `src/main.py`
4. Add Pydantic models and API endpoint
5. Update `Config.DEFAULT_MODELS`
Both CLI and API will automatically share this logic.
---
## π§° Troubleshooting
| Issue | Solution |
| ------------------------ | ----------------------- |
| `transformers` not found | Activate your venv. |
| Torch install fails | Use CPU-only wheel. |
| Models download slowly | Cached after first use. |
| Encoding issues | Ensure UTF-8 terminal. |
### API Issues
| Issue | Solution |
| -------------------- | --------------------------------------- |
| `FastAPI` missing | `pip install fastapi uvicorn[standard]` |
| Port in use | Change with `--port 8001` |
| CORS error | Edit `allow_origins` in `api/config.py` |
| Validation error 422 | Check request body |
| 500 error | Verify model loading |
---
## π§ Development Guidelines
- Keep command classes lightweight (no ML inside)
- Use the pipeline template for new tasks
- Format all outputs via `DisplayFormatter`
- Document new commands and models
---
## π§± Roadmap
- [ ] Non-interactive CLI flags (`--text`, `--task`)
- [ ] Multilingual models
- [ ] Test coverage
- [ ] Logging & profiling
- [ ] Export to JSON/CSV
---
## π License
Licensed under the [MIT License](./LICENSE).
You are free to use, modify, and distribute this project.
---
β¨ **End of Documentation**
_The AI Lab β Transformers CLI Playground: built for learning, experimenting, and sharing NLP excellence._