codeboosterstech commited on
Commit
d7fe6e1
·
verified ·
1 Parent(s): bb9a07b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -19
app.py CHANGED
@@ -1,28 +1,67 @@
1
  import gradio as gr
2
- import joblib
3
  import numpy as np
 
 
 
 
 
 
4
 
5
  # Load trained model
6
- model = joblib.load("model/house_price_model.pkl")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Define prediction function
9
- def predict_price(rm, lstat, ptratio):
10
  input_data = np.array([[rm, lstat, ptratio]])
11
  prediction = model.predict(input_data)[0]
12
  return round(prediction, 2)
13
 
14
- # Define Gradio interface
15
- iface = gr.Interface(
16
- fn=predict_price,
17
- inputs=[
18
- gr.Slider(3.0, 9.0, step=0.1, label="Average Rooms (RM)"),
19
- gr.Slider(1.0, 40.0, step=0.1, label="Lower Status Population (%) (LSTAT)"),
20
- gr.Slider(12.0, 22.0, step=0.1, label="Pupil-Teacher Ratio (PTRATIO)")
21
- ],
22
- outputs=gr.Number(label="Predicted House Price (in $1000s)"),
23
- title="🏠 Boston Housing Price Predictor",
24
- description="Enter the house features to predict the price using a linear regression model.",
25
- )
26
-
27
- # Launch app
28
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import numpy as np
3
+ import joblib
4
+ import os
5
+ import pandas as pd
6
+
7
+ from evidently.report import Report
8
+ from evidently.metrics import DataDriftPreset
9
 
10
  # Load trained model
11
+ model_path = "model/house_price_model.pkl"
12
+ if not os.path.exists(model_path):
13
+ raise FileNotFoundError("Model file not found. Make sure 'model/house_price_model.pkl' exists.")
14
+ model = joblib.load(model_path)
15
+
16
+ # Sample reference and current data (fake for demo, replace with real if available)
17
+ reference_data = pd.DataFrame({
18
+ "RM": np.random.normal(6, 0.5, 100),
19
+ "LSTAT": np.random.normal(12, 5, 100),
20
+ "PTRATIO": np.random.normal(18, 1.5, 100),
21
+ })
22
+ current_data = pd.DataFrame({
23
+ "RM": np.random.normal(6.2, 0.6, 100),
24
+ "LSTAT": np.random.normal(14, 5, 100),
25
+ "PTRATIO": np.random.normal(19, 2, 100),
26
+ })
27
+
28
+ # Generate drift report and save it
29
+ def generate_drift_report():
30
+ report = Report(metrics=[DataDriftPreset()])
31
+ report.run(reference_data=reference_data, current_data=current_data)
32
+ report.save_html("evidently_report.html")
33
+ return "evidently_report.html"
34
 
35
+ # Predict function
36
+ def predict(rm, lstat, ptratio):
37
  input_data = np.array([[rm, lstat, ptratio]])
38
  prediction = model.predict(input_data)[0]
39
  return round(prediction, 2)
40
 
41
+ # Wrapper to return the HTML drift report
42
+ def show_report():
43
+ if not os.path.exists("evidently_report.html"):
44
+ generate_drift_report()
45
+ with open("evidently_report.html", "r", encoding="utf-8") as f:
46
+ html_content = f.read()
47
+ return gr.HTML(html_content)
48
+
49
+ # Gradio UI setup
50
+ with gr.Blocks() as demo:
51
+ gr.Markdown("# 🏡 Boston Housing Price Predictor + Drift Report")
52
+
53
+ with gr.Tab("🔮 Predict Price"):
54
+ rm = gr.Slider(3.0, 9.0, step=0.1, label="Average Rooms (RM)")
55
+ lstat = gr.Slider(1.0, 40.0, step=0.1, label="Lower Status Population (%) (LSTAT)")
56
+ ptratio = gr.Slider(12.0, 22.0, step=0.1, label="Pupil-Teacher Ratio (PTRATIO)")
57
+ predict_btn = gr.Button("Predict")
58
+ result = gr.Number(label="🏠 Predicted Price ($1000s)")
59
+ predict_btn.click(predict, inputs=[rm, lstat, ptratio], outputs=result)
60
+
61
+ with gr.Tab("📊 View Data Drift Report"):
62
+ report_output = gr.HTML()
63
+ generate_btn = gr.Button("Generate Report")
64
+ generate_btn.click(fn=show_report, outputs=report_output)
65
+
66
+ if __name__ == "__main__":
67
+ demo.launch()