import json
import logging
import time

from fastapi import APIRouter, HTTPException

from app.schemas.prompt_generation import PromptGenerationImageModel, ImageRequest
from app.schemas.response_template import ResponseModel
from app.service.prompt_generation.chatgpt_for_translation import get_translation_from_llama3, \
    get_prompt_from_image

router = APIRouter()
logger = logging.getLogger()


@router.post("/translateToEN")
def prompt_generation(request_data: PromptGenerationImageModel):
    """
        翻译prompt接口
        创建一个具有以下参数的请求体:
        - **text**: 待翻译语句

        示例参数：
        {
          "text": "你好"
        }
    """
    try:
        logger.info(f"prompt_generation request item is : @@@@@@:{request_data}")
        data = get_translation_from_llama3(request_data.text)
        logger.info(f"prompt_generation response @@@@@@:{data}")
    except Exception as e:
        logger.warning(f"prompt_generation Run Exception @@@@@@:{e}")
        raise HTTPException(status_code=404, detail=str(e))
    return ResponseModel(data=data)


@router.post("/img2prompt")
def get_prompt_from_img(img: ImageRequest):
    """
    自动识别图片并输出为prompt

    :param img: 图片的minio地址
    :return: 图片的文字描述
    """
    text = ("Please describe the clothing in the image and provide a line art description of the outfit. "
            "The description should allow for the reconstruction of the corresponding line art based on the details "
            "given.")
    logger.info(f"get_prompt_from_img request item is : @@@@@@:{img}")
    description = get_prompt_from_image(img, text)
    logger.info(f"生成的图片描述 response @@@@@@:{description}")
    return description
