Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import subprocess | |
| import re | |
| from tinydb import TinyDB, Query | |
| import datetime | |
| from bs4 import BeautifulSoup | |
| import warnings | |
| from article_generator import generate_article | |
| def is_valid_html(html): | |
| with warnings.catch_warnings(record=True) as w: | |
| warnings.simplefilter('always') | |
| BeautifulSoup(html, 'html.parser') | |
| return len(w) == 0 | |
| # データベースの初期化 | |
| db = TinyDB("db.json") | |
| current_time = datetime.datetime.now() | |
| one_day_ago = current_time - datetime.timedelta(days=1) | |
| db.remove(Query().timestamp.test(lambda x: datetime.datetime.fromisoformat(x) <= one_day_ago)) | |
| def process_article(new_keyword, other_keywords, action, edited_article_structure): | |
| keyword_id = re.sub(r"\W+", "", new_keyword) if new_keyword else None | |
| response_text = "" | |
| final_article_content = "" | |
| if action == "構成作成": | |
| all_keywords = new_keyword | |
| if other_keywords: | |
| all_keywords += " " + other_keywords | |
| print(f"Running keywords_processor.py with arguments: {new_keyword}, {other_keywords}") | |
| result = subprocess.run(["python3", "keywords_processor.py", new_keyword, other_keywords], capture_output=True, text=True) | |
| print(f"Return code: {result.returncode}") | |
| if result.returncode != 0: | |
| response_text = "Keywords Processor Error: " + result.stderr | |
| print("Keywords Processor Error:", result.stderr) | |
| else: | |
| response_text = result.stdout | |
| print("Keywords Processor Output:", result.stdout) | |
| # headline_generator.py を実行 | |
| print("Running headline_generator.py") | |
| headline_result = subprocess.run(["python3", "headline_generator.py"], capture_output=True, text=True) | |
| print(f"Headline Generator Return code: {headline_result.returncode}") | |
| if headline_result.returncode != 0: | |
| print("Headline Generator Error:", headline_result.stderr) | |
| else: | |
| response_text += headline_result.stdout | |
| print("Headline Generator Output:", headline_result.stdout) | |
| return response_text, None # 編集可能な記事構成にのみ出力 | |
| elif action == "本文作成": | |
| response_text = edited_article_structure | |
| try: | |
| final_article_content = generate_article(edited_article_structure) | |
| with open("output3.txt", "r", encoding="utf-8") as f: | |
| final_article_content = f.read() | |
| except Exception as e: | |
| final_article_content = f"予期せぬエラーが発生しました:{str(e)}" | |
| print(f"予期せぬエラーが発生しました:{str(e)}") | |
| return response_text, final_article_content | |
| with gr.Blocks() as demo: | |
| gr.Markdown("### BabyWriterPRO") | |
| new_keyword = gr.Textbox(label="キーワード") | |
| other_keywords = gr.Textbox(label="その他のキーワード", lines=4) | |
| create_structure_button = gr.Button("構成作成") | |
| editable_output2 = gr.Textbox(label="編集可能な記事構成", lines=10, placeholder="記事構成がここに表示されます") | |
| create_article_button = gr.Button("本文作成") | |
| final_article = gr.Textbox(label="最終的な記事本文", lines=10) | |
| create_structure_button.click( | |
| fn=process_article, | |
| inputs=[new_keyword, other_keywords, gr.State("構成作成"), gr.State("")], | |
| outputs=[editable_output2, final_article] | |
| ) | |
| create_article_button.click( | |
| fn=process_article, | |
| inputs=[new_keyword, other_keywords, gr.State("本文作成"), editable_output2], | |
| outputs=[editable_output2, final_article] | |
| ) | |
| demo.launch() |