|
1 | 1 | --- |
2 | 2 | --- |
3 | 3 |
|
| 4 | +<!-- TODO: refactor Python API as an unique document, (and redirect related chapters (like Quick_Start.md) to Python API document)--> |
4 | 5 | # API |
5 | 6 |
|
6 | | -CogKit provides a powerful inference API for generating images and videos using various AI models. This document covers both the Python API and API server. |
7 | 7 |
|
8 | | -## Python API |
| 8 | +<!-- TODO: List all supported oprations in Python API, rather than present as a demo --> |
| 9 | +## Python |
9 | 10 |
|
10 | | -You can also use `cogkit` programmatically in your Python code: |
| 11 | +We provide a Python API for CogKit, including load and inference related operations. |
11 | 12 |
|
12 | 13 | ```python |
13 | | -from cogkit.generation import generate_image, generate_video |
| 14 | +import torch |
| 15 | +from PIL import Image |
14 | 16 |
|
15 | | -# Text-to-Image generation |
16 | | -image = generate_image( |
17 | | - prompt="a beautiful sunset over mountains", |
18 | | - model_id_or_path="THUDM/CogView4-6B", |
19 | | - lora_model_id_or_path=None, |
| 17 | +from cogkit import ( |
| 18 | + load_pipeline, |
| 19 | + load_lora_checkpoint, |
| 20 | + unload_lora_checkpoint, |
| 21 | + |
| 22 | + generate_image, |
| 23 | + generate_video, |
| 24 | +) |
| 25 | +from diffusers.utils import export_to_video |
| 26 | + |
| 27 | + |
| 28 | +model_id_or_path = "THUDM/CogView4-6B" # t2i generation task, for example. |
| 29 | +pipeline = load_pipeline( |
| 30 | + model_id_or_path, |
20 | 31 | transformer_path=None, |
21 | | - output_file="sunset.png", # Images will be saved here. |
| 32 | + dtype=torch.bfloat16, |
| 33 | +) |
| 34 | + |
| 35 | +###### [Optional] Load/Unload LoRA weights |
| 36 | +# lora_model_id_or_path = "/path/to/lora/checkpoint" |
| 37 | +# load_lora_checkpoint(pipeline, lora_model_id_or_path) |
| 38 | +# ... |
| 39 | +# unload_lora_checkpoint(pipeline) |
| 40 | + |
| 41 | + |
| 42 | +###### Text-to-Image generation |
| 43 | +batched_image = generate_image( |
| 44 | + prompt="a beautiful sunset over mountains", |
| 45 | + pipeline=pipeline, |
22 | 46 | height=1024, |
23 | 47 | width=1024, |
| 48 | + output_type="pil", |
24 | 49 | ) |
| 50 | +batched_image[0].save("output.png") |
25 | 51 |
|
26 | 52 |
|
27 | | -# Text/Image-to-Video generation |
28 | | -video = generate_video( |
| 53 | +###### Text/Image-to-Video generation |
| 54 | +batched_video, fps = generate_video( |
29 | 55 | prompt="a cat playing with a ball", |
30 | | - image_file="path/to/image.png", # Needed for Image-to-Video task |
31 | | - model_id_or_path="THUDM/CogVideoX1.5-5B", |
32 | | - lora_model_id_or_path=None, |
33 | | - transformer_path=None, |
34 | | - output_file="cat.mp4", # Videos will be saved here. |
35 | | - num_frames=81, |
36 | | - fps=16, |
| 56 | + pipeline=pipeline, |
| 57 | + # input_image=Image.open("/path/to/image.png"), # only for i2v generation |
| 58 | + output_type="pil", |
37 | 59 | ) |
38 | | - |
| 60 | +export_to_video(batched_video[0], "output.mp4", fps=fps) |
39 | 61 | ``` |
40 | 62 |
|
41 | | -See function signatures in for more details. |
| 63 | +See function signatures for more details. |
| 64 | + |
| 65 | +<!-- TODO: Add documentation for API server endpoints --> |
| 66 | +## API Server Endpoints |
0 commit comments