mirror of
https://github.com/KohakuBlueleaf/KohakuHub.git
synced 2026-03-09 07:12:07 -05:00
Merge pull request #9 from zdunecki/chore/json-schema-generator
chore(json-schema-generator): `generate_json_schema.py` script helpful for generating schemas for pydantic models
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -22,6 +22,7 @@ lint-reports/
|
||||
/logs
|
||||
data/
|
||||
**/public/docs
|
||||
__generated__
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
||||
@@ -539,6 +539,21 @@ export KOHAKU_HUB_BASE_URL=http://localhost:28080
|
||||
|
||||
---
|
||||
|
||||
## JSON Schema Generator
|
||||
|
||||
### Generate JSON Schema
|
||||
|
||||
```bash
|
||||
python scripts/generate_json_schema.py
|
||||
```
|
||||
|
||||
This script will generate a JSON schema for the KohakuHub types in `__generated__/schemas/<filename>.json`.
|
||||
|
||||
Currently supported files:
|
||||
- `config.json`
|
||||
|
||||
---
|
||||
|
||||
## Tips for Demo Deployments
|
||||
|
||||
### CloudFlare R2 Free Tier
|
||||
|
||||
30
scripts/generate_json_schema.py
Normal file
30
scripts/generate_json_schema.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import sys
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
# Add src to path so we can import kohakuhub
|
||||
src_path = Path(__file__).parent.parent / "src"
|
||||
sys.path.append(str(src_path))
|
||||
|
||||
from kohakuhub.config import Config
|
||||
|
||||
schema = Config.model_json_schema()
|
||||
|
||||
# Write to file
|
||||
output_dir = Path(__file__).parent.parent / "__generated__" / "schemas"
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
output_file = output_dir / "config.json"
|
||||
|
||||
def move_defs_to_end(schema: dict) -> dict:
|
||||
"""Move $defs to the end of the schema."""
|
||||
if '$defs' in schema:
|
||||
defs = schema.pop('$defs')
|
||||
schema['$defs'] = defs
|
||||
return schema
|
||||
|
||||
schema = move_defs_to_end(schema)
|
||||
|
||||
with open(output_file, "w") as f:
|
||||
json.dump(schema, f, indent=2)
|
||||
|
||||
print(f"Schema written to {output_file}")
|
||||
Reference in New Issue
Block a user