После нескольких попыток я продолжаю получать:
Код: Выделить всё
[image_generator] Exception: Attempted to access streaming request content, without having called read()
Вот используемая в настоящее время функция:
Код: Выделить всё
import httpx
import certifi
from js import console
async def simple_image_generator(api_key, params, timeout=1000):
if not api_key:
raise Exception("API key is required")
url = "https://api.stability.ai/v2beta/stable-image/generate/sd3"
headers = {
"Accept": "application/json, image/*",
"Authorization": f"Bearer {api_key}",
}
# Force multipart/form-data
files = []
for key, value in params.items():
files.append((key, (None, str(value))))
files.append(("none", ("empty.txt", b"", "application/octet-stream")))
async with httpx.AsyncClient(verify=certifi.where()) as client:
try:
console.info(f"\n[image_simple] Trying to POST {url}\n")
response = await client.post(
url,
headers=headers,
files=files,
timeout=timeout,
)
await response.aread()
response.raise_for_status()
console.info("\n[image_simple] API Request sent successfully\n")
return response
except Exception as e:
console.error(f"\n[image_simple] Exception: {str(e)}\n")
raise Exception(f"API request failed: {str(e)}") from e
< /code>
Пример Params: < /p>
params = {
"prompt": "a dragon flying over a futuristic city",
"mode": "text-to-image",
"model": "sd3",
"output_format": "png",
"aspect_ratio": "1:1"
}
[*] вызывающий await response.aread () перед Raise_for_status () с и без ожидания
[*] ucte-stream
Удаление потока = true (если не присутствует - was) < /li>
< /ul>
propect was was) < /li>
< /ul>
Отправить в конечную точку для стабильности /SD3 отправить фактический файл? /> Это работает в асинхронной среде Python (Pyodide), если это важно. < /p>
Любой рабочий минимальный пример или обходной путь!>
Подробнее здесь: https://stackoverflow.com/questions/796 ... -to-access