Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
|
|
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.responses import JSONResponse
|
| 5 |
import uvicorn
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
model = joblib.load("delay_model_v2.pkl")
|
| 9 |
-
|
| 10 |
-
# Hardcoded label maps
|
| 11 |
phase_map = {"Framing": 0, "Foundation": 1, "Finishing": 2}
|
| 12 |
weather_map = {"Cloudy": 0, "Rainy": 1, "Sunny": 2}
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def predict_delay(phase, weather, absentee_pct, delay_log):
|
| 16 |
phase_encoded = phase_map.get(phase, 0)
|
| 17 |
weather_encoded = weather_map.get(weather, 0)
|
| 18 |
input_data = [[phase_encoded, weather_encoded, absentee_pct, delay_log]]
|
| 19 |
-
prediction = model.predict(input_data)
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
api_app = FastAPI()
|
| 24 |
|
| 25 |
@api_app.post("/predict")
|
|
@@ -31,37 +62,42 @@ async def predict_from_salesforce(request: Request):
|
|
| 31 |
absentee_pct = float(data.get("absentee_pct", 0))
|
| 32 |
delay_log = float(data.get("delay_log", 0))
|
| 33 |
|
| 34 |
-
prediction = predict_delay(phase, weather, absentee_pct, delay_log)
|
| 35 |
|
| 36 |
return JSONResponse(content={
|
| 37 |
"delay_probability": prediction,
|
| 38 |
-
"
|
| 39 |
-
"
|
|
|
|
| 40 |
})
|
| 41 |
except Exception as e:
|
| 42 |
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
|
| 43 |
|
| 44 |
-
#
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("## Delay Predictor")
|
| 47 |
with gr.Row():
|
| 48 |
phase_input = gr.Textbox(label="Phase (Framing/Foundation/Finishing)")
|
| 49 |
weather_input = gr.Textbox(label="Weather (Sunny/Rainy/Cloudy)")
|
| 50 |
with gr.Row():
|
| 51 |
absentee_input = gr.Number(label="Absentee %")
|
| 52 |
delay_input = gr.Number(label="Previous Delay Log")
|
| 53 |
-
output = gr.Textbox(label="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
submit = gr.Button("Predict")
|
| 56 |
submit.click(
|
| 57 |
-
|
| 58 |
inputs=[phase_input, weather_input, absentee_input, delay_input],
|
| 59 |
outputs=output
|
| 60 |
)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
app = gr.mount_gradio_app(api_app, demo, path="/")
|
| 64 |
|
| 65 |
-
# Run
|
| 66 |
if __name__ == "__main__":
|
| 67 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 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 |
+
prediction = model.predict(input_data)[0]
|
| 38 |
+
prediction = round(prediction, 2)
|
| 39 |
+
|
| 40 |
+
# Risk tagging
|
| 41 |
+
if prediction >= 75:
|
| 42 |
+
risk = "High Risk"
|
| 43 |
+
elif prediction >= 50:
|
| 44 |
+
risk = "Moderate Risk"
|
| 45 |
+
else:
|
| 46 |
+
risk = "Low Risk"
|
| 47 |
+
|
| 48 |
+
# AI reasoning
|
| 49 |
+
insight = f"Phase: {phase}, Weather: {weather}, Absenteeism: {absentee_pct}%, Previous Delay: {delay_log} β Risk: {risk}"
|
| 50 |
|
| 51 |
+
return prediction, risk, insight
|
| 52 |
+
|
| 53 |
+
# FastAPI for Salesforce
|
| 54 |
api_app = FastAPI()
|
| 55 |
|
| 56 |
@api_app.post("/predict")
|
|
|
|
| 62 |
absentee_pct = float(data.get("absentee_pct", 0))
|
| 63 |
delay_log = float(data.get("delay_log", 0))
|
| 64 |
|
| 65 |
+
prediction, risk, insight = predict_delay(phase, weather, absentee_pct, delay_log)
|
| 66 |
|
| 67 |
return JSONResponse(content={
|
| 68 |
"delay_probability": prediction,
|
| 69 |
+
"risk_alert": risk,
|
| 70 |
+
"ai_insight": insight,
|
| 71 |
+
"status": "success"
|
| 72 |
})
|
| 73 |
except Exception as e:
|
| 74 |
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
|
| 75 |
|
| 76 |
+
# Gradio UI for manual testing
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("## ποΈ Delay Predictor")
|
| 79 |
with gr.Row():
|
| 80 |
phase_input = gr.Textbox(label="Phase (Framing/Foundation/Finishing)")
|
| 81 |
weather_input = gr.Textbox(label="Weather (Sunny/Rainy/Cloudy)")
|
| 82 |
with gr.Row():
|
| 83 |
absentee_input = gr.Number(label="Absentee %")
|
| 84 |
delay_input = gr.Number(label="Previous Delay Log")
|
| 85 |
+
output = gr.Textbox(label="Prediction Summary")
|
| 86 |
+
|
| 87 |
+
def predict_and_format(phase, weather, absentee, delay_log):
|
| 88 |
+
prediction, risk, insight = predict_delay(phase, weather, absentee, delay_log)
|
| 89 |
+
return f"Predicted Delay: {prediction}%\nRisk Level: {risk}\nInsight: {insight}"
|
| 90 |
|
| 91 |
submit = gr.Button("Predict")
|
| 92 |
submit.click(
|
| 93 |
+
predict_and_format,
|
| 94 |
inputs=[phase_input, weather_input, absentee_input, delay_input],
|
| 95 |
outputs=output
|
| 96 |
)
|
| 97 |
|
| 98 |
+
# Mount Gradio inside FastAPI
|
| 99 |
app = gr.mount_gradio_app(api_app, demo, path="/")
|
| 100 |
|
| 101 |
+
# Run locally (Hugging Face will ignore this)
|
| 102 |
if __name__ == "__main__":
|
| 103 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|