Files
dsProject/dsLightRag/Routes/JiMengRoute.py
2025-08-21 09:57:14 +08:00

148 lines
5.0 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.

import logging
import fastapi
from fastapi import APIRouter
from fastapi import HTTPException
from JiMeng.Kit.FenJingTouGenerator import FenJingTouGenerator # 导入分镜头生成器
from JiMeng.Kit.JmImg2VideoUtil import JmImg2Video # 导入视频生成工具
from JiMeng.Kit.JmTxt2ImgUtil import JmTxt2Img
# 创建路由路由器
router = APIRouter(prefix="/api/jimeng", tags=["即梦"])
# 配置日志
logger = logging.getLogger(__name__)
@router.post("/prompt_input")
async def prompt_input(request: fastapi.Request):
try:
data = await request.json()
prompt = data.get("prompt")
if not prompt:
raise HTTPException(status_code=400, detail="缺少提示词参数")
logger.info(f"收到图片生成请求,提示词: {prompt}")
# 调用 JmTxt2Img 生成图片
image_url = JmTxt2Img.generate_image(prompt)
logger.info(f"图片生成成功URL: {image_url}")
return {
"code": 200,
"message": "成功",
"data": {
"image_url": image_url
}
}
except HTTPException as e:
logger.error(f"请求参数错误: {str(e.detail)}")
raise e
except Exception as e:
logger.error(f"图片生成失败: {str(e)}")
raise HTTPException(status_code=500, detail=f"图片生成失败: {str(e)}")
@router.post("/generate_fenjingtou")
async def generate_fenjingtou(request: fastapi.Request):
try:
data = await request.json()
image_url = data.get("image_url")
prompt_text = data.get("prompt_text")
if not image_url:
raise HTTPException(status_code=400, detail="缺少图片地址参数")
if not prompt_text:
raise HTTPException(status_code=400, detail="缺少提示词参数")
logger.info(f"收到分镜头生成请求,图片地址: {image_url},提示词: {prompt_text}")
# 创建分镜头生成器实例并调用方法
generator = FenJingTouGenerator()
fenjingtou_content = generator.generate_fen_jing_tou(image_url, prompt_text)
if not fenjingtou_content:
raise HTTPException(status_code=500, detail="分镜头脚本生成失败")
logger.info(f"分镜头脚本生成成功")
return {
"code": 200,
"message": "成功",
"data": {
"fenjingtou_content": fenjingtou_content
}
}
except HTTPException as e:
logger.error(f"请求参数错误: {str(e.detail)}")
raise e
except Exception as e:
logger.error(f"分镜头脚本生成失败: {str(e)}")
raise HTTPException(status_code=500, detail=f"分镜头脚本生成失败: {str(e)}")
@router.post("/create_video_task")
async def create_video_task(request: fastapi.Request):
try:
data = await request.json()
image_url = data.get("image_url")
prompt = data.get("prompt")
if not image_url:
raise HTTPException(status_code=400, detail="缺少图片地址参数")
if not prompt:
raise HTTPException(status_code=400, detail="缺少分镜头脚本参数")
logger.info(f"收到视频任务创建请求,图片地址: {image_url},分镜头脚本: {prompt}")
# 创建视频生成实例并调用方法
img2video = JmImg2Video()
task_id = img2video.create_video_task([image_url], prompt)
logger.info(f"视频任务创建成功任务ID: {task_id}")
return {
"code": 200,
"message": "成功",
"data": {
"task_id": task_id
}
}
except HTTPException as e:
logger.error(f"请求参数错误: {str(e.detail)}")
raise e
except Exception as e:
logger.error(f"视频任务创建失败: {str(e)}")
raise HTTPException(status_code=500, detail=f"视频任务创建失败: {str(e)}")
@router.post("/query_video_task")
async def query_video_task(request: fastapi.Request):
try:
data = await request.json()
task_id = data.get("task_id")
if not task_id:
raise HTTPException(status_code=400, detail="缺少任务ID参数")
logger.info(f"收到视频任务查询请求任务ID: {task_id}")
# 创建视频生成实例并调用方法
img2video = JmImg2Video()
status_info = img2video.query_task_status(task_id)
logger.info(f"视频任务查询成功,状态: {status_info['status']}")
return {
"code": 200,
"message": "成功",
"data": status_info
}
except HTTPException as e:
logger.error(f"请求参数错误: {str(e.detail)}")
raise e
except Exception as e:
logger.error(f"视频任务查询失败: {str(e)}")
raise HTTPException(status_code=500, detail=f"视频任务查询失败: {str(e)}")