Spaces:
Sleeping
Sleeping
Update article_generator.py
Browse files- article_generator.py +38 -20
article_generator.py
CHANGED
|
@@ -4,13 +4,10 @@ import json
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
-
import openai
|
| 8 |
from langchain.chat_models import ChatOpenAI
|
| 9 |
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
|
| 10 |
from langchain.llms import OpenAI
|
| 11 |
from langchain.agents.tools import Tool
|
| 12 |
-
import asyncio
|
| 13 |
-
from datetime import timedelta
|
| 14 |
|
| 15 |
# APIキーの設定
|
| 16 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
@@ -111,29 +108,50 @@ def generate_article(editable_output2):
|
|
| 111 |
<h3>{h3}</h3>
|
| 112 |
"{h3}"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。""")
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
)
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 127 |
-
f.write(
|
| 128 |
-
return
|
| 129 |
|
| 130 |
with gr.Blocks() as app:
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
generate_button.click(
|
| 136 |
fn=generate_article,
|
| 137 |
inputs=[editable_output2],
|
| 138 |
outputs=[final_article]
|
| 139 |
-
)
|
|
|
|
|
|
|
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
from bs4 import BeautifulSoup
|
|
|
|
| 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.llms import OpenAI
|
| 10 |
from langchain.agents.tools import Tool
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# APIキーの設定
|
| 13 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 108 |
<h3>{h3}</h3>
|
| 109 |
"{h3}"に関する詳細な内容を日本語で記述してください。オリジナルな内容を心がけてください。""")
|
| 110 |
|
| 111 |
+
# トークン数を制限するためにメッセージを分割
|
| 112 |
+
split_instructions = []
|
| 113 |
+
current_chunk = ""
|
| 114 |
+
for instruction in instructions:
|
| 115 |
+
if len(current_chunk + instruction) > 15000: # 適宜サイズ調整
|
| 116 |
+
split_instructions.append(current_chunk)
|
| 117 |
+
current_chunk = instruction
|
| 118 |
+
else:
|
| 119 |
+
current_chunk += instruction
|
| 120 |
|
| 121 |
+
if current_chunk:
|
| 122 |
+
split_instructions.append(current_chunk)
|
| 123 |
+
|
| 124 |
+
results = []
|
| 125 |
+
for i, split_instruction in enumerate(split_instructions):
|
| 126 |
+
user_message = {
|
| 127 |
+
"role": "user",
|
| 128 |
+
"content": f"{i+1}/{len(split_instructions)}: {split_instruction}"
|
| 129 |
+
}
|
| 130 |
+
response = openai.ChatCompletion.create(
|
| 131 |
+
model="gpt-4-turbo-2024-04-09",
|
| 132 |
+
messages=[system_message, user_message],
|
| 133 |
+
temperature=0.7,
|
| 134 |
+
)
|
| 135 |
+
results.append(response.choices[0]["message"]["content"])
|
| 136 |
+
|
| 137 |
+
final_result = "\n".join(results)
|
| 138 |
|
| 139 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 140 |
+
f.write(final_result)
|
| 141 |
+
return final_result
|
| 142 |
|
| 143 |
with gr.Blocks() as app:
|
| 144 |
+
with gr.Row():
|
| 145 |
+
with gr.Column():
|
| 146 |
+
editable_output2 = gr.Textbox(label="編集可能な記事構成", placeholder="HTML content here...", lines=10)
|
| 147 |
+
generate_button = gr.Button("記事を生成")
|
| 148 |
+
with gr.Column():
|
| 149 |
+
final_article = gr.Textbox(label="最終的な記事本文", lines=20, placeholder="Generated article content will appear here.")
|
| 150 |
+
|
| 151 |
generate_button.click(
|
| 152 |
fn=generate_article,
|
| 153 |
inputs=[editable_output2],
|
| 154 |
outputs=[final_article]
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
app.launch()
|