File size: 2,566 Bytes
7a87926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# App Unification Summary

## Overview

Unified CLI and API into a single `app.py` entry point that can run in either mode depending on context.

## Structure

### `ylff/app.py`

- **CLI Application**: Imports Typer CLI from `cli.py` (lazy import)
- **API Application**: FastAPI app with all routers
- **Main Entry Point**: Detects context and runs appropriate mode

### Entry Points

#### CLI Mode (Default)

```bash
# Via module
python -m ylff validate sequence /path/to/sequence
python -m ylff train start /path/to/data

# Via command (if installed)
ylff validate sequence /path/to/sequence
ylff train start /path/to/data
```

#### API Mode

```bash
# Via module with --api flag
python -m ylff --api [--host 0.0.0.0] [--port 8000]

# Via uvicorn (recommended for production)
uvicorn ylff.app:api_app --host 0.0.0.0 --port 8000

# Via gunicorn
gunicorn ylff.app:api_app -w 4 -k uvicorn.workers.UvicornWorker
```

## Context Detection

The `main()` function detects the mode based on:

1. `--api` flag in command line arguments
2. `uvicorn` or `gunicorn` in `sys.argv[0]`
3. Default: CLI mode

## Backward Compatibility

- `ylff/cli.py` - Still exists, contains all CLI commands
- `ylff/api.py` - Still exists for backward compatibility (imports from app.py)
- `ylff/__main__.py` - Updated to use unified `main()` function
- Dockerfile - Updated to use `ylff.app:api_app`

## Benefits

1. **Single Entry Point**: One place to manage both CLI and API
2. **Context-Aware**: Automatically detects which mode to run
3. **Flexible**: Can run CLI or API from same codebase
4. **Backward Compatible**: Existing scripts and Docker configs still work

## Usage Examples

### CLI Commands

```bash
# Validation
python -m ylff validate sequence data/sequences/seq001
python -m ylff validate arkit data/arkit_recording

# Dataset building
python -m ylff dataset build data/raw_sequences --output-dir data/training

# Training
python -m ylff train start data/training --epochs 10
python -m ylff train pretrain data/arkit_sequences --epochs 5

# Evaluation
python -m ylff eval ba-agreement data/test --threshold 2.0

# Visualization
python -m ylff visualize data/validation_results
```

### API Server

```bash
# Development
python -m ylff --api

# Production
uvicorn ylff.app:api_app --host 0.0.0.0 --port 8000 --workers 4
```

## Files Changed

1. **ylff/app.py**: Unified entry point with CLI and API
2. **ylff/**main**.py**: Updated to use `main()` from app.py
3. **ylff/cli.py**: Updated imports to use new structure
4. **Dockerfile**: Updated CMD to use `ylff.app:api_app`