Spaces:
Sleeping
Sleeping
Upload app.py
Browse filesAdd main python files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
|
| 6 |
+
def load_log_file(file):
|
| 7 |
+
"""アップロードされたログファイルを開いて内容を取得"""
|
| 8 |
+
if file is None:
|
| 9 |
+
return None, "ファイルが選択されていません。"
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
with open(file.name, "r", encoding="utf-8") as f:
|
| 13 |
+
content = f.readlines()
|
| 14 |
+
return content, None
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return None, f"エラー: {str(e)}"
|
| 17 |
+
|
| 18 |
+
def search_logs(file, search_word, year, month, day):
|
| 19 |
+
"""ログを検索し、結果を返す"""
|
| 20 |
+
log_lines, error = load_log_file(file)
|
| 21 |
+
if error:
|
| 22 |
+
return [], "エラー: " + error, None, gr.update(visible=True, interactive=False)
|
| 23 |
+
|
| 24 |
+
if not log_lines:
|
| 25 |
+
return [], "ログファイルを先に読み込んでください。", None, gr.update(visible=True, interactive=False)
|
| 26 |
+
|
| 27 |
+
# 両方の検索条件が入力されている場合はエラーを返す
|
| 28 |
+
if search_word.strip() and (year != "----" and month != "----" and day != "----"):
|
| 29 |
+
return [], "エラー: ユーザー検索と日付検索を同時に行うことはできません。", None, gr.update(visible=True, interactive=False)
|
| 30 |
+
|
| 31 |
+
results = []
|
| 32 |
+
|
| 33 |
+
# ユーザー検索
|
| 34 |
+
if search_word.strip():
|
| 35 |
+
results = [line.strip() for line in log_lines if search_word in line]
|
| 36 |
+
|
| 37 |
+
# 日付検索
|
| 38 |
+
elif year != "----" and month != "----" and day != "----":
|
| 39 |
+
try:
|
| 40 |
+
search_date = f"{year}/{month}/{day}"
|
| 41 |
+
start_date = datetime.strptime(search_date, "%Y/%m/%d")
|
| 42 |
+
end_date = start_date + timedelta(days=1)
|
| 43 |
+
results = extract_date_range_logs(log_lines, start_date, end_date)
|
| 44 |
+
except ValueError:
|
| 45 |
+
return [], "無効な日付形式です。", None, gr.update(visible=True, interactive=False)
|
| 46 |
+
|
| 47 |
+
if results:
|
| 48 |
+
results_formatted = [[r] for r in results]
|
| 49 |
+
return results_formatted, f"{len(results)} 件の結果が見つかりました。", results, gr.update(visible=True, interactive=True)
|
| 50 |
+
else:
|
| 51 |
+
return [], "一致する結果が見つかりませんでした。", None, gr.update(visible=True, interactive=False)
|
| 52 |
+
|
| 53 |
+
def extract_date_range_logs(lines, start_date, end_date):
|
| 54 |
+
"""指定された日付範囲のログを抽出する"""
|
| 55 |
+
result = []
|
| 56 |
+
current_date = None
|
| 57 |
+
|
| 58 |
+
for line in lines:
|
| 59 |
+
date_match = re.match(r"(\d{4}/\d{1,2}/\d{1,2})", line)
|
| 60 |
+
if date_match:
|
| 61 |
+
try:
|
| 62 |
+
current_date = datetime.strptime(date_match.group(1), "%Y/%m/%d")
|
| 63 |
+
except ValueError:
|
| 64 |
+
continue
|
| 65 |
+
|
| 66 |
+
if current_date and start_date <= current_date < end_date:
|
| 67 |
+
result.append(line.strip())
|
| 68 |
+
|
| 69 |
+
return result
|
| 70 |
+
|
| 71 |
+
def export_results(results):
|
| 72 |
+
"""検索結果をTXTファイルとしてエクスポート"""
|
| 73 |
+
if not results:
|
| 74 |
+
return None, "エラー: エクスポートするデータがありません。"
|
| 75 |
+
|
| 76 |
+
filename = f"search_results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
|
| 77 |
+
file_path = os.path.join("/tmp", filename) # 一時ディレクトリに保存
|
| 78 |
+
|
| 79 |
+
try:
|
| 80 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 81 |
+
f.write("\n".join(results))
|
| 82 |
+
|
| 83 |
+
return file_path, "TXTファイルのダウンロードが可能になりました。"
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return None, f"エラー: {str(e)}"
|
| 86 |
+
|
| 87 |
+
# Gradio UIの作成
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("# LINEログマネージャー - Hugging Face版")
|
| 90 |
+
|
| 91 |
+
file_input = gr.File(label="ログファイルをアップロード")
|
| 92 |
+
|
| 93 |
+
# ユーザー検索用のテキストボックス
|
| 94 |
+
search_word = gr.Textbox(label="ユーザー検索ワード")
|
| 95 |
+
|
| 96 |
+
# 日付検索用のドロップダウン
|
| 97 |
+
year = gr.Dropdown(choices=["----"] + [str(y) for y in range(2020, 2041)], value="----", label="年")
|
| 98 |
+
month = gr.Dropdown(choices=["----"] + [str(m) for m in range(1, 13)], value="----", label="月")
|
| 99 |
+
day = gr.Dropdown(choices=["----"] + [str(d) for d in range(1, 32)], value="----", label="日")
|
| 100 |
+
|
| 101 |
+
search_button = gr.Button("検索")
|
| 102 |
+
result_table = gr.Dataframe(headers=["検索結果"], row_count=5, interactive=False)
|
| 103 |
+
result_message = gr.Textbox(label="ステータス", interactive=False)
|
| 104 |
+
|
| 105 |
+
# エクスポート機能
|
| 106 |
+
export_button = gr.Button("TXTをダウンロード", visible=True, interactive=False)
|
| 107 |
+
export_output = gr.File(label="ダウンロードリンク", interactive=False)
|
| 108 |
+
export_status = gr.Textbox(label="エクスポートメッセージ", interactive=False)
|
| 109 |
+
|
| 110 |
+
search_button.click(
|
| 111 |
+
search_logs,
|
| 112 |
+
inputs=[file_input, search_word, year, month, day],
|
| 113 |
+
outputs=[result_table, result_message, export_button]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
export_button.click(
|
| 117 |
+
export_results,
|
| 118 |
+
inputs=[result_table],
|
| 119 |
+
outputs=[export_output, export_status]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
gr.Markdown("[📖 ヘルプページ](https://chat.openai.com)")
|
| 123 |
+
gr.Markdown("[🛠 サポート](https://chat.openai.com)")
|
| 124 |
+
gr.Markdown("[© コピーライト](https://chat.openai.com)")
|
| 125 |
+
|
| 126 |
+
# アプリを実行
|
| 127 |
+
demo.launch()
|