File size: 4,600 Bytes
aa52b0f
 
 
 
 
 
6849f0f
7936840
aa52b0f
e6d81c6
 
 
 
 
 
6849f0f
aa52b0f
 
 
 
 
f9fefc3
59be2d9
ffa25ff
6849f0f
aa52b0f
6849f0f
ffa25ff
6849f0f
6351052
f9fefc3
 
 
 
 
 
7c899bd
aa52b0f
6849f0f
 
 
aa52b0f
59be2d9
6351052
59be2d9
6849f0f
6351052
b4e0816
2f83a2d
 
6849f0f
 
 
 
 
 
 
 
 
 
ffa25ff
 
6351052
 
 
 
aa52b0f
6351052
38ce015
59be2d9
6351052
59be2d9
6351052
38ce015
6351052
 
6849f0f
6351052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13f095f
6849f0f
 
 
13f095f
6351052
 
 
aa52b0f
f9e7fcb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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("キーワードを入力してください。")