import streamlit as st import os import subprocess import re from tinydb import TinyDB, Query import datetime from bs4 import BeautifulSoup import warnings 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)) st.title("BabyWriterPRO") st.write("こちらは、与えられたキーワードを使用して生成します。") # キーワード入力 new_keyword = st.text_input("キーワード:") other_keywords = st.text_area("その他のキーワード(改行またはカンマで区切ってください):", height=300) keyword_id = re.sub(r"\W+", "", new_keyword) if new_keyword else None # output2.txt から内容を読み込む try: with open("output2.txt", "r", encoding="utf-8") as f: editable_output2 = f.read() except FileNotFoundError: editable_output2 = "" # ファイルが存在しない場合は空文字列で初期化 if new_keyword: if other_keywords: process = subprocess.Popen(["python3", "first.py", other_keywords]) process.wait() if st.button("構成作成", key=f"run_button_{keyword_id}"): try: with st.spinner("タイトル、見出し作成中..."): process = subprocess.Popen(["python3", "second.py", new_keyword]) process.wait() with open("output2.txt", "r", encoding="utf-8") as f: editable_output2 = f.read() soup = BeautifulSoup(editable_output2, "html.parser") h_tags = soup.find_all(re.compile("^h[1-3]$")) output2 = st.empty() output2.text(editable_output2) if db.search((Query().name == "output2.txt") & (Query().keyword_id == keyword_id)): db.update({"content": editable_output2, "tags": str(h_tags)}, (Query().name == "output2.txt") & (Query().keyword_id == keyword_id)) else: db.insert({"name": "output2.txt", "content": editable_output2, "tags": str(h_tags), "timestamp": current_time.isoformat(), "keyword_id": keyword_id}) st.success("処理が完了しました。") except subprocess.CalledProcessError: st.error("記事の構成作成中にエラーが発生しました。もう一度お試しください。") except Exception as e: st.error(f"予期せぬエラーが発生しました:{str(e)}") editable_output2 = st.text_area("output2.txtを編集してください:", value=editable_output2) if st.button("本文作成"): try: with st.spinner("本文作成中..."): subprocess.run(["python3", "run_third.py", editable_output2, keyword_id], check=True) with open("output3.txt", "r", encoding="utf-8") as f: content = f.read() output3 = st.empty() output3.text(content) except subprocess.CalledProcessError: st.error("エラーです。やり直してください。") except Exception as e: st.error(f"予期せぬエラーが発生しました:{str(e)}") if st.button("保存"): h2_limit = 5 h3_limit = 10 soup = BeautifulSoup(editable_output2, "html.parser") h2_count = len(soup.find_all("h2")) h3_count = len(soup.find_all("h3")) if h2_count > h2_limit or h3_count > h3_limit: st.error(f"h2タグの数が{h2_limit}を、h3タグの数が{h3_limit}を超えています。") elif not is_valid_html(editable_output2): st.error("入力されたテキストは正しいHTML形式ではありません。") else: with open("output2.txt", "w", encoding="utf-8") as f: f.write(editable_output2) db.upsert({"name": "output2.txt", "content": editable_output2, "timestamp": current_time.isoformat(), "keyword_id": keyword_id}, (Query().name == "output2.txt") & (Query().keyword_id == keyword_id)) st.write("output2.txtに変更が保存されました。") if st.button("データクリア"): db.remove(Query().keyword_id == keyword_id) st.write("データベースがクリアされました。") else: st.warning("キーワードを入力してください。")