ai_service/README.md
2025-07-05 04:30:04 +08:00

4.1 KiB

基于多种 LLMs 组合以及 ComfyUI 参与的定制化用户海报生成的智能融合 ai 系统

概述

AI 海报生成系统集成 DeepSeek、Kimi、ComfyUI 三大 AI 模型,一键生成 Vue 组件代码和 PSD 文件。

核心功能:

  • 🤖 AI 驱动: DeepSeek 分析 + Kimi 文案 + ComfyUI 图片
  • 🎨 一键生成: 输入需求 → Vue 代码 + PSD 文件
  • 📱 REST API: 统一接口,支持前端集成
  • 🎭 预定义模板: lotus.jpg, nku.png, stamp.jpg, background.png
  • 🖼️ 自动合成: 多图层 PSD 文件生成

系统架构

前端 → FastAPI(8000) → AI模型集群
              ↓
         Vue代码 + PSD文件

快速开始

环境准备

# 创建虚拟环境
conda create -n ai_service python=3.11
conda activate ai_service

# 安装依赖
pip install -r requirements.txt

# 配置API密钥
cp .env.example .env
# 编辑 .env 文件,添加:
# DEEPSEEK_API_KEY=your_key
# MOONSHOT_API_KEY=your_key

启动服务

cd scripts
python run_pipeline.py  # 选择: 2 (API服务器)

验证服务

curl http://localhost:8000/health

API 使用

生成海报

curl -X POST http://localhost:8000/api/generate-poster \
  -H "Content-Type: application/json" \
  -d '{"user_input": "端午节海报,传统风格"}'

下载文件

curl "http://localhost:8000/api/download/psd?session_id=SESSION_ID" -o poster.psd
curl "http://localhost:8000/api/download/vue?session_id=SESSION_ID" -o poster.vue

项目结构

ai_service/
├── scripts/                 # 核心脚本
│   ├── run_pipeline.py      # API服务器
│   ├── utils.py            # 工具函数
│   ├── generate_layout.py  # Vue代码生成
│   ├── generate_text.py    # 文案生成
│   ├── flux_con.py         # 图片生成
│   └── export_psd_from_json.py # PSD合成
├── configs/                # 配置文件
│   ├── vue_templates.yaml  # Vue模板
│   └── font.yaml          # 字体配置
├── outputs/               # 输出目录
└── workflows/             # ComfyUI工作流

核心模块

模块 功能 AI 模型
utils.py 用户输入分析 DeepSeek
flux_con.py 图片生成 ComfyUI
generate_text.py 文案生成 Kimi
generate_layout.py Vue 代码生成 DeepSeek
export_psd_from_json.py PSD 合成 -

使用示例

JavaScript 调用

const response = await fetch("http://localhost:8000/api/generate-poster", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ user_input: "春节海报,红色背景" }),
});

const result = await response.json();
console.log("Vue代码:", result.data.vue_code);

Python 调用

import requests

response = requests.post('http://localhost:8000/api/generate-poster',
                        json={"user_input": "端午节海报,传统风格"})
result = response.json()

if result["status"] == "success":
    print("生成成功!")
    print("Vue代码:", result["data"]["vue_code"])

本地测试

cd scripts
python run_pipeline.py  # 选择: 1 (本地测试)
# 输入: "春节海报,红色背景,现代风格"

部署

Docker 部署

FROM python:3.11
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["uvicorn", "scripts.run_pipeline:app", "--host", "0.0.0.0", "--port", "8000"]

PM2 部署

pm2 start "uvicorn scripts.run_pipeline:app --host 0.0.0.0 --port 8000" --name poster-api

Nginx 反向代理

server {
    listen 80;
    location / {
        proxy_pass http://localhost:8000;
    }
}

故障排查

常见问题

# 检查API密钥
echo $DEEPSEEK_API_KEY

# 测试ComfyUI连接
curl http://101.201.50.90:8188/system_stats

# 检查端口占用
netstat -tulpn | grep 8000

更新日志

v 0.5.0(2025-07-05)

跑通全流程