Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import re
|
| 3 |
import os
|
| 4 |
import base64
|
| 5 |
-
import time
|
| 6 |
from datetime import datetime, timedelta, timezone
|
| 7 |
|
| 8 |
# 設定
|
|
@@ -14,30 +14,47 @@ TS_URL = "https://github.com/X1288664/LINE-Log-Manager-for-Hugging-face/blob/tro
|
|
| 14 |
JST = timezone(timedelta(hours=9))
|
| 15 |
|
| 16 |
def load_log_file(file, progress=gr.Progress()):
|
| 17 |
-
"""
|
| 18 |
if not file:
|
| 19 |
-
return None, "エラー(FILE-001):
|
| 20 |
|
| 21 |
if not file.name.endswith(".txt"):
|
| 22 |
-
return None, "エラー(FILE-005):
|
| 23 |
|
| 24 |
try:
|
| 25 |
with open(file.name, "r", encoding="utf-8") as f:
|
| 26 |
content = f.readlines()
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
time.sleep(0.01) # 処理シミュレーション
|
| 31 |
|
| 32 |
return content, None
|
| 33 |
except Exception as e:
|
| 34 |
-
return None, "エラー(FILE-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def search_logs(file, keyword, year, month, day, progress=gr.Progress()):
|
| 37 |
-
"""
|
| 38 |
log_lines, error = load_log_file(file)
|
| 39 |
if error:
|
| 40 |
-
return "",
|
| 41 |
|
| 42 |
if not log_lines:
|
| 43 |
return "", "エラー(FILE-003): ファイルが空です。", None
|
|
@@ -68,12 +85,11 @@ def search_logs(file, keyword, year, month, day, progress=gr.Progress()):
|
|
| 68 |
except ValueError:
|
| 69 |
return "", "エラー(SEARCH-002): 無効な日付形式です。", None
|
| 70 |
|
| 71 |
-
formatted_results = "\n".join(results) if results else "SEARCH-003: 一致する結果が見つかりませんでした。"
|
| 72 |
-
|
| 73 |
return formatted_results, f"{len(results)} 件の結果が見つかりました。", search_condition
|
| 74 |
|
| 75 |
def generate_download_link(results, search_condition, progress=gr.Progress()):
|
| 76 |
-
"""
|
| 77 |
if not results:
|
| 78 |
return "エラー(EXPORT-001): エクスポートするデータがありません。"
|
| 79 |
|
|
@@ -100,14 +116,12 @@ def generate_download_link(results, search_condition, progress=gr.Progress()):
|
|
| 100 |
|
| 101 |
encoded_file = base64.b64encode(file_content.encode()).decode()
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
time.sleep(0.01) # 処理シミュレーション
|
| 106 |
|
| 107 |
href = f'<a href="data:text/plain;base64,{encoded_file}" download="{file_name}">結果をダウンロード</a>'
|
| 108 |
return href
|
| 109 |
|
| 110 |
-
# Gradio UI
|
| 111 |
with gr.Blocks() as demo:
|
| 112 |
gr.Markdown("# LINEログマネージャー - Hugging Face版")
|
| 113 |
|
|
@@ -129,27 +143,8 @@ with gr.Blocks() as demo:
|
|
| 129 |
export_output = gr.HTML()
|
| 130 |
export_status = gr.Textbox(label="エクスポートメッセージ", interactive=False)
|
| 131 |
|
| 132 |
-
search_button.click(
|
| 133 |
-
|
| 134 |
-
inputs=[file_input, search_word, year, month, day],
|
| 135 |
-
outputs=[result_table, result_message, search_condition_state]
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
export_button.click(
|
| 139 |
-
generate_download_link,
|
| 140 |
-
inputs=[result_table, search_condition_state],
|
| 141 |
-
outputs=[export_output]
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
gr.Markdown(f"[📖 ヘルプページ]({HELP_URL})")
|
| 145 |
-
gr.Markdown(f"⚠[トラブルシューティング]({TS_URL})")
|
| 146 |
-
gr.Markdown(f"[?問い合わせフォーム]({CONTACT_URL})")
|
| 147 |
-
gr.Markdown(f"[🛠 サポート]({SUPPORT_URL})")
|
| 148 |
-
gr.Markdown(f"[© コピーライト]({COPYRIGHT_URL})")
|
| 149 |
-
gr.Markdown("---")
|
| 150 |
-
gr.Markdown("This project code was partially generated with assistance from [OpenAI's ChatGPT.](https://chatgpt.com/)")
|
| 151 |
-
gr.Markdown("This Application is Confidential.")
|
| 152 |
-
gr.Markdown("---")
|
| 153 |
-
gr.Markdown("Hugging Face Edition Ver.2.0.0")
|
| 154 |
|
|
|
|
| 155 |
demo.launch()
|
|
|
|
| 2 |
import re
|
| 3 |
import os
|
| 4 |
import base64
|
| 5 |
+
import time
|
| 6 |
from datetime import datetime, timedelta, timezone
|
| 7 |
|
| 8 |
# 設定
|
|
|
|
| 14 |
JST = timezone(timedelta(hours=9))
|
| 15 |
|
| 16 |
def load_log_file(file, progress=gr.Progress()):
|
| 17 |
+
"""ログファイルを読み込む(プログレスバー付き)"""
|
| 18 |
if not file:
|
| 19 |
+
return None, "エラー(FILE-001): ファイルが選択されていません。"
|
| 20 |
|
| 21 |
if not file.name.endswith(".txt"):
|
| 22 |
+
return None, "エラー(FILE-005): TXTファイルを選択してください。"
|
| 23 |
|
| 24 |
try:
|
| 25 |
with open(file.name, "r", encoding="utf-8") as f:
|
| 26 |
content = f.readlines()
|
| 27 |
+
|
| 28 |
+
for _ in progress.tqdm(range(1, 101), desc="ログファイルを読み込み中..."):
|
| 29 |
+
time.sleep(0.01)
|
|
|
|
| 30 |
|
| 31 |
return content, None
|
| 32 |
except Exception as e:
|
| 33 |
+
return None, f"エラー(FILE-002): ファイルの読み込みに失敗しました ({str(e)})"
|
| 34 |
+
|
| 35 |
+
def extract_date_range_logs(lines, start_date, end_date):
|
| 36 |
+
"""指定された日付範囲のログを抽出"""
|
| 37 |
+
results = []
|
| 38 |
+
current_date = None
|
| 39 |
+
|
| 40 |
+
for line in lines:
|
| 41 |
+
date_match = re.match(r"(\d{4}/\d{1,2}/\d{1,2})", line)
|
| 42 |
+
if date_match:
|
| 43 |
+
try:
|
| 44 |
+
current_date = datetime.strptime(date_match.group(1), "%Y/%m/%d")
|
| 45 |
+
except ValueError:
|
| 46 |
+
continue # 無効な日付フォーマットはスキップ
|
| 47 |
+
|
| 48 |
+
if current_date and start_date <= current_date < end_date:
|
| 49 |
+
results.append(line.strip())
|
| 50 |
+
|
| 51 |
+
return results
|
| 52 |
|
| 53 |
def search_logs(file, keyword, year, month, day, progress=gr.Progress()):
|
| 54 |
+
"""ログ検索(プログレスバー付き)"""
|
| 55 |
log_lines, error = load_log_file(file)
|
| 56 |
if error:
|
| 57 |
+
return "", error, None
|
| 58 |
|
| 59 |
if not log_lines:
|
| 60 |
return "", "エラー(FILE-003): ファイルが空です。", None
|
|
|
|
| 85 |
except ValueError:
|
| 86 |
return "", "エラー(SEARCH-002): 無効な日付形式です。", None
|
| 87 |
|
| 88 |
+
formatted_results = "\n".join(results) if results else "エラー(SEARCH-003): 一致する結果が見つかりませんでした。"
|
|
|
|
| 89 |
return formatted_results, f"{len(results)} 件の結果が見つかりました。", search_condition
|
| 90 |
|
| 91 |
def generate_download_link(results, search_condition, progress=gr.Progress()):
|
| 92 |
+
"""エクスポート処理(プログレスバー付き)"""
|
| 93 |
if not results:
|
| 94 |
return "エラー(EXPORT-001): エクスポートするデータがありません。"
|
| 95 |
|
|
|
|
| 116 |
|
| 117 |
encoded_file = base64.b64encode(file_content.encode()).decode()
|
| 118 |
|
| 119 |
+
for _ in progress.tqdm(range(1, 101), desc="エクスポート処理中..."):
|
| 120 |
+
time.sleep(0.01)
|
|
|
|
| 121 |
|
| 122 |
href = f'<a href="data:text/plain;base64,{encoded_file}" download="{file_name}">結果をダウンロード</a>'
|
| 123 |
return href
|
| 124 |
|
|
|
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
gr.Markdown("# LINEログマネージャー - Hugging Face版")
|
| 127 |
|
|
|
|
| 143 |
export_output = gr.HTML()
|
| 144 |
export_status = gr.Textbox(label="エクスポートメッセージ", interactive=False)
|
| 145 |
|
| 146 |
+
search_button.click(search_logs, inputs=[file_input, search_word, year, month, day], outputs=[result_table, result_message, search_condition_state])
|
| 147 |
+
export_button.click(generate_download_link, inputs=[result_table, search_condition_state], outputs=[export_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
gr.Markdown("ver.2.0.0-a")
|
| 150 |
demo.launch()
|