Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,6 +31,10 @@ def get_filter_options():
|
|
| 31 |
except:
|
| 32 |
return ["すべて"], ["すべて"]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# --- 1. データ取得・保存・引用解析エンジン ---
|
| 35 |
def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
| 36 |
try:
|
|
@@ -93,8 +97,12 @@ def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
|
| 93 |
api.upload_file(path_or_fileobj=DATA_FILE, path_in_repo=DATA_FILE, repo_id=DATASET_ID, repo_type="dataset", token=HF_TOKEN)
|
| 94 |
except: pass
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
a_opt, t_opt = get_filter_options()
|
| 97 |
-
return f"✅ {len(df)}件 更新完了!",
|
| 98 |
|
| 99 |
except Exception:
|
| 100 |
return f"❌ エラー:\n{traceback.format_exc()}", pd.DataFrame(), gr.update(), gr.update()
|
|
@@ -109,10 +117,13 @@ def search_data(author_val, tag_val, keyword_val):
|
|
| 109 |
df = df[df["本文"].str.contains(tag_val, na=False)]
|
| 110 |
if keyword_val:
|
| 111 |
df = df[df["本文"].str.contains(keyword_val, na=False, case=False)]
|
|
|
|
|
|
|
|
|
|
| 112 |
return df
|
| 113 |
|
| 114 |
# --- UI 構築 ---
|
| 115 |
-
with gr.Blocks(
|
| 116 |
gr.Markdown("# 👻 青空怪文庫 データベースポータル")
|
| 117 |
|
| 118 |
with gr.Tabs():
|
|
@@ -125,12 +136,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 125 |
search_btn = gr.Button("検索実行", variant="primary")
|
| 126 |
clear_btn = gr.Button("リセット")
|
| 127 |
|
| 128 |
-
# URL
|
| 129 |
db_table = gr.Dataframe(
|
| 130 |
label="アーカイブ一覧",
|
| 131 |
wrap=True,
|
| 132 |
-
datatype=["str", "str", "str", "
|
| 133 |
-
link_column_indices=[3], # URLが4番目の列(インデックス3)なので指定
|
| 134 |
type="pandas"
|
| 135 |
)
|
| 136 |
|
|
@@ -154,7 +164,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 154 |
)
|
| 155 |
|
| 156 |
clear_btn.click(
|
| 157 |
-
fn=lambda: [
|
| 158 |
outputs=[db_table, author_dropdown, tag_dropdown, keyword_search]
|
| 159 |
)
|
| 160 |
|
|
@@ -165,4 +175,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 165 |
)
|
| 166 |
|
| 167 |
if __name__ == "__main__":
|
| 168 |
-
|
|
|
|
|
|
| 31 |
except:
|
| 32 |
return ["すべて"], ["すべて"]
|
| 33 |
|
| 34 |
+
# URLをHTMLのリンクタグに変換するヘルパー
|
| 35 |
+
def make_clickable(url):
|
| 36 |
+
return f'<a href="{url}" target="_blank" style="color: #008DFF; text-decoration: underline;">ポストを見る</a>'
|
| 37 |
+
|
| 38 |
# --- 1. データ取得・保存・引用解析エンジン ---
|
| 39 |
def fetch_and_save(target_hashtag="#青空怪文庫", max_total=300):
|
| 40 |
try:
|
|
|
|
| 97 |
api.upload_file(path_or_fileobj=DATA_FILE, path_in_repo=DATA_FILE, repo_id=DATASET_ID, repo_type="dataset", token=HF_TOKEN)
|
| 98 |
except: pass
|
| 99 |
|
| 100 |
+
# 表示用にURLをリンク化
|
| 101 |
+
display_df = df.copy()
|
| 102 |
+
display_df['URL'] = display_df['URL'].apply(make_clickable)
|
| 103 |
+
|
| 104 |
a_opt, t_opt = get_filter_options()
|
| 105 |
+
return f"✅ {len(df)}件 更新完了!", display_df, gr.update(choices=a_opt), gr.update(choices=t_opt)
|
| 106 |
|
| 107 |
except Exception:
|
| 108 |
return f"❌ エラー:\n{traceback.format_exc()}", pd.DataFrame(), gr.update(), gr.update()
|
|
|
|
| 117 |
df = df[df["本文"].str.contains(tag_val, na=False)]
|
| 118 |
if keyword_val:
|
| 119 |
df = df[df["本文"].str.contains(keyword_val, na=False, case=False)]
|
| 120 |
+
|
| 121 |
+
# 表示用にURLをリンク化
|
| 122 |
+
df['URL'] = df['URL'].apply(make_clickable)
|
| 123 |
return df
|
| 124 |
|
| 125 |
# --- UI 構築 ---
|
| 126 |
+
with gr.Blocks() as demo:
|
| 127 |
gr.Markdown("# 👻 青空怪文庫 データベースポータル")
|
| 128 |
|
| 129 |
with gr.Tabs():
|
|
|
|
| 136 |
search_btn = gr.Button("検索実行", variant="primary")
|
| 137 |
clear_btn = gr.Button("リセット")
|
| 138 |
|
| 139 |
+
# URLをHTMLとして描画するために datatype="html" を指定
|
| 140 |
db_table = gr.Dataframe(
|
| 141 |
label="アーカイブ一覧",
|
| 142 |
wrap=True,
|
| 143 |
+
datatype=["str", "str", "str", "html", "number"],
|
|
|
|
| 144 |
type="pandas"
|
| 145 |
)
|
| 146 |
|
|
|
|
| 164 |
)
|
| 165 |
|
| 166 |
clear_btn.click(
|
| 167 |
+
fn=lambda: [search_data("すべて", "すべて", ""), "すべて", "すべて", ""],
|
| 168 |
outputs=[db_table, author_dropdown, tag_dropdown, keyword_search]
|
| 169 |
)
|
| 170 |
|
|
|
|
| 175 |
)
|
| 176 |
|
| 177 |
if __name__ == "__main__":
|
| 178 |
+
# themeはlaunch()に移動して警告を解消
|
| 179 |
+
demo.launch(theme=gr.themes.Soft())
|