Spaces:
Build error
Build error
Add application file
Browse files- app.py +55 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
from gnews import GNews
|
| 3 |
+
import fastapi_poe as fp
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# 初始化GNews
|
| 7 |
+
google_news = GNews(language='en', country='US')
|
| 8 |
+
|
| 9 |
+
api_key = 'Mz4yhxQiIySrGas7qGElXtnYgzJCyJDUltrpFcZpolI' # 确保替换为你的API密钥
|
| 10 |
+
|
| 11 |
+
# 定义异步函数来生成每个新闻的博客文章
|
| 12 |
+
async def generate_blog_for_news(article, index):
|
| 13 |
+
news_text = f"Title: {article['title']}\nDescription: {article['description']}\nURL: {article['url']}\n\nPlease write a blog post about this news."
|
| 14 |
+
|
| 15 |
+
message = fp.ProtocolMessage(role="user", content=news_text)
|
| 16 |
+
complete_text = ''
|
| 17 |
+
|
| 18 |
+
async for partial in fp.get_bot_response(messages=[message], bot_name="Mixtral-8x7B-Chat", api_key=api_key):
|
| 19 |
+
complete_text += partial.text
|
| 20 |
+
|
| 21 |
+
file_path = f'Blog_Article_{index}.txt'
|
| 22 |
+
with open(file_path, 'w') as file:
|
| 23 |
+
file.write(complete_text)
|
| 24 |
+
|
| 25 |
+
return file_path
|
| 26 |
+
|
| 27 |
+
# 运行异步函数为每条新闻生成博客文章,并收集文件路径
|
| 28 |
+
async def main():
|
| 29 |
+
top_news = google_news.get_top_news()
|
| 30 |
+
top_10_news = top_news[:1] # 获取前2条新闻来生成博客
|
| 31 |
+
|
| 32 |
+
file_paths = await asyncio.gather(*(generate_blog_for_news(article, index) for index, article in enumerate(top_10_news, 1)))
|
| 33 |
+
return file_paths
|
| 34 |
+
|
| 35 |
+
# 将异步main函数包装为同步函数,以便Gradio使用
|
| 36 |
+
def run_main():
|
| 37 |
+
return asyncio.run(main())
|
| 38 |
+
|
| 39 |
+
# Gradio界面函数,修改以返回文件路径
|
| 40 |
+
def create_blog():
|
| 41 |
+
file_paths = run_main()
|
| 42 |
+
return file_paths
|
| 43 |
+
|
| 44 |
+
# 设置Gradio界面
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=create_blog,
|
| 47 |
+
inputs=None,
|
| 48 |
+
outputs=gr.outputs.File(label="Download Blog Posts"),
|
| 49 |
+
title="自动博客生成器",
|
| 50 |
+
description="点击按钮生成博客文章,并提供下载链接!"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# 运行Gradio app
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
gnews
|
| 3 |
+
fastapi_poe
|
| 4 |
+
asyncio
|