Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from risk_model import predict_risk, retrain_model, get_history_df
|
3 |
import pandas as pd
|
4 |
import matplotlib.pyplot as plt
|
5 |
|
@@ -15,21 +15,20 @@ with gr.Blocks() as demo:
|
|
15 |
retrain_btn = gr.Button("🔁 Retrain Model")
|
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", "
|
25 |
|
26 |
-
plot = gr.Plot(label="📊 Risk
|
27 |
|
28 |
def classify(temp, duration):
|
29 |
if temp <= 0 or duration <= 0:
|
30 |
-
return "❌ Invalid
|
31 |
|
32 |
-
risk,
|
33 |
|
34 |
if risk == "Low":
|
35 |
alert_msg = "✅ SAFE - No action needed"
|
@@ -43,25 +42,16 @@ with gr.Blocks() as demo:
|
|
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 |
-
#
|
54 |
-
|
55 |
-
|
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 |
-
|
61 |
-
|
62 |
-
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
+
from risk_model import predict_risk, retrain_model, get_history_df
|
3 |
import pandas as pd
|
4 |
import matplotlib.pyplot as plt
|
5 |
|
|
|
15 |
retrain_btn = gr.Button("🔁 Retrain Model")
|
16 |
|
17 |
result = gr.Textbox(label="Risk Prediction")
|
|
|
18 |
alert = gr.Textbox(label="🚨 Alert Message")
|
19 |
ist_time = gr.Textbox(label="Timestamp (IST)")
|
20 |
retrain_output = gr.Textbox(label="Retrain Status")
|
21 |
|
22 |
summary = gr.Markdown()
|
23 |
+
history_table = gr.Dataframe(headers=["Temperature", "Duration", "Risk", "Timestamp"], label="📈 Prediction History")
|
24 |
|
25 |
+
plot = gr.Plot(label="📊 Risk Trend Chart")
|
26 |
|
27 |
def classify(temp, duration):
|
28 |
if temp <= 0 or duration <= 0:
|
29 |
+
return "❌ Invalid", "Invalid", "Invalid", "", pd.DataFrame(), plt.figure()
|
30 |
|
31 |
+
risk, timestamp = predict_risk(temp, duration)
|
32 |
|
33 |
if risk == "Low":
|
34 |
alert_msg = "✅ SAFE - No action needed"
|
|
|
42 |
- **Max Temp**: {temp} °C
|
43 |
- **Duration**: {duration} min
|
44 |
- **Risk**: {risk}
|
|
|
45 |
- **Timestamp**: {timestamp}
|
46 |
- **Alert**: {alert_msg}
|
47 |
"""
|
48 |
|
49 |
df = get_history_df()
|
50 |
|
51 |
+
# Convert Risk to numeric for plotting
|
52 |
+
risk_map = {'Low': 1, 'Moderate': 2, 'High': 3}
|
53 |
+
df["Risk_Num"] = df["Risk"].map(risk_map)
|
|
|
|
|
|
|
54 |
|
55 |
+
fig, ax = plt.subplots(figsize=(6, 3))
|
56 |
+
ax.plot(df["Timestamp"], df["Risk_Num"], marker="o", linestyle="-", color="red")
|
57 |
+
a
|
|
|
|
|
|
|
|
|
|