abo/demo.py
2025-07-16 15:34:54 +08:00

146 lines
5.0 KiB
Python
Raw Permalink 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 gradio as gr
from langchain_openai import ChatOpenAI
from pydantic import SecretStr
import os
# 设置API_KEY
# export VOLCANO_DS__API_KEY=6840c164-3b7e-4ddc-9d51-20624defc205
# Load API key from environment
api_key = os.getenv('VOLCANO_DS__API_KEY', '')
if not api_key:
raise ValueError('VOLCANO_DS__API_KEY is not set')
# Initialize DeepSeek LLM
llm = ChatOpenAI(
base_url='https://ark.cn-beijing.volces.com/api/v3', #'https://api.deepseek.com/v1',
#model= 'ep-20250223233956-tfgd8', #v3
model= 'ep-20250224014430-zshpj', #R1
api_key=SecretStr(api_key),
)
def get_prompt_template(template_name):
"""Return the corresponding template structure"""
templates = {
"标准模板": """一、产品信息:
1. 产品名称:{product_name}
2. 产品描述:{product_desc}
3. 核心卖点:{product_features}
二、文案结构:
1. 标题:简洁有力,突出核心价值
2. 导语:抓住痛点,引发共鸣
3. 主体:详细阐述产品优势
4. 结尾:明确的行动号召""",
"故事模板": """一、产品信息:
1. 产品名称:{product_name}
2. 产品描述:{product_desc}
3. 核心卖点:{product_features}
二、文案结构:
1. 标题:引人入胜的故事标题
2. 导语:讲述用户故事,引发共鸣
3. 主体:通过故事展示产品价值
4. 结尾:引导用户采取行动""",
"数据模板": """一、产品信息:
1. 产品名称:{product_name}
2. 产品描述:{product_desc}
3. 核心卖点:{product_features}
二、文案结构:
1. 标题:数据驱动的标题
2. 导语:用数据说明问题
3. 主体:详细的数据分析和案例
4. 结尾:基于数据的行动建议""",
"对比模板": """一、产品信息:
1. 产品名称:{product_name}
2. 产品描述:{product_desc}
3. 核心卖点:{product_features}
二、文案结构:
1. 标题:突出对比优势的标题
2. 导语:说明对比的必要性
3. 主体:详细的产品对比分析
4. 结尾:强调选择理由"""
}
return templates.get(template_name, templates["标准模板"])
def generate_prompt(product_name, product_desc, product_features, writing_style, template):
"""Generate and return the complete prompt"""
template_structure = get_prompt_template(template)
filled_template = template_structure.format(
product_name=product_name,
product_desc=product_desc,
product_features=product_features
)
return f"""你是一位资深营销文案专家,请根据以下信息生成高质量的营销文案:
{filled_template}
三、文案要求:
1. 目标受众25-40岁注重品质生活的中产阶级
2. 使用场景:日常使用、商务场合、特殊场景
3. 竞品对比:突出产品独特优势
4. 文案风格:{writing_style}
5. 情感诉求:建立品牌信任,激发购买欲望
四、技术要求:
1. 字数400-600字
2. 语言:符合{writing_style}要求
3. 修辞:适当使用比喻、排比等修辞手法
4. SEO自然融入10-20个相关关键词
请生成专业、吸引人的营销文案,确保内容真实可信,符合广告法规要求。"""
def query_deepseek(product_name, product_desc, product_features, writing_style, template):
"""Generate marketing copy based on product info, writing style and template"""
prompt = generate_prompt(product_name, product_desc, product_features, writing_style, template)
try:
response = llm.invoke(prompt)
return prompt, response.content
except Exception as e:
return prompt, f"Error: {str(e)}"
# Create Gradio interface
interface = gr.Interface(
fn=query_deepseek,
inputs=[
gr.Textbox(lines=2, placeholder="请输入产品名称...", label="产品名称"),
gr.Textbox(lines=4, placeholder="请输入产品详细介绍...", label="产品介绍"),
gr.Textbox(lines=4, placeholder="请输入产品特点...", label="产品特点"),
gr.Dropdown(
choices=[
"专业正式", "轻松活泼", "简洁明了", "情感共鸣",
"科技感强", "文艺清新", "幽默风趣", "权威专业",
"高端奢华", "亲民实惠", "故事叙述", "数据驱动",
"对比突出", "问题解决", "场景带入", "用户见证",
"限时优惠", "节日主题", "环保理念", "社会责任",
"未来趋势", "历史传承", "文化融合", "国际视野"
],
label="文案风格"
),
gr.Dropdown(
choices=["标准模板", "故事模板", "数据模板", "对比模板"],
label="文案模板",
value="标准模板"
)
],
outputs=[
gr.Text(label="拼装后的Prompt"),
gr.Markdown(label="生成的营销文案")
],
title="AI Marketing",
description="基于deepseek_v3的营销文案生成"
)
# Launch the interface
if __name__ == "__main__":
#interface.launch(share=True)
interface.launch()