MTeguri commited on
Commit
e3cb81f
·
1 Parent(s): cdd8d39

Add troubleshooting functionality with Gradio interface

Browse files

- Implemented the run_troubleshooting function to process sensor and threshold data.
- Added data loading from Supabase if dataframes are not defined.
- Created a Gradio interface to display troubleshooting information based on alerts.
- Refactored alert processing logic to improve clarity and maintainability.

Files changed (1) hide show
  1. app.py +73 -45
app.py CHANGED
@@ -68,48 +68,76 @@ def check_thresholds(sensor_df, threshold_df):
68
 
69
  return pd.DataFrame(alerts)
70
 
71
- # 実行
72
- alerts_df = check_thresholds(sensor_df, threshold_df)
73
-
74
- # 同じタイムスタンプでdata no.が二つ以上あるデータを抽出
75
- grouped_alerts = alerts_df.groupby('timestamp')['data no.'].nunique()
76
- multiple_data_nos_timestamps = grouped_alerts[grouped_alerts > 1].index.tolist()
77
-
78
- # alerts_df を複数データ番号を持つタイムスタンプでフィルタリング
79
- filtered_alerts_df = alerts_df[alerts_df['timestamp'].isin(multiple_data_nos_timestamps)]
80
-
81
- # タイムスタンプでグループ化し、各タイムスタンプのユニークなデータ番号をリストとして取得
82
- data_nos_by_timestamp = filtered_alerts_df.groupby('timestamp')['data no.'].unique().apply(list)
83
-
84
- # 結果を辞書のリストに変換(オプション。分かりやすさのため)
85
- result_list = []
86
- for timestamp, data_nos in data_nos_by_timestamp.items():
87
- # 数値のリストをカンマ区切りの文字列に変換
88
- data_nos_str = ', '.join(map(str, data_nos))
89
- result_list.append({"timestamp": timestamp, "data_nos": data_nos_str})
90
-
91
- # 辞書のリストをpandas DataFrameに変換
92
- result_df = pd.DataFrame(result_list)
93
-
94
-
95
- # troubleshooting_dfの指標No.列をリストのリストに変換
96
- # 例: '1,2' -> [1, 2]
97
- troubleshooting_indicator_lists = troubleshooting_df['指標No.'].str.split(',').apply(lambda x: [int(i) for i in x])
98
-
99
- # result_dfのdata_nos列をリストのリストに変換
100
- # 例: '5, 25' -> [5, 25]
101
- result_data_nos_lists = result_df['data_nos'].str.split(', ').apply(lambda x: [int(i) for i in x])
102
-
103
- # 組み合わせを持つかどうか判定し、該当する情報を出力
104
- for i, result_nos in enumerate(result_data_nos_lists):
105
- result_timestamp = result_df.loc[i, 'timestamp']
106
- for j, troubleshooting_nos in enumerate(troubleshooting_indicator_lists):
107
- # troubleshooting_nos の組み合わせが result_nos の組み合わせに含まれるか判定
108
- if set(troubleshooting_nos).issubset(set(result_nos)):
109
- troubleshooting_situation = troubleshooting_df.loc[j, 'シチュエーション\n(対応が必要な状況)']
110
- troubleshooting_action = troubleshooting_df.loc[j, 'sub goal到達のために必要な行動\n(解決策)']
111
-
112
- print(f"Timestamp: {result_timestamp}")
113
- print(f"Situation: {troubleshooting_situation}")
114
- print(f"Action: {troubleshooting_action}")
115
- print("-" * 20) # 区切り線
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  return pd.DataFrame(alerts)
70
 
71
+
72
+ import gradio as gr
73
+ import pandas as pd
74
+ import supabase
75
+
76
+ # Assuming the data loading and check_thresholds function from the previous cell are available
77
+
78
+ def run_troubleshooting():
79
+ # Ensure dataframes are accessible, assuming they are global or loaded here
80
+ # If not global, you would need to reload or pass them as arguments
81
+ try:
82
+ threshold_df
83
+ sensor_df
84
+ troubleshooting_df
85
+ except NameError:
86
+ # Reload data if dataframes are not defined
87
+ SUPABASE_URL = userdata.get('SUPABASE_URL')
88
+ SUPABASE_KEY = userdata.get('SUPABASE_KEY')
89
+ supabase_client = supabase.create_client(SUPABASE_URL, SUPABASE_KEY)
90
+ table_threshold = "Threshold_data"
91
+ table_sensor = "Sensor_data"
92
+ table_troubleshooting = "Troubleshooting_collection"
93
+ threshold_data = supabase_client.table(table_threshold).select("*").execute()
94
+ sensor_data = supabase_client.table(table_sensor).select("*").execute()
95
+ troubleshooting_data = supabase_client.table(table_troubleshooting).select("*").execute()
96
+ threshold_df = pd.DataFrame(threshold_data.data)
97
+ sensor_df = pd.DataFrame(sensor_data.data)
98
+ troubleshooting_df = pd.DataFrame(troubleshooting_data.data)
99
+
100
+
101
+ alerts_df = check_thresholds(sensor_df, threshold_df)
102
+
103
+ grouped_alerts = alerts_df.groupby('timestamp')['data no.'].nunique()
104
+ multiple_data_nos_timestamps = grouped_alerts[grouped_alerts > 1].index.tolist()
105
+
106
+ filtered_alerts_df = alerts_df[alerts_df['timestamp'].isin(multiple_data_nos_timestamps)]
107
+
108
+ data_nos_by_timestamp = filtered_alerts_df.groupby('timestamp')['data no.'].unique().apply(list)
109
+
110
+ result_list = []
111
+ for timestamp, data_nos in data_nos_by_timestamp.items():
112
+ data_nos_str = ', '.join(map(str, data_nos))
113
+ result_list.append({"timestamp": timestamp, "data_nos": data_nos_str})
114
+
115
+ result_df = pd.DataFrame(result_list)
116
+
117
+ troubleshooting_indicator_lists = troubleshooting_df['指標No.'].str.split(',').apply(lambda x: [int(i) for i in x])
118
+ result_data_nos_lists = result_df['data_nos'].str.split(', ').apply(lambda x: [int(i) for i in x])
119
+
120
+ output_text = ""
121
+ for i, result_nos in enumerate(result_data_nos_lists):
122
+ result_timestamp = result_df.loc[i, 'timestamp']
123
+ for j, troubleshooting_nos in enumerate(troubleshooting_indicator_lists):
124
+ if set(troubleshooting_nos).issubset(set(result_nos)):
125
+ troubleshooting_situation = troubleshooting_df.loc[j, 'シチュエーション\n(対応が必要な状況)']
126
+ troubleshooting_action = troubleshooting_df.loc[j, 'sub goal到達のために必要な行動\n(解決策)']
127
+
128
+ output_text += f"Timestamp: {result_timestamp}\n"
129
+ output_text += f"Situation: {troubleshooting_situation}\n"
130
+ output_text += f"Action: {troubleshooting_action}\n"
131
+ output_text += "-" * 20 + "\n" # 区切り線
132
+
133
+ return output_text
134
+
135
+ iface = gr.Interface(
136
+ fn=run_troubleshooting,
137
+ inputs=None, # No direct input needed as it uses existing dataframes
138
+ outputs="text",
139
+ title="Troubleshooting Information",
140
+ description="Displays troubleshooting information based on sensor and threshold data."
141
+ )
142
+
143
+ iface.launch(debug=True)