Ken-INOUE commited on
Commit
1eb5c08
·
1 Parent(s): 19040f3

Update trend detection application to support MCP, enhance forecasting comments, and improve JSON output handling. Adjust Gradio UI for simultaneous web and MCP server launch.

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # 傾向検出アプリ(Gradio版・重要項目ごとの詳細+JSONダウンロード修正版
2
 
3
  import gradio as gr
4
  import pandas as pd
@@ -8,10 +8,9 @@ import json
8
  import tempfile
9
  import os
10
 
11
- # --- 状態判定&未来予測関数 ---
12
  def detect_trends_with_forecast(process_name, datetime_str, window_minutes, forecast_minutes, csv_file, excel_file):
13
  try:
14
- # ★ 修正ポイント: gr.File(type="filepath") は文字列パスが来る
15
  csv_path = csv_file
16
  excel_path = excel_file
17
 
@@ -22,7 +21,7 @@ def detect_trends_with_forecast(process_name, datetime_str, window_minutes, fore
22
  df.insert(0, "timestamp", timestamp_col)
23
  df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
24
 
25
- # 閾値テーブル読み込み
26
  thresholds_df = pd.read_excel(excel_path)
27
  thresholds_df["Important"] = thresholds_df["Important"].astype(str).str.upper().map({"TRUE": True, "FALSE": False})
28
  for col in ["LL", "L", "H", "HH"]:
@@ -69,7 +68,7 @@ def detect_trends_with_forecast(process_name, datetime_str, window_minutes, fore
69
  last_val = series.iloc[-1]
70
  n = len(series)
71
 
72
- # 未来予測(最小1ステップ)
73
  forecast_steps = max(1, int(round(forecast_minutes / interval_minutes)))
74
  forecast_index = n + forecast_steps
75
  forecast_val = model.predict([[forecast_index]])[0][0]
@@ -114,18 +113,14 @@ def detect_trends_with_forecast(process_name, datetime_str, window_minutes, fore
114
 
115
  result_df = pd.DataFrame(results)
116
 
117
- # JSONテキ
118
  result_json = json.dumps(results, ensure_ascii=False, indent=2)
119
-
120
- # ★ 修正ポイント: BytesIO ではなく、一時ファイルに保存して「パス」を返す
121
  fd, tmp_path = tempfile.mkstemp(suffix=".json", prefix="trend_result_")
122
  os.close(fd)
123
  with open(tmp_path, "w", encoding="utf-8") as f:
124
  f.write(result_json)
125
 
126
  summary = f"✅ {process_name} の傾向検出+未来予測完了({start_time}~{end_time}, 予測+{forecast_minutes}分)"
127
-
128
- # gr.File 出力には「ファイルパス(文字列)」を返す
129
  return result_df, summary, tmp_path
130
 
131
  except Exception as e:
@@ -152,7 +147,6 @@ with gr.Blocks() as demo:
152
  with gr.Row():
153
  result_table = gr.Dataframe(label="傾向+未来予測結果", wrap=True)
154
  summary_output = gr.Textbox(label="サマリー")
155
- # ★ 修正ポイント: gr.File の出力にはパスを渡す。type指定は不要(出力)
156
  json_download = gr.File(label="JSON結果ダウンロード")
157
 
158
  run_btn.click(
@@ -162,8 +156,10 @@ with gr.Blocks() as demo:
162
  )
163
 
164
  if __name__ == "__main__":
165
- use_mcp = os.getenv("USE_MCP", "0") == "1"
166
- if use_mcp:
167
- demo.launch(server_name="0.0.0.0", mcp_server=True)
168
- else:
169
- demo.launch(server_name="0.0.0.0")
 
 
 
1
+ # 傾向検出アプリ(Gradio版・MCP対応
2
 
3
  import gradio as gr
4
  import pandas as pd
 
8
  import tempfile
9
  import os
10
 
11
+ # --- 傾向検出+未来予測関数 ---
12
  def detect_trends_with_forecast(process_name, datetime_str, window_minutes, forecast_minutes, csv_file, excel_file):
13
  try:
 
14
  csv_path = csv_file
15
  excel_path = excel_file
16
 
 
21
  df.insert(0, "timestamp", timestamp_col)
22
  df["timestamp"] = pd.to_datetime(df["timestamp"], errors="coerce")
23
 
24
+ # 閾値テーブル
25
  thresholds_df = pd.read_excel(excel_path)
26
  thresholds_df["Important"] = thresholds_df["Important"].astype(str).str.upper().map({"TRUE": True, "FALSE": False})
27
  for col in ["LL", "L", "H", "HH"]:
 
68
  last_val = series.iloc[-1]
69
  n = len(series)
70
 
71
+ # 未来予測
72
  forecast_steps = max(1, int(round(forecast_minutes / interval_minutes)))
73
  forecast_index = n + forecast_steps
74
  forecast_val = model.predict([[forecast_index]])[0][0]
 
113
 
114
  result_df = pd.DataFrame(results)
115
 
116
+ # JSON保存(ファイルパを返す)
117
  result_json = json.dumps(results, ensure_ascii=False, indent=2)
 
 
118
  fd, tmp_path = tempfile.mkstemp(suffix=".json", prefix="trend_result_")
119
  os.close(fd)
120
  with open(tmp_path, "w", encoding="utf-8") as f:
121
  f.write(result_json)
122
 
123
  summary = f"✅ {process_name} の傾向検出+未来予測完了({start_time}~{end_time}, 予測+{forecast_minutes}分)"
 
 
124
  return result_df, summary, tmp_path
125
 
126
  except Exception as e:
 
147
  with gr.Row():
148
  result_table = gr.Dataframe(label="傾向+未来予測結果", wrap=True)
149
  summary_output = gr.Textbox(label="サマリー")
 
150
  json_download = gr.File(label="JSON結果ダウンロード")
151
 
152
  run_btn.click(
 
156
  )
157
 
158
  if __name__ == "__main__":
159
+ # Web UI MCP を同時に有効化
160
+ demo.launch(
161
+ server_name="0.0.0.0",
162
+ mcp_server=True, # MCP サーバー起動
163
+ share=False, # Hugging Face なら不要
164
+ ssr_mode=False # SSR は無効に
165
+ )