Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from risk_model import predict_risk, retrain_model, get_history_df | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## π₯ Heating Mantle Safety Risk Predictor") | |
| with gr.Row(): | |
| temp = gr.Number(label="Max Temperature (Β°C)", value=100) | |
| duration = gr.Number(label="Duration (min)", value=30) | |
| with gr.Row(): | |
| predict_btn = gr.Button("π Predict") | |
| retrain_btn = gr.Button("π Retrain Model") | |
| result = gr.Textbox(label="Risk Prediction") | |
| alert = gr.Textbox(label="π¨ Alert Message") | |
| ist_time = gr.Textbox(label="Timestamp (IST)") | |
| retrain_output = gr.Textbox(label="Retrain Status") | |
| summary = gr.Markdown() | |
| history_table = gr.Dataframe(headers=["Temperature", "Duration", "Risk", "Timestamp"], label="π Prediction History") | |
| plot = gr.Plot(label="π Risk Trend Chart") | |
| def classify(temp, duration): | |
| if temp <= 0 or duration <= 0: | |
| return "β Invalid", "Invalid", "Invalid", "", pd.DataFrame(), plt.figure() | |
| risk, timestamp = predict_risk(temp, duration) | |
| if risk == "Low": | |
| alert_msg = "β SAFE - No action needed" | |
| elif risk == "Moderate": | |
| alert_msg = "β οΈ SAFE - Monitor closely" | |
| else: | |
| alert_msg = "π₯ SHUTDOWN - Immediate attention needed" | |
| summary_md = f""" | |
| ### π Summary | |
| - **Max Temp**: {temp} Β°C | |
| - **Duration**: {duration} min | |
| - **Risk**: {risk} | |
| - **Timestamp**: {timestamp} | |
| - **Alert**: {alert_msg} | |
| """ | |
| df = get_history_df() | |
| # Convert Risk to numeric for plotting | |
| risk_map = {'Low': 1, 'Moderate': 2, 'High': 3} | |
| df["Risk_Num"] = df["Risk"].map(risk_map) | |
| fig, ax = plt.subplots(figsize=(6, 3)) | |
| ax.plot(df["Timestamp"], df["Risk_Num"], marker="o", linestyle="-", color="red") | |
| ax.set_ylim(0.5, 3.5) | |
| ax.set_yticks([1, 2, 3]) | |
| ax.set_yticklabels(['Low', 'Moderate', 'High']) | |
| ax.set_title("Risk Level Over Time") | |
| ax.set_xlabel("Timestamp") | |
| ax.set_ylabel("Risk Level") | |
| ax.tick_params(axis='x', rotation=45) | |
| plt.tight_layout() | |
| df_display = df[["Temperature", "Duration", "Risk", "Timestamp"]] | |
| return risk, alert_msg, timestamp, summary_md, df_display, fig | |
| predict_btn.click(classify, inputs=[temp, duration], | |
| outputs=[result, alert, ist_time, summary, history_table, plot]) | |
| retrain_btn.click(retrain_model, outputs=[retrain_output]) | |
| demo.launch() | |