Files

1 line
3.5 KiB
JSON
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.
{"repo": "aaronwn/ai-xhs", "files": {"app1.py": {"deaecc349dc151827594e8fd4bce3cc4327c6935": {"t": 1785746634.3247762, "n": 2233, "h": "5f50b30eca6393b9", "c": "import os\nfrom flask import Flask, request, jsonify\nfrom flask_cors import CORS\nfrom openai import OpenAI\nfrom dotenv import load_dotenv\n\n# 加载环境变量\nload_dotenv()\n\napp = Flask(__name__)\nCORS(app)\n\n# 初始化 OpenAI 客户端,使用 Deepseek API\nclient = OpenAI(\n api_key=os.getenv(\"DEEPSEEK_API_KEY\"),\n base_url=\"https://api.deepseek.com\"\n)\n\ndef generate_note(theme):\n \"\"\"生成小红书风格的笔记\"\"\"\n prompt = f\"\"\"你是一位深谙小红书爆款笔记创作的资深博主。请根据用户输入的主题\"{theme}\",生成一篇吸引人的小红书笔记。\n\n- 基于用户输入的主题,生成更吸引眼球的标题\n- 标题字数控制在20字符以内(包含emoji)\n- 标题需包含1-2个emoji,放在标题开头或结尾\n- 标题要有爆点,制造好奇心\n- 可以用\"?\"\"!\"等标点增强表现力\n\n- 开头要吸引眼球,用简短有力的文案hook住读者\n- 必须分3个要点展开,每个要点需要:\n * 每个要点使用\"🔍|💡|✨|📌|💫\"作为开头\n * 要点标题需要加粗\n * 要点内容简明扼要\n * 要点之间空一行分隔\n * 无需添加\"要点一\"等序号标识\n * 要点标题与内容紧接排列\n * 要点内多条内容需添加序号emoji\n- 文风要求:\n * 亲和力强的对话式表达,像在跟好朋友分享\n * 口语化表达,自然不做作\n * 适度使用\"绝绝子\"\"yyds\"\"无语子\"等小红书流行用语\n * 传递真诚和专业感\n- 结尾加上3个相关话题标签,用#号开头\n\n整体要求:\n- 正文字数控制在300字以内\n- 内容要有价值和可操作性\n- 避免过度营销感和虚假信息\n- 适量使用标点符号增强表达力(❗️、❓、~)\n- 注意性别中立的表达方式,内容要适合所有用户群体\"\"\"\n\n try:\n response = client.chat.completions.create(\n model=\"deepseek-chat\",\n messages=[\n {\"role\": \"system\", \"content\": \"你是一位深谙小红书运营之道的博主,擅长创作爆款笔记。你的文章结构清晰,重点突出,并善于使用emoji增加文章趣味性。\"},\n {\"role\": \"user\", \"content\": prompt}\n ],\n temperature=0.8,\n max_tokens=1000,\n timeout=25\n )\n return response.choices[0].message.content\n except Exception as e:\n error_message = str(e)\n print(f\"Deepseek API Error: {error_message}\")\n if \"timeout\" in error_message.lower():\n return {\"error\": \"请求超时,请稍后重试\"}\n return {\"error\": f\"生成失败: {error_message}\"}\n\n@app.route('/')\ndef home():\n return app.send_static_file('index.html')\n\n@app.route('/api/generate', methods=['POST'])\ndef generate():\n try:\n data = request.json\n theme = data.get('theme')\n if not theme:\n return jsonify({\"error\": \"请提供主题\"}), 400\n\n note = generate_note(theme)\n\n if isinstance(note, dict) and \"error\" in note:\n return jsonify(note), 500\n\n return jsonify({\"note\": note})\n except Exception as e:\n return jsonify({\"error\": f\"服务器错误: {str(e)}\"}), 500\n\nif __name__ == '__main__':\n app.run(debug=True)\n"}}}}