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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from openai import OpenAI
|
| 5 |
|
| 6 |
+
# =====================
|
| 7 |
+
# 配置
|
| 8 |
+
# =====================
|
| 9 |
|
| 10 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 11 |
+
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 12 |
+
|
| 13 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 14 |
+
|
| 15 |
+
# =====================
|
| 16 |
+
# AI文章生成
|
| 17 |
+
# =====================
|
| 18 |
+
|
| 19 |
+
def generate_article(keyword):
|
| 20 |
+
|
| 21 |
+
prompt = f"""
|
| 22 |
+
请根据关键词《{keyword}》写一篇适合今日头条发布的文章。
|
| 23 |
+
|
| 24 |
+
要求:
|
| 25 |
+
1. 标题吸引人
|
| 26 |
+
2. 正文800字左右
|
| 27 |
+
3. 分段清晰
|
| 28 |
+
4. 结尾总结
|
| 29 |
+
"""
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
+
model="gpt-4.1-mini",
|
| 34 |
+
messages=[
|
| 35 |
+
{
|
| 36 |
+
"role": "user",
|
| 37 |
+
"content": prompt
|
| 38 |
+
}
|
| 39 |
+
],
|
| 40 |
+
temperature=0.8
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return response.choices[0].message.content
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"文章生成失败:{str(e)}"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# =====================
|
| 50 |
+
# 搜图
|
| 51 |
+
# =====================
|
| 52 |
+
|
| 53 |
+
def search_images(keyword):
|
| 54 |
+
|
| 55 |
+
if not PEXELS_API_KEY:
|
| 56 |
+
return []
|
| 57 |
+
|
| 58 |
+
url = "https://api.pexels.com/v1/search"
|
| 59 |
+
|
| 60 |
+
headers = {
|
| 61 |
+
"Authorization": PEXELS_API_KEY
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
params = {
|
| 65 |
+
"query": keyword,
|
| 66 |
+
"per_page": 6
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
res = requests.get(
|
| 71 |
+
url,
|
| 72 |
+
headers=headers,
|
| 73 |
+
params=params,
|
| 74 |
+
timeout=20
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
data = res.json()
|
| 78 |
+
|
| 79 |
+
images = []
|
| 80 |
+
|
| 81 |
+
for photo in data.get("photos", []):
|
| 82 |
+
images.append(photo["src"]["medium"])
|
| 83 |
+
|
| 84 |
+
return images
|
| 85 |
+
|
| 86 |
+
except:
|
| 87 |
+
return []
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# =====================
|
| 91 |
+
# 视频搜索(示例)
|
| 92 |
+
# =====================
|
| 93 |
+
|
| 94 |
+
def search_videos(keyword):
|
| 95 |
+
|
| 96 |
+
links = [
|
| 97 |
+
f"https://www.youtube.com/results?search_query={keyword}"
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
return "\n".join(
|
| 101 |
+
[f"- {link}" for link in links]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
# =====================
|
| 106 |
+
# 主函数
|
| 107 |
+
# =====================
|
| 108 |
+
|
| 109 |
+
def main(keyword):
|
| 110 |
+
|
| 111 |
+
article = generate_article(keyword)
|
| 112 |
+
|
| 113 |
+
images = search_images(keyword)
|
| 114 |
+
|
| 115 |
+
videos = search_videos(keyword)
|
| 116 |
+
|
| 117 |
+
return article, images, videos
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# =====================
|
| 121 |
+
# Gradio界面
|
| 122 |
+
# =====================
|
| 123 |
+
|
| 124 |
+
with gr.Blocks() as demo:
|
| 125 |
+
|
| 126 |
+
gr.Markdown("# 头条 AI Agent")
|
| 127 |
+
|
| 128 |
+
gr.Markdown(
|
| 129 |
+
"输入关键词,自动生成文章、搜索图片和视频"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
keyword = gr.Textbox(
|
| 133 |
+
label="关键词",
|
| 134 |
+
placeholder="例如:人工智能"
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
btn = gr.Button("生成")
|
| 138 |
+
|
| 139 |
+
article_output = gr.Textbox(
|
| 140 |
+
label="生成文章",
|
| 141 |
+
lines=20
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
gallery_output = gr.Gallery(
|
| 145 |
+
label="相关图片",
|
| 146 |
+
columns=3
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
video_output = gr.Markdown(
|
| 150 |
+
label="相关视频"
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
btn.click(
|
| 154 |
+
fn=main,
|
| 155 |
+
inputs=keyword,
|
| 156 |
+
outputs=[
|
| 157 |
+
article_output,
|
| 158 |
+
gallery_output,
|
| 159 |
+
video_output
|
| 160 |
+
]
|
| 161 |
+
)
|
| 162 |
|
| 163 |
demo.launch()
|