|
4 | 4 | <!-- TODO: check this doc --> |
5 | 5 | # Command-Line Interface |
6 | 6 |
|
7 | | -`cogkit` provides a powerful command-line interface (CLI) that allows you to perform various tasks without writing Python code. This guide covers the available commands and their usage. |
| 7 | +CogKit provides a powerful command-line interface (CLI) that allows you to perform various tasks without writing Python code. This guide covers the available commands and their usage. |
8 | 8 |
|
9 | 9 | ## Overview |
10 | 10 |
|
@@ -45,15 +45,70 @@ See `cogkit inference --help` for more information. |
45 | 45 | <!-- TODO: add docs for launch server --> |
46 | 46 | ## Launch Command |
47 | 47 |
|
48 | | -The `launch` command will starts a API server: |
| 48 | +The `launch` command starts an API server for image and video generation. Before using this command, you need to install the API dependencies: |
49 | 49 |
|
50 | | -<!-- FIXME: Add examples --> |
51 | 50 | ```bash |
52 | | -... |
| 51 | +pip install "cogkit[api]@git+https://github.com/THUDM/cogkit.git" |
53 | 52 | ``` |
54 | 53 |
|
55 | | -Please refer to [API](./02-API.md#api-server) for details on how to interact with the API server using client interfaces. |
| 54 | +<!-- FIXME: correct url --> |
| 55 | +Before starting the server, make sure to configure the model paths that you want to serve. This step is necessary to specify which models will be available through the API server. |
| 56 | + |
| 57 | +To configure the model paths: |
| 58 | + |
| 59 | +1. Create a `.env` file in your working directory |
| 60 | +2. Refer to the [environment template]() and add needed environment variables to specify model paths. For example, to serve `CogView4-6B` as a service, you must specify `COGVIEW4_PATH` in your `.env` file: |
| 61 | + |
| 62 | + ```bash |
| 63 | + # /your/workdir/.env |
| 64 | + |
| 65 | + COGVIEW4_PATH="THUDM/CogView4-6B" # or local path |
| 66 | + # other variables... |
| 67 | + ``` |
| 68 | + |
| 69 | +Then starts a API server, for example: |
| 70 | + |
| 71 | +```bash |
| 72 | +cogkit launch |
| 73 | +``` |
56 | 74 |
|
57 | 75 | :::tip |
58 | 76 | See `cogkit launch --help` for more information. |
59 | 77 | ::: |
| 78 | + |
| 79 | + |
| 80 | +### Client Interfaces |
| 81 | + |
| 82 | +The server API is OpenAI-compatible, which means you can use it with any OpenAI client library. Here's an example using the OpenAI Python client: |
| 83 | +
|
| 84 | +```python |
| 85 | +import base64 |
| 86 | +
|
| 87 | +from io import BytesIO |
| 88 | +from PIL import Image |
| 89 | +
|
| 90 | +from openai import OpenAI |
| 91 | +
|
| 92 | +client = OpenAI( |
| 93 | + api_key="foo", |
| 94 | + base_url="http://localhost:8000/v1" # Your server URL |
| 95 | +) |
| 96 | +
|
| 97 | +# Generate an image from cogview-4 |
| 98 | +response = client.images.generate( |
| 99 | + model="cogview-4", |
| 100 | + prompt="a beautiful sunset over mountains", |
| 101 | + n=1, |
| 102 | + size="1024x1024", |
| 103 | +) |
| 104 | +image_b64 = response.data[0].b64_json |
| 105 | +
|
| 106 | +# Decode the base64 string |
| 107 | +image_data = base64.b64decode(image_b64) |
| 108 | +
|
| 109 | +# Create an image from the decoded data |
| 110 | +image = Image.open(BytesIO(image_data)) |
| 111 | +
|
| 112 | +# Save the image |
| 113 | +image.save("output.png") |
| 114 | +``` |
0 commit comments