Taimwe commited on
Commit
3149a63
·
verified ·
1 Parent(s): 51e098a

Add README

Browse files
Files changed (1) hide show
  1. README.md +131 -7
README.md CHANGED
@@ -1,10 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- title: Trainhub Api
3
- emoji: 🌖
4
- colorFrom: green
5
- colorTo: pink
6
- sdk: docker
7
- pinned: false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # TrainHub API
2
+
3
+ A self-hosted AI training platform backend, modelled on the Hugging Face Hub.
4
+
5
+ **Stack:** FastAPI · SQLAlchemy (SQLite) · JWT auth · NumPy training worker · file storage · Hugging Face Hub bridge.
6
+
7
+ | Capability | Status |
8
+ |---|---|
9
+ | User registration / login (JWT) | ✅ real, persistent |
10
+ | Model / Dataset / Space repos (CRUD, likes) | ✅ real, persistent |
11
+ | File upload / list / download (disk + DB) | ✅ real |
12
+ | Training jobs (queue → run → metrics → complete) | ✅ real (NumPy backprop; swap in TRL for real models) |
13
+ | Hugging Face Hub search + model import | ✅ real (proxies hf.co) |
14
+ | Interactive API docs | ✅ `/docs` |
15
+
16
  ---
17
+
18
+ ## Run locally
19
+
20
+ ```bash
21
+ pip install -r requirements.txt
22
+ uvicorn app.main:app --reload --port 8000
23
+ # open http://localhost:8000/docs
24
+ ```
25
+
26
+ ## Run with Docker
27
+
28
+ ```bash
29
+ docker build -t trainhub-api .
30
+ docker run -p 7860:7860 -e SECRET_KEY=secret -v $(pwd)/storage:/app/storage trainhub-api
31
+ ```
32
+
33
+ ## Deploy to Hugging Face Spaces
34
+
35
+ This repo is a `docker`-SDK Space. Push to `main` and it builds automatically at
36
+ `https://huggingface.co/spaces/Taimwe/trainhub-api`.
37
+
38
+ > **Custom domain (you own a domain):** in the Space settings open **Settings → Custom
39
+ > domain**, add e.g. `api.yourdomain.com`, then in your DNS create a `CNAME` from
40
+ > `api.yourdomain.com` → `Taimwe-trainhub-api.hf.space` (or the Space's `.hf.space`
41
+ > hostname). For a root domain (`yourdomain.com`) you need an `ALIAS`/`ANAME` record or
42
+ > a proxy; refer to your DNS provider. Custom domains require a paid HF plan + an
43
+ > HF token set on the Space.
44
+
45
+ ## Environment variables
46
+
47
+ | Var | Default | Purpose |
48
+ |---|---|---|
49
+ | `SECRET_KEY` | `dev-secret-change-me` | JWT signing key — **change in prod** |
50
+ | `ACCESS_TOKEN_EXPIRE_MINUTES` | `1440` | token lifetime |
51
+ | `DATABASE_URL` | `sqlite:///./trainhub.db` | SQLAlchemy URL (Postgres works too) |
52
+ | `STORAGE_DIR` | `./storage` | where uploaded files land |
53
+ | `HF_TOKEN` | — | optional, raises Hub rate limits / enables private access |
54
+
55
  ---
56
 
57
+ ## API quick tour
58
+
59
+ Base URL: `http://localhost:8000` (or your Space/domain).
60
+
61
+ ### Auth
62
+ ```
63
+ POST /auth/register {"username","email","password"} -> {access_token, user}
64
+ POST /auth/login {"username","password"} -> {access_token, user}
65
+ ```
66
+ Send the token as `Authorization: Bearer <token>`.
67
+
68
+ ### Models / Datasets / Spaces
69
+ ```
70
+ POST /models {"name","description","pipeline_tag","tags","license"}
71
+ GET /models?search=&owner=
72
+ GET /models/{id}
73
+ PATCH /models/{id}
74
+ DELETE /models/{id}
75
+ POST /models/{id}/like
76
+
77
+ POST/GET/PATCH/DELETE /datasets (same pattern)
78
+ POST/GET/DELETE /spaces
79
+ ```
80
+
81
+ ### Files
82
+ ```
83
+ POST /files/model/{id} multipart "file" -> upload
84
+ GET /files/model/{id} -> list
85
+ GET /files/model/{id}/{filename} -> download (streamed)
86
+ ```
87
+ `dataset` works the same way (`/files/dataset/{id}`).
88
+
89
+ ### Training jobs
90
+ ```
91
+ POST /jobs {"base_model","dataset","config":{"epochs","lr","batch_size","seed"}}
92
+ GET /jobs
93
+ GET /jobs/{id}
94
+ GET /jobs/{id}/metrics -> [{epoch, loss}, ...] (loss genuinely decreases)
95
+ POST /jobs/{id}/cancel
96
+ ```
97
+ Jobs run in a background thread and persist status + loss history to the DB.
98
+
99
+ ### Hugging Face Hub bridge
100
+ ```
101
+ GET /hub/search?q=llama -> real models from hf.co
102
+ POST /hub/import {"repo_id"} -> import metadata into your local models
103
+ ```
104
+
105
+ ### Meta
106
+ ```
107
+ GET /health GET /stats GET /
108
+ ```
109
+
110
+ ---
111
+
112
+ ## Fine-tuning real models
113
+
114
+ The worker in `app/services/training.py` trains a small MLP with NumPy so the whole
115
+ stack runs anywhere with zero GPU. To fine-tune actual models, replace
116
+ `_run()` with a `transformers`/`TRL` call, e.g.:
117
+
118
+ ```python
119
+ from transformers import AutoModelForCausalLM, Trainer, TrainingArguments
120
+ model = AutoModelForCausalLM.from_pretrained(job.base_model)
121
+ trainer = Trainer(model=model, args=TrainingArguments(..., report_to=[]), train_dataset=ds)
122
+ for step in trainer.train(): # or a custom training loop
123
+ ... # write loss to job.metrics each step, db.commit()
124
+ ```
125
+ Point `base_model` at your HF model id (`Taimwe/...` or any public id) and add
126
+ `torch`/`trl` to `requirements.txt`.
127
+
128
+ ## Notes
129
+
130
+ - Single-process design (threads) is ideal for a self-hosted box. To scale to many
131
+ workers, replace `services/training.py` with a Celery/Redis or Kubernetes job runner
132
+ and keep the same REST contract.
133
+ - SQLite is the default for simplicity; set `DATABASE_URL` to Postgres for production.
134
+ - `CORS` is wide open for local frontends; tighten `allow_origins` in production.