Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
-
from risk_model import predict_risk, retrain_model, get_history_df
|
|
|
|
|
3 |
|
4 |
with gr.Blocks() as demo:
|
5 |
gr.Markdown("## 🔥 Heating Mantle Safety Risk Predictor")
|
@@ -14,18 +16,52 @@ with gr.Blocks() as demo:
|
|
14 |
|
15 |
result = gr.Textbox(label="Risk Prediction")
|
16 |
score = gr.Textbox(label="Confidence (%)")
|
|
|
|
|
17 |
retrain_output = gr.Textbox(label="Retrain Status")
|
18 |
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
def classify(temp, duration):
|
22 |
if temp <= 0 or duration <= 0:
|
23 |
-
return "❌ Invalid Input", "Use values > 0",
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
predict_btn.click(classify, inputs=[temp, duration], outputs=[result, score, history_table])
|
29 |
retrain_btn.click(retrain_model, outputs=[retrain_output])
|
30 |
|
31 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from risk_model import predict_risk, retrain_model, get_history_df, get_ist_time
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
|
6 |
with gr.Blocks() as demo:
|
7 |
gr.Markdown("## 🔥 Heating Mantle Safety Risk Predictor")
|
|
|
16 |
|
17 |
result = gr.Textbox(label="Risk Prediction")
|
18 |
score = gr.Textbox(label="Confidence (%)")
|
19 |
+
alert = gr.Textbox(label="🚨 Alert Message")
|
20 |
+
ist_time = gr.Textbox(label="Timestamp (IST)")
|
21 |
retrain_output = gr.Textbox(label="Retrain Status")
|
22 |
|
23 |
+
summary = gr.Markdown()
|
24 |
+
history_table = gr.Dataframe(headers=["Temperature", "Duration", "Risk", "Confidence", "Timestamp"], label="📈 Prediction History")
|
25 |
+
|
26 |
+
plot = gr.Plot(label="📊 Risk Distribution Chart")
|
27 |
|
28 |
def classify(temp, duration):
|
29 |
if temp <= 0 or duration <= 0:
|
30 |
+
return "❌ Invalid Input", "Use values > 0", "N/A", "N/A", "", pd.DataFrame(), plt.figure()
|
31 |
+
|
32 |
+
risk, confidence, timestamp = predict_risk(temp, duration)
|
33 |
+
|
34 |
+
if risk == "Low":
|
35 |
+
alert_msg = "✅ SAFE - No action needed"
|
36 |
+
elif risk == "Moderate":
|
37 |
+
alert_msg = "⚠️ SAFE - Monitor closely"
|
38 |
+
else:
|
39 |
+
alert_msg = "🔥 SHUTDOWN - Immediate attention needed"
|
40 |
+
|
41 |
+
summary_md = f"""
|
42 |
+
### 🔎 Summary
|
43 |
+
- **Max Temp**: {temp} °C
|
44 |
+
- **Duration**: {duration} min
|
45 |
+
- **Risk**: {risk}
|
46 |
+
- **Confidence**: {confidence:.2f}%
|
47 |
+
- **Timestamp**: {timestamp}
|
48 |
+
- **Alert**: {alert_msg}
|
49 |
+
"""
|
50 |
+
|
51 |
+
df = get_history_df()
|
52 |
+
|
53 |
+
# Plot chart
|
54 |
+
fig, ax = plt.subplots()
|
55 |
+
risk_counts = df["Risk"].value_counts()
|
56 |
+
risk_counts.plot(kind="bar", ax=ax, color=["green", "orange", "red"])
|
57 |
+
ax.set_title("Risk Level Distribution")
|
58 |
+
ax.set_ylabel("Count")
|
59 |
+
|
60 |
+
return f"{risk}", f"{confidence:.2f}", alert_msg, timestamp, summary_md, df, fig
|
61 |
+
|
62 |
+
predict_btn.click(classify, inputs=[temp, duration],
|
63 |
+
outputs=[result, score, alert, ist_time, summary, history_table, plot])
|
64 |
|
|
|
65 |
retrain_btn.click(retrain_model, outputs=[retrain_output])
|
66 |
|
67 |
demo.launch()
|