SPARC64 commited on
Commit
65d5c85
·
verified ·
1 Parent(s): 56007d7

Update app.py

Browse files

Add error message

Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -11,34 +11,34 @@ JST = timezone(timedelta(hours=9))
11
  def load_log_file(file):
12
  """アップロードされたログファイルを開き、内容を取得"""
13
  if not file:
14
- return None, "エラー: ファイルが選択されていません。"
15
 
16
  # ファイルの拡張子をチェック (.txt 以外はエラー)
17
  if not file.name.endswith(".txt"):
18
- return None, "エラー (FILE-005): サポートされていないファイル形式です。TXTファイルを選択してください。"
19
 
20
  try:
21
  with open(file.name, "r", encoding="utf-8") as f:
22
  content = f.readlines()
23
  return content, None
24
  except Exception as e:
25
- return None, f"エラー: {str(e)}"
26
 
27
  def search_logs(file, keyword, year, month, day):
28
  """ログを検索し、結果を返す"""
29
  log_lines, error = load_log_file(file)
30
  if error:
31
- return "", error, None
32
 
33
  if not log_lines:
34
- return "", "エラー: ログファイルが空です。", None
35
 
36
  # 検索条件のチェック
37
  is_word_search = bool(keyword.strip())
38
  is_date_search = year != "----" and month != "----" and day != "----"
39
 
40
  if is_word_search and is_date_search:
41
- return "", "エラー: ワード検索と日付検索は同時に行えません。", None
42
 
43
  # 検索条件の設定
44
  search_condition = "検索条件: なし"
@@ -57,9 +57,9 @@ def search_logs(file, keyword, year, month, day):
57
  results = extract_date_range_logs(log_lines, start_date, end_date)
58
  results = [f"- {line.strip()}" for line in results]
59
  except ValueError:
60
- return "", "エラー: 無効な日付形式です。", None
61
 
62
- formatted_results = "\n".join(results) if results else "一致する結果が見つかりませんでした。"
63
 
64
  return formatted_results, f"{len(results)} 件の結果が見つかりました。", search_condition
65
 
@@ -84,7 +84,7 @@ def extract_date_range_logs(lines, start_date, end_date):
84
  def generate_download_link(results, search_condition):
85
  """検索結果をTXTファイルとしてbase64エンコードし、ダウンロードリンクを生成"""
86
  if not results:
87
- return "エラー: エクスポートするデータがありません。"
88
 
89
  #↓はエクスポートファイル名に挿入する用
90
  execution_time = datetime.now(JST).strftime("%Y%m%d_%H%M%S")
@@ -115,6 +115,11 @@ def generate_download_link(results, search_condition):
115
  href = f'<a href="data:text/plain;base64,{encoded_file}" download="{file_name}">結果をダウンロード</a>'
116
  return href
117
 
 
 
 
 
 
118
  # Gradio UI
119
  with gr.Blocks() as demo:
120
  gr.Markdown("# LINEログマネージャー - Hugging Face版")
 
11
  def load_log_file(file):
12
  """アップロードされたログファイルを開き、内容を取得"""
13
  if not file:
14
+ return None, "エラー(FILE-001): ファイルが選択されていません"
15
 
16
  # ファイルの拡張子をチェック (.txt 以外はエラー)
17
  if not file.name.endswith(".txt"):
18
+ return None, "エラー(FILE-005): サポートされていないファイル形式です。TXTファイルを選択してください。"
19
 
20
  try:
21
  with open(file.name, "r", encoding="utf-8") as f:
22
  content = f.readlines()
23
  return content, None
24
  except Exception as e:
25
+ return None, "エラー(FILE-004): ファイルのエンコード形式が不正です(UTF-8のみ対応)"
26
 
27
  def search_logs(file, keyword, year, month, day):
28
  """ログを検索し、結果を返す"""
29
  log_lines, error = load_log_file(file)
30
  if error:
31
+ return "", "エラー(FILE-002): ファイルの読み込みに失敗しました", None
32
 
33
  if not log_lines:
34
+ return "", "エラー(FILE-003): ファイルが空です。", None
35
 
36
  # 検索条件のチェック
37
  is_word_search = bool(keyword.strip())
38
  is_date_search = year != "----" and month != "----" and day != "----"
39
 
40
  if is_word_search and is_date_search:
41
+ return "", "エラー(SEARCH-001): ワード検索と日付検索は同時に行えません。", None
42
 
43
  # 検索条件の設定
44
  search_condition = "検索条件: なし"
 
57
  results = extract_date_range_logs(log_lines, start_date, end_date)
58
  results = [f"- {line.strip()}" for line in results]
59
  except ValueError:
60
+ return "", "エラー(SEARCH-002): 無効な日付形式です。", None
61
 
62
+ formatted_results = "\n".join(results) if results else "SEARCH-003: 一致する結果が見つかりませんでした。"
63
 
64
  return formatted_results, f"{len(results)} 件の結果が見つかりました。", search_condition
65
 
 
84
  def generate_download_link(results, search_condition):
85
  """検索結果をTXTファイルとしてbase64エンコードし、ダウンロードリンクを生成"""
86
  if not results:
87
+ return "エラー(EXPORT-001): エクスポートするデータがありません。"
88
 
89
  #↓はエクスポートファイル名に挿入する用
90
  execution_time = datetime.now(JST).strftime("%Y%m%d_%H%M%S")
 
115
  href = f'<a href="data:text/plain;base64,{encoded_file}" download="{file_name}">結果をダウンロード</a>'
116
  return href
117
 
118
+ if not encoded_file:
119
+ return "エラー(EXPORT-002): エクスポートファイルの作成に失敗しました"
120
+
121
+
122
+
123
  # Gradio UI
124
  with gr.Blocks() as demo:
125
  gr.Markdown("# LINEログマネージャー - Hugging Face版")