51 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import StreamingResponse
from service import MidjourneyService
from utils import jingrow_api_verify_and_billing
from settings import settings
import json
import asyncio
from typing import AsyncGenerator, List
router = APIRouter(prefix=settings.router_prefix)
service = MidjourneyService()
@router.post(settings.generate_route)
@jingrow_api_verify_and_billing(api_name=settings.api_name)
async def ve_generate_image(data: dict, request: Request):
"""
使用 VectorEngine API 生成图片
Args:
data: 请求数据,包含:
- prompt: 提示词(必需)
- config: 配置参数(可选)
- base64_array: base64 编码的图片数组
- notify_hook: 回调地址
- state: 自定义状态
- bot_type: 机器人类型(默认 MID_JOURNEY
- split_image: 是否分割图片(默认 True
Returns:
流式响应,包含生成状态和结果
"""
if "prompt" not in data:
raise HTTPException(status_code=400, detail="缺少prompt参数")
prompt = data["prompt"]
config = data.get("config", {})
async def generate() -> AsyncGenerator[str, None]:
async for result in service.ve_generate_image(prompt, config):
yield json.dumps(result, ensure_ascii=False) + "\n"
return StreamingResponse(
generate(),
media_type="application/x-ndjson",
headers={
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
"X-Content-Type-Options": "nosniff"
}
)