Sanjayraju30 commited on
Commit
6227435
·
verified ·
1 Parent(s): cb1157d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -1,5 +1,5 @@
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
 
@@ -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", "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"
@@ -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
- # 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()
 
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