Ken-INOUE commited on
Commit
ed624a7
·
1 Parent(s): 5ac3d72

Refactor file handling in diagnosis function and update Gradio UI for file input types

Browse files
Files changed (1) hide show
  1. app.py +8 -17
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import pandas as pd
3
  import json
4
 
5
- # --- 状態判定関数 ---
6
  def judge_status(value, ll, l, h, hh):
7
  if pd.notna(ll) and value < ll:
8
  return "LOW-LOW"
@@ -15,20 +14,19 @@ def judge_status(value, ll, l, h, hh):
15
  else:
16
  return "OK"
17
 
18
- # --- 診断処理 ---
19
  def diagnose_app(csv_file, excel_file, process_name, datetime_str, window_minutes):
20
  if csv_file is None or excel_file is None:
21
  return "⚠ CSVとExcelファイルを両方アップロードしてください", None, None
22
 
23
- # CSV読み込み(3行ヘッダー維持
24
- df = pd.read_csv(csv_file.name, header=[0, 1, 2])
25
  timestamp_col = df.iloc[:, 0]
26
  df = df.drop(df.columns[0], axis=1)
27
  df.insert(0, "timestamp", timestamp_col)
28
  df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
29
 
30
- # 閾値テーブル読み込み
31
- thresholds_df = pd.read_excel(excel_file.name)
32
  thresholds_df["Important"] = thresholds_df["Important"].astype(str).str.upper().map({"TRUE": True, "FALSE": False})
33
  for col in ["LL", "L", "H", "HH"]:
34
  if col in thresholds_df.columns:
@@ -40,20 +38,16 @@ def diagnose_app(csv_file, excel_file, process_name, datetime_str, window_minute
40
  except Exception:
41
  return f"⚠ 入力した日時 {datetime_str} が無効です", None, None
42
 
43
- # 時間幅のデータ抽出
44
  start_time = target_time - pd.Timedelta(minutes=window_minutes)
45
  end_time = target_time
46
  df_window = df[(df["timestamp"] >= start_time) & (df["timestamp"] <= end_time)]
47
-
48
  if df_window.empty:
49
  return "⚠ 指定した時間幅にデータがありません", None, None
50
 
51
- # プロセスの閾値行を抽出
52
  proc_thresholds = thresholds_df[thresholds_df["ProcessNo_ProcessName"] == process_name]
53
  if proc_thresholds.empty:
54
  return f"⚠ プロセス {process_name} の閾値が見つかりません", None, None
55
 
56
- # 判定実行
57
  all_results = []
58
  for _, row in df_window.iterrows():
59
  for _, thr in proc_thresholds.iterrows():
@@ -71,7 +65,6 @@ def diagnose_app(csv_file, excel_file, process_name, datetime_str, window_minute
71
  "時刻": row["timestamp"]
72
  })
73
 
74
- # 集計(件数と割合)
75
  total = len(all_results)
76
  if total == 0:
77
  return f"⚠ プロセス {process_name} の診断結果なし", None, None
@@ -96,14 +89,13 @@ def diagnose_app(csv_file, excel_file, process_name, datetime_str, window_minute
96
 
97
  return summary, result_df, result_json
98
 
99
-
100
  # --- Gradio UI ---
101
  with gr.Blocks() as demo:
102
  gr.Markdown("## 閾値診断アプリ")
103
 
104
  with gr.Row():
105
- csv_input = gr.File(label="CSVファイルをアップロード", type="file")
106
- excel_input = gr.File(label="閾値テーブルをアップロード", type="file")
107
 
108
  process_name = gr.Textbox(label="プロセス名", placeholder="例: E018-A012_除害RO")
109
  datetime_str = gr.Textbox(label="基準日時 (例: 2025/8/1 0:05)")
@@ -123,8 +115,7 @@ with gr.Blocks() as demo:
123
 
124
  if __name__ == "__main__":
125
  import os
126
- # Hugging Face / ローカル両対応
127
- if os.getenv("SPACE_ID"):
128
  demo.launch(server_name="0.0.0.0")
129
- else:
130
  demo.launch()
 
2
  import pandas as pd
3
  import json
4
 
 
5
  def judge_status(value, ll, l, h, hh):
6
  if pd.notna(ll) and value < ll:
7
  return "LOW-LOW"
 
14
  else:
15
  return "OK"
16
 
 
17
  def diagnose_app(csv_file, excel_file, process_name, datetime_str, window_minutes):
18
  if csv_file is None or excel_file is None:
19
  return "⚠ CSVとExcelファイルを両方アップロードしてください", None, None
20
 
21
+ # CSV読み込み(3行ヘッダー)
22
+ df = pd.read_csv(csv_file, header=[0, 1, 2])
23
  timestamp_col = df.iloc[:, 0]
24
  df = df.drop(df.columns[0], axis=1)
25
  df.insert(0, "timestamp", timestamp_col)
26
  df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
27
 
28
+ # 閾値テーブル
29
+ thresholds_df = pd.read_excel(excel_file)
30
  thresholds_df["Important"] = thresholds_df["Important"].astype(str).str.upper().map({"TRUE": True, "FALSE": False})
31
  for col in ["LL", "L", "H", "HH"]:
32
  if col in thresholds_df.columns:
 
38
  except Exception:
39
  return f"⚠ 入力した日時 {datetime_str} が無効です", None, None
40
 
 
41
  start_time = target_time - pd.Timedelta(minutes=window_minutes)
42
  end_time = target_time
43
  df_window = df[(df["timestamp"] >= start_time) & (df["timestamp"] <= end_time)]
 
44
  if df_window.empty:
45
  return "⚠ 指定した時間幅にデータがありません", None, None
46
 
 
47
  proc_thresholds = thresholds_df[thresholds_df["ProcessNo_ProcessName"] == process_name]
48
  if proc_thresholds.empty:
49
  return f"⚠ プロセス {process_name} の閾値が見つかりません", None, None
50
 
 
51
  all_results = []
52
  for _, row in df_window.iterrows():
53
  for _, thr in proc_thresholds.iterrows():
 
65
  "時刻": row["timestamp"]
66
  })
67
 
 
68
  total = len(all_results)
69
  if total == 0:
70
  return f"⚠ プロセス {process_name} の診断結果なし", None, None
 
89
 
90
  return summary, result_df, result_json
91
 
 
92
  # --- Gradio UI ---
93
  with gr.Blocks() as demo:
94
  gr.Markdown("## 閾値診断アプリ")
95
 
96
  with gr.Row():
97
+ csv_input = gr.File(label="CSVファイルをアップロード", type="filepath")
98
+ excel_input = gr.File(label="閾値テーブルをアップロード", type="filepath")
99
 
100
  process_name = gr.Textbox(label="プロセス名", placeholder="例: E018-A012_除害RO")
101
  datetime_str = gr.Textbox(label="基準日時 (例: 2025/8/1 0:05)")
 
115
 
116
  if __name__ == "__main__":
117
  import os
118
+ if os.getenv("SPACE_ID"): # Hugging Face環境
 
119
  demo.launch(server_name="0.0.0.0")
120
+ else: # ローカル
121
  demo.launch()