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.
app.py
CHANGED
|
@@ -68,48 +68,76 @@ def check_thresholds(sensor_df, threshold_df):
|
|
| 68 |
|
| 69 |
return pd.DataFrame(alerts)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|