Yasu777 commited on
Commit
1bc316c
·
verified ·
1 Parent(s): bc7b145

Update article_generator.py

Browse files
Files changed (1) hide show
  1. article_generator.py +67 -8
article_generator.py CHANGED
@@ -37,7 +37,7 @@ class EnhancedTavilySearchTool:
37
  params = {
38
  'api_key': tavily_api_key,
39
  'query': combined_query,
40
- 'max_results': 50, # 必要に応じて結果の数を調整
41
  'detail_level': 'high',
42
  'search_depth': 'advanced'
43
  }
@@ -70,9 +70,9 @@ def expand_h3_sections(soup, preloaded_data):
70
 
71
  if key in preloaded_data:
72
  context = preloaded_data[key]
73
- prompt = f"「{h3_text}」に続ける文章を生成してください。こちらが背景情報です:\n{context}"
74
  else:
75
- prompt = f"「{h3_text}」に続ける文章を生成してください。"
76
 
77
  expanded_text = generate_text_with_gpt4(prompt)
78
  new_paragraph = soup.new_tag('p')
@@ -94,11 +94,11 @@ def expand_section_with_gpt4(h2_text, h3_texts, preloaded_data):
94
  key = f"{h2_text} {h3_text}"
95
  if key in preloaded_data:
96
  context = preloaded_data[key]
97
- prompt = f"「{h3_text}」についしく説明してください。こちらが背景情報です:\n{context}"
98
  prompts.append(prompt)
99
  h3_to_text[h3_text] = prompt # プロンプトではなく後で置き換えるテキストを格納するための準備
100
  else:
101
- prompt = f"「{h3_text}」についしく説明してください。"
102
  prompts.append(prompt)
103
  h3_to_text[h3_text] = prompt
104
 
@@ -178,7 +178,9 @@ def generate_expanded_article(article_html, h3_to_text, cached_responses):
178
 
179
  process_summary_section(soup, cached_responses) # まとめセクションを特別処理し、キャッシュされたレスポンスを渡す
180
 
181
- return str(soup)
 
 
182
 
183
  # PlanAndExecuteエージェントをセットアップする関数
184
  def setup_plan_and_execute_agent():
@@ -203,7 +205,7 @@ def setup_plan_and_execute_agent():
203
  def generate_text_with_gpt4(prompt):
204
  response = openai.ChatCompletion.create(
205
  model="gpt-4o",
206
- messages=[{"role": "system", "content": "以下についての詳細な情報をまとめ、適宜箇所書き、もしくはを使ってオリジナルの内容にしてください。"},
207
  {"role": "user", "content": prompt}],
208
  temperature=0.7,
209
  max_tokens=500
@@ -379,4 +381,61 @@ def generate_article(editable_output2):
379
  f.write(final_html)
380
 
381
  print("Article generation complete. Output saved to output3.txt.")
382
- return final_html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  params = {
38
  'api_key': tavily_api_key,
39
  'query': combined_query,
40
+ 'max_results': 20, # 必要に応じて結果の数を調整
41
  'detail_level': 'high',
42
  'search_depth': 'advanced'
43
  }
 
70
 
71
  if key in preloaded_data:
72
  context = preloaded_data[key]
73
+ prompt = f"「{h3_text}」の内容を踏まえて、「{h3_text}」に続く、オリジナルの文章を生成してください。あなたの返答は必要なく、そのままテキストを生成してください。こちらが背景情報です:\n{context}"
74
  else:
75
+ prompt = f"「{h3_text}」の内容を踏まえて、「{h3_text}」に続く、オリジナルの文章を生成してください。あなたの返答は必要なく、そのままテキストを生成してください。"
76
 
77
  expanded_text = generate_text_with_gpt4(prompt)
78
  new_paragraph = soup.new_tag('p')
 
94
  key = f"{h2_text} {h3_text}"
95
  if key in preloaded_data:
96
  context = preloaded_data[key]
97
+ prompt = f"「{h3_text}」の内容を踏まえ、「{h3_text}」に続く、オリジナルの文章を生成ださい。あなたの返答は必要なく、そのまま文章やテキストを生成してください。こちらが背景情報です:\n{context}"
98
  prompts.append(prompt)
99
  h3_to_text[h3_text] = prompt # プロンプトではなく後で置き換えるテキストを格納するための準備
100
  else:
101
+ prompt = f"「{h3_text}」の内容を踏まえ、「{h3_text}」に続く、オリジナルの文章を生成ださい。あなたの返答は必要なく、そのまま文章やテキストを生成してください。"
102
  prompts.append(prompt)
103
  h3_to_text[h3_text] = prompt
104
 
 
178
 
179
  process_summary_section(soup, cached_responses) # まとめセクションを特別処理し、キャッシュされたレスポンスを渡す
180
 
181
+ final_html = str(soup)
182
+ latest_content.set(final_html) # 最新のHTMLコンテンツを状態に保存
183
+ return final_html
184
 
185
  # PlanAndExecuteエージェントをセットアップする関数
186
  def setup_plan_and_execute_agent():
 
205
  def generate_text_with_gpt4(prompt):
206
  response = openai.ChatCompletion.create(
207
  model="gpt-4o",
208
+ messages=[{"role": "system", "content": "以下についての詳細な情報をまとめ、適宜箇所書き、もしくはグラフを使ってオリジナルの内容にしてください。"},
209
  {"role": "user", "content": prompt}],
210
  temperature=0.7,
211
  max_tokens=500
 
381
  f.write(final_html)
382
 
383
  print("Article generation complete. Output saved to output3.txt.")
384
+ return final_html
385
+
386
+ # HTMLをMarkdownに変換する関数
387
+ def custom_html_to_markdown(html):
388
+ soup = BeautifulSoup(html, 'html.parser')
389
+
390
+ # 不要なタグの除去
391
+ for tag in soup(['html', 'body', 'head', 'div']):
392
+ tag.decompose()
393
+
394
+ # タグごとの処理
395
+ for h in soup.find_all('h1'):
396
+ h.replace_with(f"# {h.get_text().strip()}\n\n")
397
+ for h in soup.find_all('h2'):
398
+ h.replace_with(f"## {h.get_text().strip()}\n\n")
399
+ for h in soup.find_all('h3'):
400
+ h.replace_with(f"### {h.get_text().strip()}\n\n")
401
+ for p in soup.find_all('p'):
402
+ p.replace_with(f"{p.get_text().strip()}\n\n")
403
+
404
+ # 最終的なMarkdownテキストの取得
405
+ return soup.get_text()
406
+
407
+ # コンテンツを表示する関数
408
+ def display_content(content, format):
409
+ if format == "Markdown":
410
+ return custom_html_to_markdown(content)
411
+ return content
412
+
413
+ # Gradioアプリの設定
414
+ def setup_gradio_interface():
415
+ with gr.Blocks(css='''
416
+ .gr-markdown h1, .gr-markdown h2, .gr-markdown h3 {
417
+ word-wrap: break-word;
418
+ overflow-wrap: break-word;
419
+ }
420
+ ''') as app:
421
+ with gr.Row():
422
+ format_selector = gr.Radio(choices=["Markdown", "HTML"], label="Display Format", value="Markdown")
423
+ content_display = gr.Markdown(label="Content", value="")
424
+
425
+ # 初期のHTMLコンテンツを想定
426
+ initial_html_content = "<h1>Welcome</h1><p>This is a sample paragraph in HTML.</p>"
427
+ initial_markdown_content = display_content(initial_html_content, "Markdown")
428
+
429
+ # 最新のコンテンツを保持するための状態
430
+ latest_content = gr.Variable(value=initial_html_content)
431
+
432
+ # 最初のコンテンツを設定
433
+ content_display.value = initial_markdown_content
434
+
435
+ # フォーマット選択器の変更イベントを設定
436
+ def update_content(format_choice):
437
+ # 最新のコンテンツに基づいて表示を更新
438
+ updated_content = display_content(latest_content.get(), format_choice)
439
+ return updated_content
440
+
441
+ format_selector.change(update_content, inputs=[format_selector], outputs=[content_display])