File size: 3,671 Bytes
b184017
758ec90
 
d360462
 
 
758ec90
 
28e2062
758ec90
 
 
 
 
 
 
d360462
 
 
 
 
44497a1
4219831
d360462
758ec90
ccca4d0
758ec90
 
94e858c
758ec90
94e858c
2d45a2d
 
2016c50
94e858c
 
 
 
a553e7e
94e858c
e34fa88
372ad1d
 
 
 
 
 
 
 
 
 
ccca4d0
758ec90
 
00a64ba
758ec90
00a64ba
e34fa88
 
758ec90
ccca4d0
d360462
00a64ba
7cc69de
758ec90
 
c06fe91
ccca4d0
4775e2a
d0abdb9
4775e2a
d0abdb9
758ec90
4775e2a
d0abdb9
4775e2a
 
 
 
 
 
 
d0abdb9
 
758ec90
d0abdb9
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
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()