Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,29 +16,19 @@ BSKY_PASSWORD = os.getenv("BSKY_PASSWORD", "Raira0626")
|
|
| 16 |
|
| 17 |
api = HfApi()
|
| 18 |
|
| 19 |
-
# --- 便利関数:CSVから選択肢を抽出 ---
|
| 20 |
def get_filter_options():
|
| 21 |
if not os.path.exists(DATA_FILE):
|
| 22 |
return ["すべて"], ["すべて"]
|
| 23 |
-
|
| 24 |
df = pd.read_csv(DATA_FILE)
|
| 25 |
-
|
| 26 |
-
# 投稿者リストの作成
|
| 27 |
-
authors = sorted(df["投稿者"].unique().tolist())
|
| 28 |
-
author_options = ["すべて"] + authors
|
| 29 |
-
|
| 30 |
-
# 本文からハッシュタグ(#...)を抽出してリスト化
|
| 31 |
all_tags = []
|
| 32 |
for text in df["本文"].astype(str):
|
| 33 |
tags = re.findall(r'#\w+', text)
|
| 34 |
all_tags.extend(tags)
|
| 35 |
-
|
| 36 |
hashtags = sorted(list(set(all_tags)))
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return author_options, tag_options
|
| 40 |
|
| 41 |
-
# --- 1. データ取得・保存
|
| 42 |
def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
| 43 |
try:
|
| 44 |
client = Client()
|
|
@@ -58,13 +48,46 @@ def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
|
| 58 |
|
| 59 |
new_data = []
|
| 60 |
for post in all_posts[:max_total]:
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
new_data.append({
|
| 64 |
"日付": post.record.created_at,
|
| 65 |
-
"投稿者":
|
| 66 |
-
"本文":
|
| 67 |
-
"URL":
|
| 68 |
"いいね数": getattr(post, 'like_count', 0)
|
| 69 |
})
|
| 70 |
|
|
@@ -72,10 +95,11 @@ def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
|
| 72 |
df.to_csv(DATA_FILE, index=False, encoding='utf-8-sig')
|
| 73 |
|
| 74 |
if HF_TOKEN:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# 更新後の選択肢を取得
|
| 79 |
a_opt, t_opt = get_filter_options()
|
| 80 |
return f"✅ {len(df)}件 更新完了!", df, gr.update(choices=a_opt), gr.update(choices=t_opt)
|
| 81 |
|
|
@@ -84,20 +108,14 @@ def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
|
| 84 |
|
| 85 |
# --- 2. 検索用関数 ---
|
| 86 |
def search_data(author_val, tag_val, keyword_val):
|
| 87 |
-
if not os.path.exists(DATA_FILE):
|
| 88 |
-
return pd.DataFrame()
|
| 89 |
-
|
| 90 |
df = pd.read_csv(DATA_FILE)
|
| 91 |
-
|
| 92 |
if author_val and author_val != "すべて":
|
| 93 |
df = df[df["投稿者"] == author_val]
|
| 94 |
-
|
| 95 |
if tag_val and tag_val != "すべて":
|
| 96 |
df = df[df["本文"].str.contains(tag_val, na=False)]
|
| 97 |
-
|
| 98 |
if keyword_val:
|
| 99 |
df = df[df["本文"].str.contains(keyword_val, na=False, case=False)]
|
| 100 |
-
|
| 101 |
return df
|
| 102 |
|
| 103 |
# --- UI 構築 ---
|
|
@@ -105,50 +123,29 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 105 |
gr.Markdown("# 👻 青空怪文庫 データベースポータル")
|
| 106 |
|
| 107 |
with gr.Tabs():
|
| 108 |
-
# --- タブ1: 閲覧と検索 ---
|
| 109 |
with gr.TabItem("🔍 データベースを検索"):
|
| 110 |
with gr.Row():
|
| 111 |
author_dropdown = gr.Dropdown(label="投稿者で絞り込む", choices=["すべて"], value="すべて")
|
| 112 |
tag_dropdown = gr.Dropdown(label="ハッシュタグで絞り込む", choices=["すべて"], value="すべて")
|
| 113 |
keyword_search = gr.Textbox(label="本文キーワード検索", placeholder="キーワードを入力...")
|
| 114 |
-
|
| 115 |
with gr.Row():
|
| 116 |
search_btn = gr.Button("検索実行", variant="primary")
|
| 117 |
clear_btn = gr.Button("リセット")
|
| 118 |
-
|
| 119 |
db_table = gr.Dataframe(label="アーカイブ一覧", wrap=True)
|
| 120 |
|
| 121 |
-
|
| 122 |
-
with gr.TabItem("🔄 データを更新"):
|
| 123 |
-
gr.Markdown("Blueskyから最新投稿を取得して、データベースの選択肢も自動更新します。")
|
| 124 |
with gr.Row():
|
| 125 |
tag_input = gr.Textbox(label="取得するハッシュタグ", value="青空怪文庫")
|
| 126 |
count_input = gr.Slider(minimum=50, maximum=500, step=50, label="取得件数", value=300)
|
| 127 |
-
|
| 128 |
-
update_btn = gr.Button("データを取得して保存(上書き)", variant="stop")
|
| 129 |
update_log = gr.Textbox(label="更新ログ", interactive=False)
|
| 130 |
|
| 131 |
-
# --- 起動時の処理 ---
|
| 132 |
-
def
|
| 133 |
-
|
| 134 |
-
df
|
| 135 |
-
return df,
|
| 136 |
-
|
| 137 |
-
demo.load(fn=
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
search_btn.click(fn=search_data, inputs=[author_dropdown, tag_dropdown, keyword_search], outputs=db_table)
|
| 141 |
-
|
| 142 |
-
# リセットボタン
|
| 143 |
-
clear_btn.click(fn=lambda: ["すべて", "すべて", "", pd.read_csv(DATA_FILE) if os.path.exists(DATA_FILE) else None],
|
| 144 |
-
outputs=[author_dropdown, tag_dropdown, keyword_search, db_table])
|
| 145 |
-
|
| 146 |
-
# 更新ボタン(ログ、テーブル、プルダウン2つを同時に更新)
|
| 147 |
-
update_btn.click(
|
| 148 |
-
fn=fetch_and_save,
|
| 149 |
-
inputs=[tag_input, count_input],
|
| 150 |
-
outputs=[update_log, db_table, author_dropdown, tag_dropdown]
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 16 |
|
| 17 |
api = HfApi()
|
| 18 |
|
|
|
|
| 19 |
def get_filter_options():
|
| 20 |
if not os.path.exists(DATA_FILE):
|
| 21 |
return ["すべて"], ["すべて"]
|
|
|
|
| 22 |
df = pd.read_csv(DATA_FILE)
|
| 23 |
+
authors = sorted(df["投稿者"].dropna().unique().tolist())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
all_tags = []
|
| 25 |
for text in df["本文"].astype(str):
|
| 26 |
tags = re.findall(r'#\w+', text)
|
| 27 |
all_tags.extend(tags)
|
|
|
|
| 28 |
hashtags = sorted(list(set(all_tags)))
|
| 29 |
+
return ["すべて"] + authors, ["すべて"] + hashtags
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# --- 1. データ取得・保存・引用解析エンジン ---
|
| 32 |
def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
| 33 |
try:
|
| 34 |
client = Client()
|
|
|
|
| 48 |
|
| 49 |
new_data = []
|
| 50 |
for post in all_posts[:max_total]:
|
| 51 |
+
# 基本情報(タグを付けた人)
|
| 52 |
+
display_author = post.author.handle
|
| 53 |
+
display_text = getattr(post.record, 'text', "")
|
| 54 |
+
display_url = f"https://bsky.app/profile/{post.author.did}/post/{post.uri.split('/')[-1]}"
|
| 55 |
+
|
| 56 |
+
# --- 引用ポストの判定と書き換えロジック ---
|
| 57 |
+
# embed属性が存在し、それが引用ポスト(record)である場合
|
| 58 |
+
if hasattr(post, 'embed') and post.embed is not None:
|
| 59 |
+
# atprotoの構造上、引用データはさらに深い階層にある場合があります
|
| 60 |
+
embed = post.embed
|
| 61 |
+
# 引用されている投稿のデータ(ViewRecord)を探す
|
| 62 |
+
quoted_record = None
|
| 63 |
+
|
| 64 |
+
# 引用のみ、または画像+引用の場合に対応
|
| 65 |
+
if hasattr(embed, 'record'):
|
| 66 |
+
quoted_record = embed.record
|
| 67 |
+
|
| 68 |
+
# 引用先のデータが取得可能な場合(削除されていない等)
|
| 69 |
+
if quoted_record and hasattr(quoted_record, 'author'):
|
| 70 |
+
# 引用元の情報に書き換える
|
| 71 |
+
original_author = quoted_record.author.handle
|
| 72 |
+
# 引用元の本文を取得(階層が深いので安全に取得)
|
| 73 |
+
original_text = ""
|
| 74 |
+
if hasattr(quoted_record, 'value'): # RecordView
|
| 75 |
+
original_text = quoted_record.value.text
|
| 76 |
+
elif hasattr(quoted_record, 'record'): # RecordViewNotFoundなどの対策
|
| 77 |
+
original_text = getattr(quoted_record.record, 'text', "")
|
| 78 |
+
|
| 79 |
+
original_url = f"https://bsky.app/profile/{quoted_record.author.did}/post/{quoted_record.uri.split('/')[-1]}"
|
| 80 |
+
|
| 81 |
+
# データの差し替え
|
| 82 |
+
display_author = original_author
|
| 83 |
+
display_text = f"{original_text}\n\n({post.author.handle} によるタグ付け)"
|
| 84 |
+
display_url = original_url
|
| 85 |
+
|
| 86 |
new_data.append({
|
| 87 |
"日付": post.record.created_at,
|
| 88 |
+
"投稿者": display_author,
|
| 89 |
+
"本文": display_text,
|
| 90 |
+
"URL": display_url,
|
| 91 |
"いいね数": getattr(post, 'like_count', 0)
|
| 92 |
})
|
| 93 |
|
|
|
|
| 95 |
df.to_csv(DATA_FILE, index=False, encoding='utf-8-sig')
|
| 96 |
|
| 97 |
if HF_TOKEN:
|
| 98 |
+
try:
|
| 99 |
+
api.create_repo(repo_id=DATASET_ID, repo_type="dataset", exist_ok=True, token=HF_TOKEN)
|
| 100 |
+
api.upload_file(path_or_fileobj=DATA_FILE, path_in_repo=DATA_FILE, repo_id=DATASET_ID, repo_type="dataset", token=HF_TOKEN)
|
| 101 |
+
except: pass
|
| 102 |
|
|
|
|
| 103 |
a_opt, t_opt = get_filter_options()
|
| 104 |
return f"✅ {len(df)}件 更新完了!", df, gr.update(choices=a_opt), gr.update(choices=t_opt)
|
| 105 |
|
|
|
|
| 108 |
|
| 109 |
# --- 2. 検索用関数 ---
|
| 110 |
def search_data(author_val, tag_val, keyword_val):
|
| 111 |
+
if not os.path.exists(DATA_FILE): return pd.DataFrame()
|
|
|
|
|
|
|
| 112 |
df = pd.read_csv(DATA_FILE)
|
|
|
|
| 113 |
if author_val and author_val != "すべて":
|
| 114 |
df = df[df["投稿者"] == author_val]
|
|
|
|
| 115 |
if tag_val and tag_val != "すべて":
|
| 116 |
df = df[df["本文"].str.contains(tag_val, na=False)]
|
|
|
|
| 117 |
if keyword_val:
|
| 118 |
df = df[df["本文"].str.contains(keyword_val, na=False, case=False)]
|
|
|
|
| 119 |
return df
|
| 120 |
|
| 121 |
# --- UI 構築 ---
|
|
|
|
| 123 |
gr.Markdown("# 👻 青空怪文庫 データベースポータル")
|
| 124 |
|
| 125 |
with gr.Tabs():
|
|
|
|
| 126 |
with gr.TabItem("🔍 データベースを検索"):
|
| 127 |
with gr.Row():
|
| 128 |
author_dropdown = gr.Dropdown(label="投稿者で絞り込む", choices=["すべて"], value="すべて")
|
| 129 |
tag_dropdown = gr.Dropdown(label="ハッシュタグで絞り込む", choices=["すべて"], value="すべて")
|
| 130 |
keyword_search = gr.Textbox(label="本文キーワード検索", placeholder="キーワードを入力...")
|
|
|
|
| 131 |
with gr.Row():
|
| 132 |
search_btn = gr.Button("検索実行", variant="primary")
|
| 133 |
clear_btn = gr.Button("リセット")
|
|
|
|
| 134 |
db_table = gr.Dataframe(label="アーカイブ一覧", wrap=True)
|
| 135 |
|
| 136 |
+
with gr.TabItem("🔄 手動更新"):
|
|
|
|
|
|
|
| 137 |
with gr.Row():
|
| 138 |
tag_input = gr.Textbox(label="取得するハッシュタグ", value="青空怪文庫")
|
| 139 |
count_input = gr.Slider(minimum=50, maximum=500, step=50, label="取得件数", value=300)
|
| 140 |
+
update_btn = gr.Button("データを再取得して保存", variant="stop")
|
|
|
|
| 141 |
update_log = gr.Textbox(label="更新ログ", interactive=False)
|
| 142 |
|
| 143 |
+
# --- 起動時の自動処理 ---
|
| 144 |
+
def auto_init():
|
| 145 |
+
# 起動時に300件取得を実行
|
| 146 |
+
log, df, a_up, t_up = fetch_and_save("#青空怪文庫", 300)
|
| 147 |
+
return df, a_up, t_up
|
| 148 |
+
|
| 149 |
+
demo.load(fn=auto_init, outputs=[db_table, author_dropdown, tag_dropdown])
|
| 150 |
+
|
| 151 |
+
search_btn.click(fn=search_data, inputs=[author_dropdown, tag_dropdown, keyword_search
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|