Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,105 +0,0 @@
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|