Working with the YandexART API in Python
After you have logged in/created an account, go to the console https://console.yandex.cloud.
Create a payment account and top it up with any amount (I put in 25 rubles).
After creating a payment account, go to the “Service accounts” page.
Next, click on the “Create a service account” button.
Enter any name, you don't need to add a description.
Adding a role to the ai.imageGeneration.user catalog
After selecting, click the “Create” button.
After creation, click on the “Create new key” button.
In the pop-up menu, select “Create API Key”.
Enter a description and then click “Create”.
After creation, copy the key identifier and the secret key itself, BE SURE TO SAVE IT!
Now the most interesting part is the script for generation.
For the smart ones I will leave the script right away, for dummies I will explain what and how.
import requests
import base64
import time
def yandex_art_request(prompt, seed):
prompt = {
"modelUri": "art://<ID Каталога>/yandex-art/latest",
"generationOptions": {
"seed": seed,
"aspectRatio": {
"widthRatio": "1",
"heightRatio": "1"
}
},
"messages": [
{
"weight": "1",
"text": prompt
}
]
}
headers = {
"Content-Type": "application/json",
"Authorization": "Api-key <Секретный ключ>"
}
create_request = requests.post('https://llm.api.cloud.yandex.net/foundationModels/v1/imageGenerationAsync', headers=headers, json=prompt)
while True:
time.sleep(5)
done_request = requests.get(f'https://llm.api.cloud.yandex.net:443/operations/{create_request.json()["id"]}', headers=headers)
if done_request.json()['done'] == True:
with open(create_request.json()['id'] + '.jpeg', 'wb') as file:
file.write(base64.b64decode(done_request.json()['response']['image']))
break
return create_request.json()['id'] + '.jpeg'
Now I'll explain for dummies what we did in the code.
One of the most important is prompt (request to the neural network).
prompt = {
"modelUri": "art://<ID Каталога>/yandex-art/latest",
"generationOptions": {
"seed": seed,
"aspectRatio": {
"widthRatio": "1",
"heightRatio": "1"
}
},
"messages": [
{
"weight": "1",
"text": prompt
}
]
}
Instead of
Now the explanation is first we specify the model url in the “modelUri” key.
Afterwards we specify the generation settings
seed that is the grain of generation, I generate it randomly
The aspectratio parameter, that is, the resolution of the image that the neural network will produce
messages — these are messages for the neural network, in our prompt only 1 message with the request text is specified
Now let's move on to authorization:
headers = {
"Content-Type": "application/json",
"Authorization": "Api-key <Секретный ключ>"
}
In the “Authorization” key, we specify our secret API key that we received earlier in the format “Api-key
Well, the most interesting thing is the beginning of generation.
To start generation we need to send a request with a prompt.
create_request = requests.post('https://llm.api.cloud.yandex.net/foundationModels/v1/imageGenerationAsync', headers=headers, json=prompt)
After sending a request to start generation, we start checking the generation, that is, we send a request every 5 seconds, if it is ready, we write it to the jpeg file.
while True:
time.sleep(5)
done_request = requests.get(f'https://llm.api.cloud.yandex.net:443/operations/{create_request.json()["id"]}', headers=headers)
if done_request.json()['done'] == True: # Проверка готово ли изображение
with open(create_request.json()['id'] + '.jpeg', 'wb') as file: # Если изображение готово то записываем картинку в файл
file.write(base64.b64decode(done_request.json()['response']['image'])) # Расшифровываем изображение и записываем в файл
break
After the generation is complete, we return the name of the created image to the folder with the code.
return create_request.json()['id'] + '.jpeg'
Full code:
import requests
import base64
import time
def yandex_art_request(prompt, seed):
prompt = {
"modelUri": "art://<ID Каталога>/yandex-art/latest",
"generationOptions": {
"seed": seed,
"aspectRatio": {
"widthRatio": "1",
"heightRatio": "1"
}
},
"messages": [
{
"weight": "1",
"text": prompt
}
]
}
headers = {
"Content-Type": "application/json",
"Authorization": "Api-key <Секретный ключ>"
}
create_request = requests.post('https://llm.api.cloud.yandex.net/foundationModels/v1/imageGenerationAsync', headers=headers, json=prompt)
while True:
time.sleep(5)
done_request = requests.get(f'https://llm.api.cloud.yandex.net:443/operations/{create_request.json()["id"]}', headers=headers)
if done_request.json()['done'] == True:
with open(create_request.json()['id'] + '.jpeg', 'wb') as file:
file.write(base64.b64decode(done_request.json()['response']['image']))
break
return create_request.json()['id'] + '.jpeg'
I hope this article will help someone, if you have any questions – write them in the comments, I will try to answer them all.