japi/apps/jupscale/api.py
2025-05-12 02:39:56 +08:00

58 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 ImageUpscaleService
from utils import jingrow_api_verify_and_billing
from settings import settings
import json
import asyncio
import logging
import time
from typing import Optional
router = APIRouter(prefix=settings.router_prefix)
service = ImageUpscaleService()
@router.post(settings.upscale_route)
@jingrow_api_verify_and_billing(api_name=settings.api_name)
async def upscale_image_api(data: dict, request: Request):
"""
根据图像URL放大图像
Args:
data: 包含图像URL的字典
request: FastAPI 请求对象
Returns:
放大后的图片URL
"""
if "image_url" not in data:
raise HTTPException(status_code=400, detail="缺少image_url参数")
result = await service.upscale_image(data["image_url"])
return result
@router.post(settings.batch_route)
@jingrow_api_verify_and_billing(api_name=settings.api_name)
async def upscale_image_batch(data: dict, request: Request):
"""
批量处理多个图像URL
Args:
data: 包含图像URL列表的字典
request: FastAPI 请求对象
Returns:
流式响应包含每个图像的处理结果图片URL
"""
if "image_urls" not in data:
raise HTTPException(status_code=400, detail="缺少image_urls参数")
async def process_and_stream():
async for result in service.process_batch(data["image_urls"]):
yield json.dumps(result) + "\n"
return StreamingResponse(
process_and_stream(),
media_type="application/x-ndjson"
)