File size: 3,016 Bytes
53267d3
7bffc00
baacba5
 
53267d3
bb607ff
53267d3
 
7bffc00
 
70d0717
7bffc00
 
53267d3
 
 
 
7bffc00
53267d3
 
 
7bffc00
53267d3
baacba5
 
70d0717
baacba5
6227435
 
53267d3
 
 
70d0717
baacba5
6227435
70d0717
baacba5
7bffc00
 
 
 
 
baacba5
 
 
 
 
70d0717
baacba5
 
 
 
 
 
6227435
 
baacba5
6227435
 
c9dc531
 
 
 
 
 
 
 
 
 
 
70d0717
c9dc531
7bffc00
 
 
70d0717
7bffc00
 
 
 
 
70d0717
7bffc00
 
 
 
 
70d0717
7bffc00
c9dc531
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import gradio as gr
from risk_model import predict_risk, get_history_df
import pandas as pd
import matplotlib.pyplot as plt

with gr.Blocks() as demo:
    gr.Markdown("## 🔥 Heating Mantle Safety Risk Predictor")

    # 🔄 Reset button at the top
    with gr.Row():
        reset_btn = gr.Button("🔄 Reset")

    # Input fields
    with gr.Row():
        temp = gr.Number(label="Max Temperature (°C)", value=100)
        duration = gr.Number(label="Duration (min)", value=30)

    # Predict button
    with gr.Row():
        predict_btn = gr.Button("🔍 Predict")

    # Output fields
    result = gr.Textbox(label="Risk Prediction")
    alert = gr.Textbox(label="🚨 Alert Message")
    ist_time = gr.Textbox(label="Timestamp (IST)")
    risk_score = gr.Textbox(label="📊 Risk Score")  # NEW FIELD
    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)
        score = round((temp + duration) / 2, 2)

        alert_msg = {
            "Low": "✅ SAFE - No action needed",
            "Moderate": "⚠️ SAFE - Monitor closely",
            "High": "🔥 SHUTDOWN - Immediate attention needed"
        }.get(risk, "Unknown")

        summary_md = f"""
### 🔎 Summary
- **Max Temp**: {temp} °C  
- **Duration**: {duration} min  
- **Risk Score**: {score}  
- **Risk**: {risk}  
- **Timestamp**: {timestamp}  
- **Alert**: {alert_msg}
"""

        df = get_history_df()
        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, score, summary_md, df_display, fig

    # Function to reset all fields
    def reset_all():
        return (
            100, 30, "", "", "", "", "", pd.DataFrame(columns=["Temperature", "Duration", "Risk", "Timestamp"]), plt.figure()
        )

    predict_btn.click(
        classify,
        inputs=[temp, duration],
        outputs=[result, alert, ist_time, risk_score, summary, history_table, plot]
    )

    reset_btn.click(
        reset_all,
        inputs=[],
        outputs=[temp, duration, result, alert, ist_time, risk_score, summary, history_table, plot]
    )

demo.launch()