58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from fastapi import APIRouter, HTTPException, Request
|
|
from fastapi.responses import StreamingResponse
|
|
from service import TxtImgService
|
|
from utils import jingrow_api_verify_and_billing
|
|
from settings import settings
|
|
import json
|
|
import asyncio
|
|
|
|
router = APIRouter(prefix=settings.router_prefix)
|
|
service = TxtImgService()
|
|
|
|
@router.post(settings.generate_route)
|
|
@jingrow_api_verify_and_billing(api_name=settings.api_name)
|
|
async def generate_image(data: dict, request: Request):
|
|
"""
|
|
根据文本提示生成图像
|
|
|
|
Args:
|
|
data: 包含文本提示和配置参数的字典
|
|
request: FastAPI 请求对象
|
|
|
|
Returns:
|
|
生成的图像内容
|
|
"""
|
|
if "prompt" not in data:
|
|
raise HTTPException(status_code=400, detail="缺少prompt参数")
|
|
|
|
config = data.get("config", {})
|
|
result = await service.generate_image(data["prompt"], config)
|
|
return result
|
|
|
|
@router.post(settings.batch_route)
|
|
@jingrow_api_verify_and_billing(api_name=settings.api_name)
|
|
async def generate_image_batch(data: dict, request: Request):
|
|
"""
|
|
批量处理多个文本提示
|
|
|
|
Args:
|
|
data: 包含文本提示列表和配置参数的字典
|
|
request: FastAPI 请求对象
|
|
|
|
Returns:
|
|
流式响应,包含每个提示的处理结果
|
|
"""
|
|
if "prompts" not in data:
|
|
raise HTTPException(status_code=400, detail="缺少prompts参数")
|
|
|
|
config = data.get("config", {})
|
|
|
|
async def process_and_stream():
|
|
async for result in service.process_batch(data["prompts"], config):
|
|
yield json.dumps(result) + "\n"
|
|
|
|
return StreamingResponse(
|
|
process_and_stream(),
|
|
media_type="application/x-ndjson"
|
|
)
|