37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
from fastapi import APIRouter, HTTPException, Request
|
||
from service import ImageDescribeService
|
||
from utils import jingrow_api_verify_and_billing
|
||
from settings import settings
|
||
import json
|
||
|
||
router = APIRouter(prefix=settings.router_prefix)
|
||
service = ImageDescribeService()
|
||
|
||
@router.post(settings.get_route)
|
||
@jingrow_api_verify_and_billing(api_name=settings.api_name)
|
||
async def describe_image_api(data: dict, request: Request):
|
||
"""
|
||
根据图像URL生成中英文描述
|
||
|
||
Args:
|
||
data: 包含以下字段的字典:
|
||
- image_url: 图片URL(必需)
|
||
- system_message: 自定义系统消息(可选)
|
||
- user_content: 自定义用户消息(可选)
|
||
request: FastAPI 请求对象
|
||
|
||
Returns:
|
||
图像的中英文描述
|
||
"""
|
||
if "image_url" not in data:
|
||
raise HTTPException(status_code=400, detail="缺少image_url参数")
|
||
|
||
# 如果提供了自定义消息,则更新service实例的消息
|
||
if "system_message" in data:
|
||
service.system_message = data["system_message"]
|
||
if "user_content" in data:
|
||
service.user_content = data["user_content"]
|
||
|
||
result = await service.describe_image(data["image_url"])
|
||
return result
|