abo/demo.py

146 lines
5.0 KiB
Python
Raw Normal View History

2025-07-16 15:34:54 +08:00
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()