Spaces:
Sleeping
Sleeping
Update article_generator.py
Browse files- article_generator.py +24 -22
article_generator.py
CHANGED
|
@@ -138,7 +138,7 @@ def setup_plan_and_execute_agent():
|
|
| 138 |
def generate_text_with_gpt4(prompt):
|
| 139 |
response = openai.ChatCompletion.create(
|
| 140 |
model="gpt-4o",
|
| 141 |
-
messages=[{"role": "system", "content": "以下についての詳細な情報をまとめ、適宜
|
| 142 |
{"role": "user", "content": prompt}],
|
| 143 |
temperature=0.7,
|
| 144 |
max_tokens=1000
|
|
@@ -148,14 +148,18 @@ def generate_text_with_gpt4(prompt):
|
|
| 148 |
# 記事のセクションをGPT-4で拡張する関数
|
| 149 |
def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
|
| 150 |
prompts = []
|
|
|
|
| 151 |
for h3_text in h3_texts:
|
| 152 |
key = f"{h2_text} {h3_text}"
|
| 153 |
if key in preloaded_data:
|
| 154 |
context = preloaded_data[key]
|
| 155 |
-
prompt = f"「{h3_text}」
|
| 156 |
prompts.append(prompt)
|
|
|
|
| 157 |
else:
|
| 158 |
-
|
|
|
|
|
|
|
| 159 |
|
| 160 |
if not prompts: # promptsが空の場合
|
| 161 |
print("No prompts to process.")
|
|
@@ -164,36 +168,32 @@ def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
|
|
| 164 |
expanded_texts = []
|
| 165 |
# ThreadPoolExecutorのmax_workersに最小値を設定
|
| 166 |
with ThreadPoolExecutor(max_workers=max(1, len(prompts))) as executor:
|
| 167 |
-
future_to_prompt = {executor.submit(generate_text_with_gpt4, prompt):
|
| 168 |
for future in as_completed(future_to_prompt):
|
| 169 |
-
|
| 170 |
try:
|
| 171 |
expanded_text = future.result()
|
| 172 |
expanded_texts.append(expanded_text)
|
|
|
|
| 173 |
except Exception as e:
|
| 174 |
-
error_message = f"Error
|
| 175 |
print(error_message)
|
| 176 |
expanded_texts.append("Error in text generation.")
|
| 177 |
|
| 178 |
-
return
|
| 179 |
|
| 180 |
# 記事を拡張する関数
|
| 181 |
-
def generate_expanded_article(article_html):
|
| 182 |
print("Expanding article...")
|
| 183 |
soup = BeautifulSoup(article_html, 'html.parser')
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
h3_texts = [h3.get_text() for h3 in h2.find_next_siblings('h3')]
|
| 189 |
-
expanded_texts = expand_section_with_gpt4(h2.get_text(), h3_texts, preloaded_data)
|
| 190 |
-
|
| 191 |
-
for h3, expanded_text in zip(h3_texts, expanded_texts):
|
| 192 |
-
h3_tag = soup.find('h3', text=h3)
|
| 193 |
new_paragraph = soup.new_tag('p')
|
| 194 |
-
new_paragraph.string =
|
| 195 |
-
|
| 196 |
-
|
| 197 |
return str(soup)
|
| 198 |
|
| 199 |
# 記事を生成する関数
|
|
@@ -314,7 +314,8 @@ def generate_article(editable_output2):
|
|
| 314 |
final_result = "\n".join(results)
|
| 315 |
|
| 316 |
# 生成された初期記事を拡張
|
| 317 |
-
|
|
|
|
| 318 |
|
| 319 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 320 |
f.write(expanded_article)
|
|
@@ -376,7 +377,8 @@ def continue_generate_article():
|
|
| 376 |
final_result = "\n".join(results)
|
| 377 |
|
| 378 |
# 生成された初期記事を拡張
|
| 379 |
-
|
|
|
|
| 380 |
|
| 381 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 382 |
f.write(expanded_article)
|
|
|
|
| 138 |
def generate_text_with_gpt4(prompt):
|
| 139 |
response = openai.ChatCompletion.create(
|
| 140 |
model="gpt-4o",
|
| 141 |
+
messages=[{"role": "system", "content": "以下についての詳細な情報をまとめ、適宜箇所書き、もしくは表を使ってオリジナルの内容にしてください。"},
|
| 142 |
{"role": "user", "content": prompt}],
|
| 143 |
temperature=0.7,
|
| 144 |
max_tokens=1000
|
|
|
|
| 148 |
# 記事のセクションをGPT-4で拡張する関数
|
| 149 |
def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
|
| 150 |
prompts = []
|
| 151 |
+
h3_to_text = {}
|
| 152 |
for h3_text in h3_texts:
|
| 153 |
key = f"{h2_text} {h3_text}"
|
| 154 |
if key in preloaded_data:
|
| 155 |
context = preloaded_data[key]
|
| 156 |
+
prompt = f"「{h3_text}」について詳しく説明してください。こちらが背景情報です:\n{context}"
|
| 157 |
prompts.append(prompt)
|
| 158 |
+
h3_to_text[h3_text] = prompt # プロンプトではなく後で置き換えるテキストを格納するための準備
|
| 159 |
else:
|
| 160 |
+
prompt = f"「{h3_text}」について詳しく説明してください。"
|
| 161 |
+
prompts.append(prompt)
|
| 162 |
+
h3_to_text[h3_text] = prompt
|
| 163 |
|
| 164 |
if not prompts: # promptsが空の場合
|
| 165 |
print("No prompts to process.")
|
|
|
|
| 168 |
expanded_texts = []
|
| 169 |
# ThreadPoolExecutorのmax_workersに最小値を設定
|
| 170 |
with ThreadPoolExecutor(max_workers=max(1, len(prompts))) as executor:
|
| 171 |
+
future_to_prompt = {executor.submit(generate_text_with_gpt4, prompt): h3_text for prompt, h3_text in zip(prompts, h3_texts)}
|
| 172 |
for future in as_completed(future_to_prompt):
|
| 173 |
+
h3_text = future_to_prompt[future]
|
| 174 |
try:
|
| 175 |
expanded_text = future.result()
|
| 176 |
expanded_texts.append(expanded_text)
|
| 177 |
+
h3_to_text[h3_text] = expanded_text # 実際に生成されたテキストを保存
|
| 178 |
except Exception as e:
|
| 179 |
+
error_message = f"Error generating text for {h3_text}: {str(e)}"
|
| 180 |
print(error_message)
|
| 181 |
expanded_texts.append("Error in text generation.")
|
| 182 |
|
| 183 |
+
return h3_to_text
|
| 184 |
|
| 185 |
# 記事を拡張する関数
|
| 186 |
+
def generate_expanded_article(article_html, h3_to_text):
|
| 187 |
print("Expanding article...")
|
| 188 |
soup = BeautifulSoup(article_html, 'html.parser')
|
| 189 |
+
h3_elements = soup.find_all('h3')
|
| 190 |
+
|
| 191 |
+
for h3 in h3_elements:
|
| 192 |
+
if h3.get_text() in h3_to_text:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
new_paragraph = soup.new_tag('p')
|
| 194 |
+
new_paragraph.string = h3_to_text[h3.get_text()] # 生成されたテキストを挿入
|
| 195 |
+
h3.insert_after(new_paragraph)
|
| 196 |
+
|
| 197 |
return str(soup)
|
| 198 |
|
| 199 |
# 記事を生成する関数
|
|
|
|
| 314 |
final_result = "\n".join(results)
|
| 315 |
|
| 316 |
# 生成された初期記事を拡張
|
| 317 |
+
h3_to_text = expand_section_with_gpt4(final_result, h3_texts, cached_responses)
|
| 318 |
+
expanded_article = generate_expanded_article(final_result, h3_to_text)
|
| 319 |
|
| 320 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 321 |
f.write(expanded_article)
|
|
|
|
| 377 |
final_result = "\n".join(results)
|
| 378 |
|
| 379 |
# 生成された初期記事を拡張
|
| 380 |
+
h3_to_text = expand_section_with_gpt4(final_result, h3_texts, cached_responses)
|
| 381 |
+
expanded_article = generate_expanded_article(final_result, h3_to_text)
|
| 382 |
|
| 383 |
with open("output3.txt", "w", encoding="utf-8") as f:
|
| 384 |
f.write(expanded_article)
|