| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import logging
- import os
- import uuid
- from typing import List
- from fastapi import APIRouter, UploadFile, File, Form, HTTPException
- import shutil
- from starlette.concurrency import run_in_threadpool
- from setting import UPLOAD_DIR
- import service.ai_image_ollama as ai_image
- logger = logging.getLogger(__name__)
- # 创建路由对象,可以统一设置前缀 (prefix) 和 标签 (tags)
- router = APIRouter(
- prefix="/api1/image",
- tags=["video"]
- )
- @router.post("/analyze")
- async def upload_image(
- file: UploadFile = File(...),
- prompts: List[str] = Form(...)
- ):
- if not file:
- raise HTTPException(status_code=400, detail="文件不能为空")
- if not prompts:
- raise HTTPException(status_code=400, detail="Prompts 不能为空")
- task_id = str(uuid.uuid4())[:8]
- ext = file.filename.split('.')[-1]
- save_filename = f"{task_id}.{ext}"
- save_path = os.path.join(UPLOAD_DIR, save_filename)
- # 异步保存文件
- def save_file():
- with open(save_path, "wb") as buffer:
- shutil.copyfileobj(file.file, buffer)
- await run_in_threadpool(save_file)
- try:
- # 读取字节流进行 AI 处理
- with open(save_path, 'rb') as f:
- image_bytes = f.read()
- result = ai_image.describe_image(prompts, image_bytes)
- return {
- "task_id": task_id,
- "model_name": result['model_name'],
- "image_url": f"/api1/file/image/{save_filename}",
- "results": result['results']
- }
- except Exception as e:
- # 记录日志并抛出错误
- raise HTTPException(status_code=500, detail=f"AI 推理失败: {str(e)}")
|