Spaces:
Build error
Build error
Update article_generator.py
Browse files- 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
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
# コンテンツを表示する関数
|
| 394 |
def display_content(content, format):
|
| 395 |
if format == "Markdown":
|
| 396 |
-
return
|
| 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
|
| 415 |
# フォーマットに基づいてコンテンツを更新
|
| 416 |
-
updated_content = display_content(
|
| 417 |
-
|
| 418 |
|
| 419 |
-
format_selector.change(update_content, inputs=
|
|
|
|
| 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)
|