Refactor Gradio interface to display ammonia status
Browse files- Replaced the run_troubleshooting function with a new display_message function to show a specific alert regarding ammonia levels.
- Updated the Gradio interface to use Blocks for a more structured layout, including a Markdown header and a non-interactive textbox for status display.
app.py
CHANGED
|
@@ -1,148 +1,10 @@
|
|
| 1 |
-
from dotenv import load_dotenv
|
| 2 |
-
load_dotenv()
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
# 環境変数から取得
|
| 6 |
-
SUPABASE_URL = os.environ.get('SUPABASE_URL')
|
| 7 |
-
|
| 8 |
-
SUPABASE_KEY = os.environ.get('SUPABASE_KEY')
|
| 9 |
-
|
| 10 |
-
import supabase
|
| 11 |
-
table_threshold = "Threshold_data"
|
| 12 |
-
table_sensor = "Sensor_data"
|
| 13 |
-
table_troubleshooting = "Troubleshooting_collection"
|
| 14 |
-
|
| 15 |
-
# クライアントの初期化
|
| 16 |
-
supabase_client = supabase.create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# データ取得
|
| 20 |
-
threshold_data = supabase_client.table(table_threshold).select("*").execute()
|
| 21 |
-
sensor_data = supabase_client.table(table_sensor).select("*").execute()
|
| 22 |
-
troubleshooting_data = supabase_client.table(table_troubleshooting).select("*").execute()
|
| 23 |
-
|
| 24 |
-
import pandas as pd
|
| 25 |
-
threshold_df = pd.DataFrame(threshold_data.data)
|
| 26 |
-
sensor_df = pd.DataFrame(sensor_data.data)
|
| 27 |
-
troubleshooting_df = pd.DataFrame(troubleshooting_data.data)
|
| 28 |
-
|
| 29 |
-
def check_thresholds(sensor_df, threshold_df):
|
| 30 |
-
alerts = []
|
| 31 |
-
|
| 32 |
-
# '下限'と'上限'カラムを数値型に変換。変換できない値はNaNとする。
|
| 33 |
-
threshold_df['下限'] = pd.to_numeric(threshold_df['下限'], errors='coerce')
|
| 34 |
-
threshold_df['上限'] = pd.to_numeric(threshold_df['上限'], errors='coerce')
|
| 35 |
-
|
| 36 |
-
for _, row in threshold_df.iterrows():
|
| 37 |
-
metric = row["指標名"]
|
| 38 |
-
min_val = row["下限"]
|
| 39 |
-
max_val = row["上限"]
|
| 40 |
-
data_no = row["No."] # Get the 'No.' from threshold_df
|
| 41 |
-
|
| 42 |
-
# センサーデータに指標が存在しない場合はスキップ
|
| 43 |
-
if metric not in sensor_df.columns:
|
| 44 |
-
continue
|
| 45 |
-
|
| 46 |
-
# センサーデータの該当カラムを数値型に変換。変換できない値はNaNとする。
|
| 47 |
-
sensor_metric_data = pd.to_numeric(sensor_df[metric], errors='coerce')
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
for i, value in enumerate(sensor_metric_data):
|
| 51 |
-
# Assuming 'datetime' is the timestamp column in sensor_df
|
| 52 |
-
timestamp = sensor_df.loc[i, "datetime"] if "datetime" in sensor_df.columns else i
|
| 53 |
-
|
| 54 |
-
# 下限チェック
|
| 55 |
-
if pd.notna(min_val) and pd.notna(value) and value < min_val:
|
| 56 |
-
alerts.append({
|
| 57 |
-
"timestamp": timestamp,
|
| 58 |
-
"metric": metric,
|
| 59 |
-
"value": value,
|
| 60 |
-
"status": f"下限値 {min_val} 未満",
|
| 61 |
-
"data no.": data_no # Add the 'data no.'
|
| 62 |
-
})
|
| 63 |
-
|
| 64 |
-
# 上限チェック
|
| 65 |
-
if pd.notna(max_val) and pd.notna(value) and value > max_val:
|
| 66 |
-
alerts.append({
|
| 67 |
-
"timestamp": timestamp,
|
| 68 |
-
"metric": metric,
|
| 69 |
-
"value": value,
|
| 70 |
-
"status": f"上限値 {max_val} 超過",
|
| 71 |
-
"data no.": data_no # Add the 'data no.'
|
| 72 |
-
})
|
| 73 |
-
|
| 74 |
-
return pd.DataFrame(alerts)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
import gradio as gr
|
| 78 |
-
import pandas as pd
|
| 79 |
-
import supabase
|
| 80 |
-
|
| 81 |
-
# Assuming the data loading and check_thresholds function from the previous cell are available
|
| 82 |
-
|
| 83 |
-
def run_troubleshooting():
|
| 84 |
-
# Ensure dataframes are accessible, assuming they are global or loaded here
|
| 85 |
-
# If not global, you would need to reload or pass them as arguments
|
| 86 |
-
try:
|
| 87 |
-
threshold_df
|
| 88 |
-
sensor_df
|
| 89 |
-
troubleshooting_df
|
| 90 |
-
except NameError:
|
| 91 |
-
# Reload data if dataframes are not defined
|
| 92 |
-
# SUPABASE_URL = userdata.get('SUPABASE_URL')
|
| 93 |
-
# SUPABASE_KEY = userdata.get('SUPABASE_KEY')
|
| 94 |
-
supabase_client = supabase.create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 95 |
-
table_threshold = "Threshold_data"
|
| 96 |
-
table_sensor = "Sensor_data"
|
| 97 |
-
table_troubleshooting = "Troubleshooting_collection"
|
| 98 |
-
threshold_data = supabase_client.table(table_threshold).select("*").execute()
|
| 99 |
-
sensor_data = supabase_client.table(table_sensor).select("*").execute()
|
| 100 |
-
troubleshooting_data = supabase_client.table(table_troubleshooting).select("*").execute()
|
| 101 |
-
threshold_df = pd.DataFrame(threshold_data.data)
|
| 102 |
-
sensor_df = pd.DataFrame(sensor_data.data)
|
| 103 |
-
troubleshooting_df = pd.DataFrame(troubleshooting_data.data)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
alerts_df = check_thresholds(sensor_df, threshold_df)
|
| 107 |
-
|
| 108 |
-
grouped_alerts = alerts_df.groupby('timestamp')['data no.'].nunique()
|
| 109 |
-
multiple_data_nos_timestamps = grouped_alerts[grouped_alerts > 1].index.tolist()
|
| 110 |
-
|
| 111 |
-
filtered_alerts_df = alerts_df[alerts_df['timestamp'].isin(multiple_data_nos_timestamps)]
|
| 112 |
-
|
| 113 |
-
data_nos_by_timestamp = filtered_alerts_df.groupby('timestamp')['data no.'].unique().apply(list)
|
| 114 |
-
|
| 115 |
-
result_list = []
|
| 116 |
-
for timestamp, data_nos in data_nos_by_timestamp.items():
|
| 117 |
-
data_nos_str = ', '.join(map(str, data_nos))
|
| 118 |
-
result_list.append({"timestamp": timestamp, "data_nos": data_nos_str})
|
| 119 |
-
|
| 120 |
-
result_df = pd.DataFrame(result_list)
|
| 121 |
-
|
| 122 |
-
troubleshooting_indicator_lists = troubleshooting_df['指標No.'].str.split(',').apply(lambda x: [int(i) for i in x])
|
| 123 |
-
result_data_nos_lists = result_df['data_nos'].str.split(', ').apply(lambda x: [int(i) for i in x])
|
| 124 |
-
|
| 125 |
-
output_text = ""
|
| 126 |
-
for i, result_nos in enumerate(result_data_nos_lists):
|
| 127 |
-
result_timestamp = result_df.loc[i, 'timestamp']
|
| 128 |
-
for j, troubleshooting_nos in enumerate(troubleshooting_indicator_lists):
|
| 129 |
-
if set(troubleshooting_nos).issubset(set(result_nos)):
|
| 130 |
-
troubleshooting_situation = troubleshooting_df.loc[j, 'シチュエーション\n(対応が必要な状況)']
|
| 131 |
-
troubleshooting_action = troubleshooting_df.loc[j, 'sub goal到達のために必要な行動\n(解決策)']
|
| 132 |
-
|
| 133 |
-
output_text += f"Timestamp: {result_timestamp}\n"
|
| 134 |
-
output_text += f"Trouble: {troubleshooting_situation}\n"
|
| 135 |
-
output_text += f"Troubleshooting: {troubleshooting_action}\n"
|
| 136 |
-
output_text += "-" * 20 + "\n" # 区切り線
|
| 137 |
|
| 138 |
-
|
|
|
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
outputs="text",
|
| 144 |
-
title="Troubleshooting Information",
|
| 145 |
-
description="Displays troubleshooting information based on sensor and threshold data."
|
| 146 |
-
)
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def display_message():
|
| 4 |
+
return "現在、以下のトラブルが発生しています。アンモニア態窒素が1,000mg/Lを超えました。"
|
| 5 |
|
| 6 |
+
with gr.Blocks() as demo:
|
| 7 |
+
gr.Markdown("# アンモニア態窒素ステータス")
|
| 8 |
+
output_text = gr.Textbox(label="ステータス", value=display_message(), interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
demo.launch(mcp_server=True)
|