2025-05-12 02:39:56 +08:00

36 lines
1.2 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 service import TranslateService
from utils import jingrow_api_verify_and_billing
from settings import settings
router = APIRouter(prefix=settings.router_prefix)
service = TranslateService()
@router.post(settings.get_route)
@jingrow_api_verify_and_billing(api_name=settings.api_name)
async def translate_text_api(data: dict, request: Request):
"""
将中文文本翻译成英文
Args:
data: 包含以下字段的字典:
- source_text: 源文本(必需)
- system_message: 自定义系统消息(可选)
- user_content: 自定义用户消息(可选)
request: FastAPI 请求对象
Returns:
翻译后的英文文本
"""
if "source_text" not in data:
raise HTTPException(status_code=400, detail="缺少source_text参数")
# 如果提供了自定义消息则更新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.translate_text(data["source_text"])
return result