AjaykumarPilla commited on
Commit
29f24d0
·
verified ·
1 Parent(s): 167488f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +105 -0
  2. new_delay_data.csv +52 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import pandas as pd
4
+ from fastapi import FastAPI, Request
5
+ from fastapi.responses import JSONResponse
6
+ import uvicorn
7
+ from sklearn.linear_model import LinearRegression
8
+ from sklearn.model_selection import train_test_split
9
+
10
+ # Hardcoded mappings
11
+ phase_map = { "Framing": 0, "Foundation": 1, "Finishing": 2}
12
+ weather_map = {"Cloudy": 0, "Rainy": 1, "Sunny": 2}
13
+
14
+ # Load and preprocess training data
15
+ df = pd.read_csv("delay_data.csv")
16
+
17
+ # Encode categorical features
18
+ df["Phase"] = df["Phase"].map(phase_map)
19
+ df["Weather"] = df["Weather"].map(weather_map)
20
+
21
+ # Handle missing or invalid mappings
22
+ df.dropna(subset=["Phase", "Weather", "Absentee", "DelayLog", "Delay%"], inplace=True)
23
+
24
+ # Split features and target
25
+ X = df[["Phase", "Weather", "Absentee", "DelayLog"]]
26
+ y = df["Delay%"]
27
+
28
+ # Train model
29
+ model = LinearRegression()
30
+ model.fit(X, y)
31
+
32
+ # Main prediction function
33
+ def predict_delay(phase, weather, absentee_pct, delay_log):
34
+ phase_encoded = phase_map.get(phase, 0)
35
+ weather_encoded = weather_map.get(weather, 0)
36
+ input_data = [[phase_encoded, weather_encoded, absentee_pct, delay_log]]
37
+
38
+ # Model makes prediction
39
+ prediction = model.predict(input_data)[0]
40
+ prediction = round(prediction, 2)
41
+
42
+ # Risk tagging based on predicted delay percentage
43
+ if prediction >= 75:
44
+ risk = "High Risk"
45
+ elif prediction >= 50:
46
+ risk = "Moderate Risk"
47
+ else:
48
+ risk = "Low Risk"
49
+
50
+ # AI reasoning for insights
51
+ insight = f"Phase: {phase}, Weather: {weather}, Absenteeism: {absentee_pct}%, Previous Delay: {delay_log} → Risk: {risk}"
52
+
53
+ return prediction, risk, insight
54
+
55
+ # FastAPI for Salesforce
56
+ api_app = FastAPI()
57
+
58
+ @api_app.post("/predict")
59
+ async def predict_from_salesforce(request: Request):
60
+ try:
61
+ data = await request.json()
62
+ phase = data.get("phase", "Framing")
63
+ weather = data.get("weather", "Sunny")
64
+ absentee_pct = float(data.get("absentee_pct", 0))
65
+ delay_log = float(data.get("delay_log", 0))
66
+
67
+ prediction, risk, insight = predict_delay(phase, weather, absentee_pct, delay_log)
68
+
69
+ return JSONResponse(content={
70
+ "delay_probability": prediction,
71
+ "risk_alert": risk,
72
+ "ai_insight": insight,
73
+ "status": "success"
74
+ })
75
+ except Exception as e:
76
+ return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
77
+
78
+ # Gradio UI for manual testing
79
+ with gr.Blocks() as demo:
80
+ gr.Markdown("## 🏗️ Delay Predictor")
81
+ with gr.Row():
82
+ phase_input = gr.Textbox(label="Phase (Framing/Foundation/Finishing)")
83
+ weather_input = gr.Textbox(label="Weather (Sunny/Rainy/Cloudy)")
84
+ with gr.Row():
85
+ absentee_input = gr.Number(label="Absentee %")
86
+ delay_input = gr.Number(label="Previous Delay Log")
87
+ output = gr.Textbox(label="Prediction Summary")
88
+
89
+ def predict_and_format(phase, weather, absentee, delay_log):
90
+ prediction, risk, insight = predict_delay(phase, weather, absentee, delay_log)
91
+ return f"Predicted Delay: {prediction}%\nRisk Level: {risk}\nInsight: {insight}"
92
+
93
+ submit = gr.Button("Predict")
94
+ submit.click(
95
+ predict_and_format,
96
+ inputs=[phase_input, weather_input, absentee_input, delay_input],
97
+ outputs=output
98
+ )
99
+
100
+ # Mount Gradio inside FastAPI
101
+ app = gr.mount_gradio_app(api_app, demo, path="/")
102
+
103
+ # Run locally (Hugging Face will ignore this)
104
+ if __name__ == "__main__":
105
+ uvicorn.run(app, host="0.0.0.0", port=7860)
new_delay_data.csv ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Phase,Weather,Absentee,DelayLog,Delay%,PredictedDelay
2
+ Framing,Sunny,10.00,2.00,48.50,43.65
3
+ Foundation,Sunny,10.00,2.00,38.20,34.38
4
+ Finishing,Sunny,10.00,2.00,28.10,25.29
5
+ Framing,Cloudy,8.50,3.50,46.30,41.67
6
+ Foundation,Cloudy,7.20,2.80,36.50,32.85
7
+ Finishing,Cloudy,6.90,2.50,26.80,24.12
8
+ Framing,Rainy,12.50,4.20,55.60,50.04
9
+ Foundation,Rainy,11.80,3.90,45.30,40.77
10
+ Finishing,Rainy,10.20,3.60,33.40,30.06
11
+ Framing,Sunny,9.80,1.90,47.20,42.48
12
+ Foundation,Sunny,9.50,1.80,37.10,33.39
13
+ Finishing,Sunny,9.30,1.70,27.50,24.75
14
+ Framing,Cloudy,11.00,4.00,50.80,45.72
15
+ Foundation,Cloudy,10.50,3.50,40.60,36.54
16
+ Finishing,Cloudy,9.90,3.20,30.20,27.18
17
+ Framing,Rainy,13.20,5.00,58.90,53.01
18
+ Foundation,Rainy,12.70,4.50,47.80,43.02
19
+ Finishing,Rainy,11.50,4.00,34.70,31.23
20
+ Framing,Sunny,10.20,2.10,49.10,44.19
21
+ Foundation,Sunny,10.10,2.00,38.90,35.01
22
+ Finishing,Sunny,9.80,1.90,28.30,25.47
23
+ Framing,Cloudy,7.50,2.70,45.20,40.68
24
+ Foundation,Cloudy,6.80,2.40,35.40,31.86
25
+ Finishing,Cloudy,6.50,2.20,25.90,23.31
26
+ Framing,Sunny,10.50,2.30,50.30,45.27
27
+ Foundation,Sunny,10.30,2.20,39.50,35.55
28
+ Finishing,Sunny,10.00,2.10,29.00,26.10
29
+ Framing,Rainy,14.00,5.50,60.20,54.18
30
+ Foundation,Rainy,13.50,5.00,48.90,44.01
31
+ Finishing,Rainy,12.80,4.50,35.60,32.04
32
+ Framing,Cloudy,9.00,3.00,47.90,43.11
33
+ Foundation,Cloudy,8.50,2.80,37.70,33.93
34
+ Finishing,Cloudy,8.20,2.60,27.80,25.02
35
+ Framing,Sunny,10.70,2.50,51.40,46.26
36
+ Foundation,Sunny,10.50,2.40,40.20,36.18
37
+ Finishing,Sunny,10.20,2.30,29.50,26.55
38
+ Framing,Cloudy,11.50,4.20,52.60,47.34
39
+ Foundation,Cloudy,11.00,3.80,41.80,37.62
40
+ Finishing,Cloudy,10.50,3.50,30.90,27.81
41
+ Framing,Rainy,12.00,4.80,56.70,51.03
42
+ Foundation,Rainy,11.50,4.30,46.20,41.58
43
+ Finishing,Rainy,10.80,4.00,34.10,30.69
44
+ Framing,Sunny,9.90,2.00,48.80,43.92
45
+ Foundation,Sunny,9.70,1.90,38.40,34.56
46
+ Finishing,Sunny,9.50,1.80,28.20,25.38
47
+ Framing,Cloudy,8.00,3.20,46.50,41.85
48
+ Foundation,Cloudy,7.50,2.90,36.80,33.12
49
+ Finishing,Cloudy,7.20,2.70,27.00,24.30
50
+ Framing,Sunny,10.40,2.40,50.00,45.00
51
+ Foundation,Sunny,10.20,2.30,39.70,35.73
52
+ Finishing,Sunny,10.00,2.20,29.20,26.28