Spaces:
Sleeping
Sleeping
Update article_generator.py
Browse files- article_generator.py +52 -24
article_generator.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import openai
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
-
|
| 7 |
from langchain.chat_models import ChatOpenAI
|
| 8 |
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
|
| 9 |
from langchain.agents.tools import Tool
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
tavily_api_key = os.getenv('TAVILY_API_KEY')
|
| 14 |
|
|
|
|
| 15 |
class EnhancedTavilySearchTool:
|
| 16 |
def search(self, query):
|
| 17 |
params = {
|
|
@@ -26,37 +26,65 @@ class EnhancedTavilySearchTool:
|
|
| 26 |
else:
|
| 27 |
raise Exception("Failed to fetch data from Tavily API")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
description="Enhanced search tool using Tavily API"
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
tools = [tavily_search_tool]
|
| 37 |
-
|
| 38 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, max_tokens=1000)
|
| 39 |
planner = load_chat_planner(llm)
|
| 40 |
executor = load_agent_executor(llm, tools, verbose=True)
|
| 41 |
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
soup = BeautifulSoup(editable_output2, 'html.parser')
|
| 44 |
h1_text = soup.find('h1').get_text()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
# 実行と結果の取得
|
| 47 |
-
result = agent.run(f"Research about {h1_text}")
|
| 48 |
return result
|
| 49 |
|
| 50 |
-
# Gradioインターフェースの設定
|
| 51 |
with gr.Blocks() as app:
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
generate_button = gr.Button("Generate")
|
| 56 |
-
output_article = gr.Textbox(label="Generated Article", lines=20)
|
| 57 |
|
| 58 |
generate_button.click(
|
| 59 |
-
fn=
|
| 60 |
-
inputs=[
|
| 61 |
-
outputs=
|
| 62 |
)
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
+
import openai
|
| 5 |
from langchain.chat_models import ChatOpenAI
|
| 6 |
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
|
| 7 |
from langchain.agents.tools import Tool
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
# APIキーの設定
|
| 11 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
tavily_api_key = os.getenv('TAVILY_API_KEY')
|
| 13 |
|
| 14 |
+
# Tavily APIのカスタムツールを定義
|
| 15 |
class EnhancedTavilySearchTool:
|
| 16 |
def search(self, query):
|
| 17 |
params = {
|
|
|
|
| 26 |
else:
|
| 27 |
raise Exception("Failed to fetch data from Tavily API")
|
| 28 |
|
| 29 |
+
# PlanAndExecute エージェントのセットアップ
|
| 30 |
+
def setup_agent():
|
| 31 |
+
tavily_search_tool = EnhancedTavilySearchTool()
|
| 32 |
+
tools = [Tool(name="TavilySearch", func=tavily_search_tool.search, description="Enhanced search tool using Tavily API")]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo-1106", temperature=0, max_tokens=1000)
|
| 34 |
planner = load_chat_planner(llm)
|
| 35 |
executor = load_agent_executor(llm, tools, verbose=True)
|
| 36 |
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)
|
| 37 |
+
return agent
|
| 38 |
|
| 39 |
+
agent = setup_agent()
|
| 40 |
+
|
| 41 |
+
# 記事本文の生成
|
| 42 |
+
def generate_article(editable_output2):
|
| 43 |
soup = BeautifulSoup(editable_output2, 'html.parser')
|
| 44 |
h1_text = soup.find('h1').get_text()
|
| 45 |
+
h2_texts = [h2.get_text() for h2 in soup.find_all('h2')]
|
| 46 |
+
h3_texts = [h3.get_text() for h3 in soup.find_all('h3')]
|
| 47 |
+
|
| 48 |
+
system_message = {
|
| 49 |
+
"role": "system",
|
| 50 |
+
"content": "あなたはプロのライターです。すべての回答を日本語でお願いします。"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
instructions = [
|
| 54 |
+
f"<h1>{h1_text}</h1> \"{h1_text}\"に関する導入文を日本語で作成してください。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。"
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
for idx, h2_text in enumerate(h2_texts):
|
| 58 |
+
h3_for_this_h2 = [h3 for h3 in h3_texts if h3.startswith(f"{idx+1}-")]
|
| 59 |
+
instructions.append(
|
| 60 |
+
f"<h2>{h2_text}</h2> \"{h2_text}\"に関する導入文を日本語で作成してください。この導入文は、以下の小見出しの内容を考慮してください:{'、'.join(h3_for_this_h2)}。直接的なコピーまたは近いフレーズを避けて、オリジナルな内容にしてください。"
|
| 61 |
+
)
|
| 62 |
+
for h3 in h3_for_this_h2:
|
| 63 |
+
instructions.append(
|
| 64 |
+
f"<h3>{h3}</h3> \"{h3}\"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。"
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
user_message = {
|
| 68 |
+
"role": "user",
|
| 69 |
+
"content": "\n".join(instructions)
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
response = openai.ChatCompletion.create(
|
| 73 |
+
model="gpt-4-0125-preview",
|
| 74 |
+
messages=[system_message, user_message],
|
| 75 |
+
temperature=0.7
|
| 76 |
+
)
|
| 77 |
+
result = response.choices[0]["message"]["content"]
|
| 78 |
|
|
|
|
|
|
|
| 79 |
return result
|
| 80 |
|
|
|
|
| 81 |
with gr.Blocks() as app:
|
| 82 |
+
editable_output2 = gr.Textbox(label="編集可能な記事構成", placeholder="HTML content here...", lines=10)
|
| 83 |
+
final_article = gr.Textbox(label="最終的な記事本文", lines=20, placeholder="Generated article content will appear here.")
|
| 84 |
+
generate_button = gr.Button("記事を生成")
|
|
|
|
|
|
|
| 85 |
|
| 86 |
generate_button.click(
|
| 87 |
+
fn=generate_article,
|
| 88 |
+
inputs=[editable_output2],
|
| 89 |
+
outputs=[final_article]
|
| 90 |
)
|