增加 url ai 接口
This commit is contained in:
parent
36746a6cec
commit
680870bed3
@ -5,14 +5,40 @@ from app.api.deps import get_current_user
|
|||||||
from app.models.user import UserDB
|
from app.models.user import UserDB
|
||||||
import logging
|
import logging
|
||||||
from app.core.qcloud import qcloud_manager
|
from app.core.qcloud import qcloud_manager
|
||||||
|
from pydantic import BaseModel
|
||||||
router = APIRouter()
|
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)
|
@router.post("/extract_pickup_code", response_model=ResponseModel)
|
||||||
async def extract_pickup_code(
|
async def extract_pickup_code(
|
||||||
file: UploadFile = File(...)
|
file: UploadFile = File(...)
|
||||||
):
|
):
|
||||||
"""从图片中提取取件码"""
|
"""从图片中提取取件码"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 检查文件类型
|
# 检查文件类型
|
||||||
if not file.content_type.startswith('image/'):
|
if not file.content_type.startswith('image/'):
|
||||||
@ -22,7 +48,7 @@ async def extract_pickup_code(
|
|||||||
image_data = await file.read()
|
image_data = await file.read()
|
||||||
|
|
||||||
# 调用 AI 客户端提取取件码
|
# 调用 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:
|
if "error" in result:
|
||||||
return error_response(code=500, message=result.get("message", "提取取件码失败"))
|
return error_response(code=500, message=result.get("message", "提取取件码失败"))
|
||||||
|
|||||||
@ -12,7 +12,6 @@ router = APIRouter()
|
|||||||
@router.post("/image", response_model=ResponseModel)
|
@router.post("/image", response_model=ResponseModel)
|
||||||
async def upload_image(
|
async def upload_image(
|
||||||
file: UploadFile = File(...),
|
file: UploadFile = File(...),
|
||||||
current_user: UserDB = Depends(get_current_user)
|
|
||||||
):
|
):
|
||||||
"""上传单张图片"""
|
"""上传单张图片"""
|
||||||
if not file.content_type.startswith('image/'):
|
if not file.content_type.startswith('image/'):
|
||||||
@ -27,7 +26,6 @@ async def upload_image(
|
|||||||
@router.post("/images", response_model=ResponseModel)
|
@router.post("/images", response_model=ResponseModel)
|
||||||
async def upload_images(
|
async def upload_images(
|
||||||
files: List[UploadFile] = File(...),
|
files: List[UploadFile] = File(...),
|
||||||
current_user: UserDB = Depends(get_current_user)
|
|
||||||
):
|
):
|
||||||
"""上传多张图片"""
|
"""上传多张图片"""
|
||||||
if len(files) > 5:
|
if len(files) > 5:
|
||||||
|
|||||||
@ -12,7 +12,7 @@ class AIClient:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.timeout = 15 # 请求超时时间(秒)
|
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: 提取结果,包含取件码信息
|
Dict: 提取结果,包含取件码信息
|
||||||
"""
|
"""
|
||||||
try:
|
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):
|
if self._is_valid_result(primary_result):
|
||||||
@ -35,12 +35,12 @@ class AIClient:
|
|||||||
logging.exception(f"提取取件码异常: {str(e)}")
|
logging.exception(f"提取取件码异常: {str(e)}")
|
||||||
return {"error": "处理失败", "message": 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:
|
try:
|
||||||
# 添加超时控制
|
# 添加超时控制
|
||||||
return await asyncio.wait_for(
|
return await asyncio.wait_for(
|
||||||
qwen_client.extract_pickup_code(image_data),
|
qwen_client.extract_pickup_code(image_data, url),
|
||||||
timeout=self.timeout
|
timeout=self.timeout
|
||||||
)
|
)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
|
|||||||
@ -20,7 +20,7 @@ class QwenClient:
|
|||||||
self.api_key = settings.QWEN_API_KEY
|
self.api_key = settings.QWEN_API_KEY
|
||||||
self.model = "qwen-vl-max" # 使用千问视觉语言大模型
|
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]:
|
||||||
"""
|
"""
|
||||||
从图片中提取取件码
|
从图片中提取取件码
|
||||||
|
|
||||||
@ -31,9 +31,6 @@ class QwenClient:
|
|||||||
Dict: 提取结果,包含取件码信息
|
Dict: 提取结果,包含取件码信息
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# 将图片转换为 base64
|
|
||||||
image_base64 = base64.b64encode(imageData).decode('utf-8')
|
|
||||||
|
|
||||||
# 构建消息
|
# 构建消息
|
||||||
messages = [
|
messages = [
|
||||||
{
|
{
|
||||||
@ -49,7 +46,7 @@ class QwenClient:
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "image",
|
"type": "image",
|
||||||
"image": f"data:image/jpeg;base64,{image_base64}"
|
"image": f"data:image/jpeg;base64,{base64.b64encode(imageData).decode('utf-8')}" if imageData else url,
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user