增加 url ai 接口

This commit is contained in:
aaron 2025-03-08 12:12:29 +08:00
parent 36746a6cec
commit 680870bed3
4 changed files with 36 additions and 15 deletions

View File

@ -5,14 +5,40 @@ from app.api.deps import get_current_user
from app.models.user import UserDB
import logging
from app.core.qcloud import qcloud_manager
from pydantic import BaseModel
router = APIRouter()
class ExtractPickupCodeRequest(BaseModel):
url: str
@router.post("/extract_pickup_code_with_url", response_model=ResponseModel)
async def extract_pickup_code_with_url(
data: ExtractPickupCodeRequest
):
"""从图片 URL 中提取取件码"""
if not data.url:
return error_response(code=400, message="URL 不能为空")
try:
# 调用 AI 客户端提取取件码
result = await ai_client.extract_pickup_code(url=data.url)
if "error" in result:
return error_response(code=500, message=result.get("message", "提取取件码失败"))
# 返回原始数据和格式化文本
return success_response(data=result)
except Exception as e:
logging.exception(f"提取取件码失败: {str(e)}")
return error_response(code=500, message=f"提取取件码失败: {str(e)}")
@router.post("/extract_pickup_code", response_model=ResponseModel)
async def extract_pickup_code(
file: UploadFile = File(...)
):
"""从图片中提取取件码"""
try:
# 检查文件类型
if not file.content_type.startswith('image/'):
@ -22,7 +48,7 @@ async def extract_pickup_code(
image_data = await file.read()
# 调用 AI 客户端提取取件码
result = await ai_client.extract_pickup_code(image_data)
result = await ai_client.extract_pickup_code(image_data=image_data)
if "error" in result:
return error_response(code=500, message=result.get("message", "提取取件码失败"))

View File

@ -12,7 +12,6 @@ router = APIRouter()
@router.post("/image", response_model=ResponseModel)
async def upload_image(
file: UploadFile = File(...),
current_user: UserDB = Depends(get_current_user)
):
"""上传单张图片"""
if not file.content_type.startswith('image/'):
@ -27,7 +26,6 @@ async def upload_image(
@router.post("/images", response_model=ResponseModel)
async def upload_images(
files: List[UploadFile] = File(...),
current_user: UserDB = Depends(get_current_user)
):
"""上传多张图片"""
if len(files) > 5:

View File

@ -12,7 +12,7 @@ class AIClient:
def __init__(self):
self.timeout = 15 # 请求超时时间(秒)
async def extract_pickup_code(self, image_data: bytes) -> Dict[str, Any]:
async def extract_pickup_code(self, image_data: bytes = None, url: str = None) -> Dict[str, Any]:
"""
从图片中提取取件码
@ -23,7 +23,7 @@ class AIClient:
Dict: 提取结果包含取件码信息
"""
try:
primary_result = await self._extract_with_qwen(image_data)
primary_result = await self._extract_with_qwen(image_data, url)
# 检查结果是否有效
if self._is_valid_result(primary_result):
@ -35,12 +35,12 @@ class AIClient:
logging.exception(f"提取取件码异常: {str(e)}")
return {"error": "处理失败", "message": str(e)}
async def _extract_with_qwen(self, image_data: bytes) -> Dict[str, Any]:
async def _extract_with_qwen(self, image_data: bytes = None, url: str = None) -> Dict[str, Any]:
"""使用千问提取取件码"""
try:
# 添加超时控制
return await asyncio.wait_for(
qwen_client.extract_pickup_code(image_data),
qwen_client.extract_pickup_code(image_data, url),
timeout=self.timeout
)
except asyncio.TimeoutError:

View File

@ -20,7 +20,7 @@ class QwenClient:
self.api_key = settings.QWEN_API_KEY
self.model = "qwen-vl-max" # 使用千问视觉语言大模型
async def extract_pickup_code(self, imageData: bytes) -> Dict[str, Any]:
async def extract_pickup_code(self, imageData: bytes = None, url: str = None) -> Dict[str, Any]:
"""
从图片中提取取件码
@ -30,10 +30,7 @@ class QwenClient:
Returns:
Dict: 提取结果包含取件码信息
"""
try:
# 将图片转换为 base64
image_base64 = base64.b64encode(imageData).decode('utf-8')
try:
# 构建消息
messages = [
{
@ -48,8 +45,8 @@ class QwenClient:
"text": "请识别图中驿站的所有取件码,以[{\"station\":\"驿站名字\",\"pickup_codes\":[\"3232\",\"2323\"]}]的格式返回。只返回JSON格式数据不要其他解释。"
},
{
"type": "image",
"image": f"data:image/jpeg;base64,{image_base64}"
"type": "image",
"image": f"data:image/jpeg;base64,{base64.b64encode(imageData).decode('utf-8')}" if imageData else url,
}
]
}