Yasu777 commited on
Commit
442072a
·
verified ·
1 Parent(s): 4071a1c

Update article_generator.py

Browse files
Files changed (1) hide show
  1. article_generator.py +24 -12
article_generator.py CHANGED
@@ -9,7 +9,6 @@ from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_e
9
  from langchain.llms import OpenAI
10
  from langchain.agents.tools import Tool
11
  import gradio as gr
12
- import html2text
13
 
14
  # APIキーの設定
15
  openai.api_key = os.getenv("OPENAI_API_KEY")
@@ -383,17 +382,30 @@ def generate_article(editable_output2):
383
  return final_html
384
 
385
  # HTMLをMarkdownに変換する関数
386
- def html_to_markdown(html_content):
387
- converter = html2text.HTML2Text()
388
- converter.ignore_links = False
389
- converter.ignore_images = False
390
- markdown_content = converter.handle(html_content)
391
- return markdown_content
 
 
 
 
 
 
 
 
 
 
 
 
 
392
 
393
  # コンテンツを表示する関数
394
  def display_content(content, format):
395
  if format == "Markdown":
396
- return html_to_markdown(content)
397
  return content
398
 
399
  # Gradioアプリの設定
@@ -411,9 +423,9 @@ def setup_gradio_interface():
411
  content_display.value = initial_markdown_content
412
 
413
  # フォーマット選択器の変更イベントを設定
414
- def update_content(format_choice, content):
415
  # フォーマットに基づいてコンテンツを更新
416
- updated_content = display_content(content, format_choice)
417
- return updated_content
418
 
419
- format_selector.change(update_content, inputs=[format_selector, content_display], outputs=content_display)
 
9
  from langchain.llms import OpenAI
10
  from langchain.agents.tools import Tool
11
  import gradio as gr
 
12
 
13
  # APIキーの設定
14
  openai.api_key = os.getenv("OPENAI_API_KEY")
 
382
  return final_html
383
 
384
  # HTMLをMarkdownに変換する関数
385
+ def custom_html_to_markdown(html):
386
+ soup = BeautifulSoup(html, 'html.parser')
387
+
388
+ # 不要なタグの除去
389
+ for tag in soup(['html', 'body', 'head', 'div']):
390
+ tag.decompose()
391
+
392
+ # タグごとの処理
393
+ for h in soup.find_all('h1'):
394
+ h.replace_with(f"# {h.get_text().strip()}\n\n")
395
+ for h in soup.find_all('h2'):
396
+ h.replace_with(f"## {h.get_text().strip()}\n\n")
397
+ for h in soup.find_all('h3'):
398
+ h.replace_with(f"### {h.get_text().strip()}\n\n")
399
+ for p in soup.find_all('p'):
400
+ p.replace_with(f"{p.get_text().strip()}\n\n")
401
+
402
+ # 最終的なMarkdownテキストの取得
403
+ return soup.get_text()
404
 
405
  # コンテンツを表示する関数
406
  def display_content(content, format):
407
  if format == "Markdown":
408
+ return custom_html_to_markdown(content)
409
  return content
410
 
411
  # Gradioアプリの設定
 
423
  content_display.value = initial_markdown_content
424
 
425
  # フォーマット選択器の変更イベントを設定
426
+ def update_content(format_choice):
427
  # フォーマットに基づいてコンテンツを更新
428
+ updated_content = display_content(initial_html_content, format_choice)
429
+ content_display.update(value=updated_content)
430
 
431
+ format_selector.change(update_content, inputs=format_selector, outputs=content_display)