Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,23 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
|
| 6 |
# =====================
|
| 7 |
-
# 配置
|
| 8 |
# =====================
|
| 9 |
-
|
| 10 |
-
|
| 11 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# =====================
|
| 16 |
-
# AI文章生成
|
| 17 |
# =====================
|
| 18 |
-
|
| 19 |
def generate_article(keyword):
|
| 20 |
-
|
| 21 |
prompt = f"""
|
| 22 |
请根据关键词《{keyword}》写一篇适合今日头条发布的文章。
|
| 23 |
|
|
@@ -27,40 +27,34 @@ def generate_article(keyword):
|
|
| 27 |
3. 分段清晰
|
| 28 |
4. 结尾总结
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
try:
|
|
|
|
| 32 |
response = client.chat.completions.create(
|
| 33 |
-
model="
|
| 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
|
|
@@ -73,86 +67,87 @@ def search_images(keyword):
|
|
| 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 |
-
|
| 101 |
-
|
| 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 |
-
|
| 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=
|
| 156 |
outputs=[
|
| 157 |
article_output,
|
| 158 |
gallery_output,
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
import gradio as gr
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
# =====================
|
| 7 |
+
# 配置 (从环境变量安全读取)
|
| 8 |
# =====================
|
| 9 |
+
# 获取 Hugging Face 的 Access Token(在后台 Settings -> Secrets 里配置)
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 11 |
PEXELS_API_KEY = os.getenv("PEXELS_API_KEY")
|
| 12 |
|
| 13 |
+
# 初始化 Hugging Face 免费大模型客户端
|
| 14 |
+
# 使用官方推荐且对中文支持极好的 Qwen 2.5 7B 模型,完全运行在 HF 云端,不吃本地内存
|
| 15 |
+
client = InferenceClient("Qwen/Qwen-2.5-7B-Instruct", token=HF_TOKEN)
|
| 16 |
|
| 17 |
# =====================
|
| 18 |
+
# AI文章生成(HF 免费版)
|
| 19 |
# =====================
|
|
|
|
| 20 |
def generate_article(keyword):
|
|
|
|
| 21 |
prompt = f"""
|
| 22 |
请根据关键词《{keyword}》写一篇适合今日头条发布的文章。
|
| 23 |
|
|
|
|
| 27 |
3. 分段清晰
|
| 28 |
4. 结尾总结
|
| 29 |
"""
|
|
|
|
| 30 |
try:
|
| 31 |
+
# 使用标准的 chat.completions 接口,完全免费
|
| 32 |
response = client.chat.completions.create(
|
| 33 |
+
model="Qwen/Qwen-2.5-7B-Instruct",
|
| 34 |
messages=[
|
| 35 |
{
|
| 36 |
"role": "user",
|
| 37 |
"content": prompt
|
| 38 |
}
|
| 39 |
],
|
| 40 |
+
temperature=0.8,
|
| 41 |
+
max_tokens=1500
|
| 42 |
)
|
|
|
|
| 43 |
return response.choices[0].message.content
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
+
return f"文章生成失败,原因:{str(e)}\n\n💡 提示:如果提示无权限,请检查是否在 Space 的 Settings -> Secrets 中正确配置了 HF_TOKEN。"
|
|
|
|
| 46 |
|
| 47 |
# =====================
|
| 48 |
+
# 搜图 (Pexels API)
|
| 49 |
# =====================
|
|
|
|
| 50 |
def search_images(keyword):
|
|
|
|
| 51 |
if not PEXELS_API_KEY:
|
| 52 |
return []
|
| 53 |
|
| 54 |
url = "https://api.pexels.com/v1/search"
|
|
|
|
| 55 |
headers = {
|
| 56 |
"Authorization": PEXELS_API_KEY
|
| 57 |
}
|
|
|
|
| 58 |
params = {
|
| 59 |
"query": keyword,
|
| 60 |
"per_page": 6
|
|
|
|
| 67 |
params=params,
|
| 68 |
timeout=20
|
| 69 |
)
|
|
|
|
| 70 |
data = res.json()
|
| 71 |
+
|
| 72 |
images = []
|
|
|
|
| 73 |
for photo in data.get("photos", []):
|
| 74 |
images.append(photo["src"]["medium"])
|
|
|
|
| 75 |
return images
|
|
|
|
| 76 |
except:
|
| 77 |
return []
|
| 78 |
|
|
|
|
| 79 |
# =====================
|
| 80 |
# 视频搜索(示例)
|
| 81 |
# =====================
|
|
|
|
| 82 |
def search_videos(keyword):
|
| 83 |
+
# 用标准 Markdown 语法包装超链接
|
| 84 |
links = [
|
| 85 |
+
f"[在 YouTube 上搜索 《{keyword}》](https://www.youtube.com/results?search_query={keyword})"
|
| 86 |
]
|
| 87 |
+
|
| 88 |
+
# 采用纯文本符号 !!! 代替复杂 emoji,防止系统字体缺失导致乱码
|
| 89 |
+
result = "### !!! 相关视频搜索链接\n"
|
| 90 |
+
result += "\n".join([f"- {link}" for link in links])
|
| 91 |
+
return result
|
| 92 |
|
| 93 |
# =====================
|
| 94 |
# 主函数
|
| 95 |
# =====================
|
|
|
|
| 96 |
def main(keyword):
|
| 97 |
+
if not keyword.strip():
|
| 98 |
+
return "请输入有效的关键词!", [], "### !!! 请输入关键词"
|
| 99 |
+
|
| 100 |
article = generate_article(keyword)
|
|
|
|
| 101 |
images = search_images(keyword)
|
|
|
|
| 102 |
videos = search_videos(keyword)
|
|
|
|
| 103 |
return article, images, videos
|
| 104 |
|
|
|
|
| 105 |
# =====================
|
| 106 |
+
# Gradio界面布局
|
| 107 |
# =====================
|
| 108 |
+
with gr.Blocks(title="头条 AI Agent") as demo:
|
| 109 |
|
| 110 |
+
gr.Markdown("# 头条 AI Agent (HF 免费版)")
|
| 111 |
+
gr.Markdown("输入关键词,依托 Hugging Face 免费云端算力一键生成爆款文章、匹配图片和视频资源。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# 布局:上方输入栏
|
| 114 |
+
with gr.Row():
|
| 115 |
+
keyword_input = gr.Textbox(
|
| 116 |
+
label="关键词",
|
| 117 |
+
placeholder="例如:人工智能新趋势",
|
| 118 |
+
scale=4
|
| 119 |
+
)
|
| 120 |
+
btn = gr.Button("立即生成", variant="primary", scale=1)
|
| 121 |
+
|
| 122 |
+
# 布局:下方左右分栏
|
| 123 |
+
with gr.Row():
|
| 124 |
+
# 左栏:文章输出
|
| 125 |
+
with gr.Column(scale=2):
|
| 126 |
+
article_output = gr.Textbox(
|
| 127 |
+
label="生成文章",
|
| 128 |
+
lines=22,
|
| 129 |
+
show_copy_button=True # 开启一键复制功能,方便搬运
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
# 右栏:素材匹配
|
| 133 |
+
with gr.Column(scale=1):
|
| 134 |
+
gallery_output = gr.Gallery(
|
| 135 |
+
label="匹配素材(Pexels 免费商用图)",
|
| 136 |
+
columns=2,
|
| 137 |
+
rows=3,
|
| 138 |
+
height=350
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# 视频链接包裹在一个基础小框里,保持视觉整洁
|
| 142 |
+
with gr.Box():
|
| 143 |
+
video_output = gr.Markdown(
|
| 144 |
+
label="素材视频链接"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# 绑定点击事件
|
| 148 |
btn.click(
|
| 149 |
fn=main,
|
| 150 |
+
inputs=keyword_input,
|
| 151 |
outputs=[
|
| 152 |
article_output,
|
| 153 |
gallery_output,
|