From 5eec48a889658bfe48cd7dc6b22d60935b8084e0 Mon Sep 17 00:00:00 2001 From: Wang Xiuqiang Date: Tue, 3 Jun 2025 20:44:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=BA=86=E6=96=87=E6=A1=88?= =?UTF-8?q?=E5=92=8C=E5=AF=BC=E5=87=BApsd=E6=A0=BC=E5=BC=8F=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=B8=BA=E5=90=8E=E7=BB=AD=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E5=81=9A=E5=87=86=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- outputs/generated_code.jsx | 47 -------------------------------------- outputs/generated_code.vue | 45 ++++++++++++++++++++++++++++++++++++ requirements.txt | 11 +++++++++ scripts/generate_layout.py | 30 ++++++++---------------- 4 files changed, 65 insertions(+), 68 deletions(-) delete mode 100644 outputs/generated_code.jsx create mode 100644 outputs/generated_code.vue create mode 100644 requirements.txt diff --git a/outputs/generated_code.jsx b/outputs/generated_code.jsx deleted file mode 100644 index 6f04287..0000000 --- a/outputs/generated_code.jsx +++ /dev/null @@ -1,47 +0,0 @@ -```jsx -import React from 'react'; - -const DragonBoatFestivalPoster = () => { - return ( -
- {/* 背景图层 */} -
- - {/* 主体图层 */} -
-

-

-
- - {/* 活动亮点 */} -
-
-
-
-

-

-
-
-
-

-

-
-
-
-

-

-
-
-
- - {/* 页脚 */} -
-

-
-
-
- ); -}; - -export default DragonBoatFestivalPoster; -``` \ No newline at end of file diff --git a/outputs/generated_code.vue b/outputs/generated_code.vue new file mode 100644 index 0000000..6678248 --- /dev/null +++ b/outputs/generated_code.vue @@ -0,0 +1,45 @@ + + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2cc43e7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,11 @@ +# 基础依赖 +python-dotenv # 用于加载.env文件中的环境变量 +pyyaml # 用于解析YAML配置文件 + +# API客户端 +openai == 1.82.1 # 用于调用OpenAI/Moonshot/DeepSeek API + +# 图像处理 +Pillow # PIL库,用于基础图像处理 +psd-tools # 用于PSD文件的创建和操作 + diff --git a/scripts/generate_layout.py b/scripts/generate_layout.py index 56906c4..8bea5d3 100644 --- a/scripts/generate_layout.py +++ b/scripts/generate_layout.py @@ -2,8 +2,6 @@ import os from openai import OpenAI from dotenv import load_dotenv import time -import logging - # === Config LLM call === load_dotenv() @@ -12,12 +10,6 @@ deepseek_api = os.getenv("DEEPSEEK_API_KEY") if not deepseek_api: raise ValueError("DEEPSEEK_API_KEY not set!") -# 配置日志 -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - - -# === prompts and parameters === def call_deepseek( messages=None, system_prompt="你是一个擅长前端开发的AI,专注于生成Vue.js代码。", @@ -99,37 +91,33 @@ def call_deepseek( return content, response.usage except Exception as e: - if hasattr(e, 'status_code') and e.status_code == 429: # 限流 - logger.warning(f"请求过于频繁,正在重试... (尝试 {attempt + 1}/{max_retries})") + if hasattr-------------('status_code') and e.status_code == 429: # 限流 + print(f"请求过于频繁,正在重试... (尝试 {attempt + 1}/{max_retries})") time.sleep(2 ** attempt) # 指数退避 else: - logger.error(f"API 调用失败:{str(e)}") raise raise Exception("达到最大重试次数,API 调用失败") def generate_vue_code(prompt=None): prompt = ( - "生成一个React组件代码,用于端午节活动海报,包含以下部分并指定排版位置:" + "生成一个Vue组件代码,用于端午节活动海报,包含以下部分并指定排版位置:" "1. 背景图层:div,占据整个组件区域。" "2. 主体图层:div,位于顶部1/4处,居中,包含标题和副标题。" "3. 活动亮点:div,位于底部1/4处,居中,使用网格布局展示三项活动(每项包含图标、标题和描述)。" "4. 页脚:div,位于底部,居中,包含主办单位信息和logo图片。" "组件尺寸为1080x1920px,布局使用absolute定位,仅关注排版位置,不包含任何样式描述(如颜色、字体、阴影、动画等)。" - "仅生成React代码本身,不包含说明性文字、注释或Markdown格式。" + "仅生成Vue代码本身,不包含说明性文字、注释或Markdown格式。" ) system_prompt = ( - "你是一个擅长前端开发的AI,专注于生成React.js代码。" + "你是一个擅长前端开发的AI,专注于生成Vue.js代码。" "生成的代码仅关注排版位置,使用absolute定位,不包含任何样式描述(如颜色、字体、阴影、动画等)。" - "确保代码符合React最佳实践,仅生成代码本身。" + "确保代码符合Vue最佳实践,仅生成代码本身。" ) - - result,usage = call_deepseek(prompt=prompt, system_prompt=system_prompt,temperature=0.4) - # print(result) - # print(usage) + result, usage = call_deepseek(prompt=prompt, system_prompt=system_prompt, temperature=0.4) return result -def save_code(code,file_path="../outputs/generated_code.jsx"): +def save_code(code, file_path="../outputs/generated_code.vue"): os.makedirs(os.path.dirname(file_path), exist_ok=True) with open(file_path, "w", encoding="utf-8") as f: f.write(code) @@ -137,4 +125,4 @@ def save_code(code,file_path="../outputs/generated_code.jsx"): if __name__ == "__main__": vue_code = generate_vue_code() save_code(vue_code) - print("React组件代码已生成并保存到outputs/generated_code.jsx") \ No newline at end of file + print("Vue组件代码已生成并保存到outputs/generated_code.vue") \ No newline at end of file