Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,160 +1,192 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
+
import uvicorn
|
| 6 |
+
from sklearn.linear_model import LinearRegression
|
| 7 |
+
from sklearn.model_selection import train_test_split
|
| 8 |
+
import os
|
| 9 |
+
import subprocess
|
| 10 |
+
import uuid
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
from fastapi.staticfiles import StaticFiles
|
| 13 |
+
|
| 14 |
+
# Hardcoded mappings
|
| 15 |
+
weather_map = {"Cloudy": 0, "Rainy": 1, "Sunny": 2}
|
| 16 |
+
|
| 17 |
+
# Load and preprocess training data
|
| 18 |
+
df = pd.read_csv("new_delay_data.csv")
|
| 19 |
+
|
| 20 |
+
# Encode categorical features
|
| 21 |
+
df = pd.get_dummies(df, columns=["Phase"], drop_first=True) # Finishing as baseline
|
| 22 |
+
df["Weather"] = df["Weather"].map(weather_map)
|
| 23 |
+
|
| 24 |
+
# Handle missing or invalid mappings
|
| 25 |
+
df.dropna(subset=["Phase_Framing", "Phase_Foundation", "Weather", "Absentee", "DelayLog", "Delay%"], inplace=True)
|
| 26 |
+
|
| 27 |
+
# Split features and target
|
| 28 |
+
X = df[["Phase_Framing", "Phase_Foundation", "Weather", "Absentee", "DelayLog"]]
|
| 29 |
+
y = df["Delay%"]
|
| 30 |
+
|
| 31 |
+
# Train model
|
| 32 |
+
model = LinearRegression()
|
| 33 |
+
model.fit(X, y)
|
| 34 |
+
|
| 35 |
+
# Function to clean up old files
|
| 36 |
+
def clean_tmp():
|
| 37 |
+
directory = "./static_files"
|
| 38 |
+
for file in os.listdir(directory):
|
| 39 |
+
if file.endswith((".pdf", ".tex", ".aux", ".log")):
|
| 40 |
+
try:
|
| 41 |
+
os.remove(os.path.join(directory, file))
|
| 42 |
+
except OSError:
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
# Function to generate LaTeX content for PDF
|
| 46 |
+
def generate_latex_content(phase, weather, absentee_pct, delay_log, prediction, risk, insight):
|
| 47 |
+
return f"""
|
| 48 |
+
\\documentclass[a4paper,12pt]{{article}}
|
| 49 |
+
\\usepackage{{geometry}}
|
| 50 |
+
\\usepackage{{utf8x}}
|
| 51 |
+
\\usepackage{{parskip}}
|
| 52 |
+
\\usepackage{{titlesec}}
|
| 53 |
+
\\usepackage{{xcolor}}
|
| 54 |
+
\\geometry{{margin=1in}}
|
| 55 |
+
\\titleformat{{\\section}}{{\\Large\\bfseries\\color{{blue}}}}{{\\thesection}}{{1em}}{{}}
|
| 56 |
+
\\begin{{document}}
|
| 57 |
+
\\section*{{Delay Prediction Report}}
|
| 58 |
+
\\textbf{{Generated on:}} {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\\\\
|
| 59 |
+
\\textbf{{Project Details}}\\\\
|
| 60 |
+
\\begin{{itemize}}
|
| 61 |
+
\\item \\textbf{{Phase:}} {phase}
|
| 62 |
+
\\item \\textbf{{Weather:}} {weather}
|
| 63 |
+
\\item \\textbf{{Absentee \\%:}} {absentee_pct:.2f}\\%
|
| 64 |
+
\\item \\textbf{{Previous Delay Log:}} {delay_log:.2f} days
|
| 65 |
+
\\end{{itemize}}
|
| 66 |
+
\\vspace{{0.5cm}}
|
| 67 |
+
\\textbf{{Prediction Results}}\\\\
|
| 68 |
+
\\begin{{itemize}}
|
| 69 |
+
\\item \\textbf{{Predicted Delay:}} {prediction:.2f}\\%
|
| 70 |
+
\\item \\textbf{{Risk Level:}} {risk}
|
| 71 |
+
\\item \\textbf{{AI Insight:}} {insight}
|
| 72 |
+
\\end{{itemize}}
|
| 73 |
+
\\end{{document}}
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
# Function to generate PDF and return its URL
|
| 77 |
+
def generate_pdf(phase, weather, absentee_pct, delay_log, prediction, risk, insight):
|
| 78 |
+
os.makedirs("./static_files", exist_ok=True)
|
| 79 |
+
clean_tmp() # Clean up old files
|
| 80 |
+
pdf_filename = f"delay_report_{uuid.uuid4().hex}.pdf"
|
| 81 |
+
tex_filename = f"./static_files/{pdf_filename.replace('.pdf', '.tex')}"
|
| 82 |
+
pdf_path = f"./static_files/{pdf_filename}"
|
| 83 |
+
|
| 84 |
+
latex_content = generate_latex_content(phase, weather, absentee_pct, delay_log, prediction, risk, insight)
|
| 85 |
+
with open(tex_filename, "w") as f:
|
| 86 |
+
f.write(latex_content)
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
subprocess.run(
|
| 90 |
+
["pdflatex", f"-output-directory=./static_files", tex_filename],
|
| 91 |
+
check=True,
|
| 92 |
+
stdout=subprocess.PIPE,
|
| 93 |
+
stderr=subprocess.PIPE,
|
| 94 |
+
text=True
|
| 95 |
+
)
|
| 96 |
+
except subprocess.CalledProcessError as e:
|
| 97 |
+
raise Exception(f"PDF generation failed: {e.stderr}")
|
| 98 |
+
|
| 99 |
+
for ext in [".aux", ".log", ".tex"]:
|
| 100 |
+
try:
|
| 101 |
+
os.remove(tex_filename.replace(".tex", ext))
|
| 102 |
+
except OSError:
|
| 103 |
+
pass
|
| 104 |
+
|
| 105 |
+
pdf_url = f"https://ajaykumarpilla-delay-predictor.hf.space/static/{pdf_filename}"
|
| 106 |
+
return pdf_url, pdf_path
|
| 107 |
+
|
| 108 |
+
# Main prediction function
|
| 109 |
+
def predict_delay(phase, weather, absentee_pct, delay_log):
|
| 110 |
+
framing = 1 if phase == "Framing" else 0
|
| 111 |
+
foundation = 1 if phase == "Foundation" else 0
|
| 112 |
+
weather_encoded = weather_map.get(weather, 0)
|
| 113 |
+
input_data = [[framing, foundation, weather_encoded, absentee_pct, delay_log]]
|
| 114 |
+
|
| 115 |
+
prediction = model.predict(input_data)[0]
|
| 116 |
+
prediction = round(prediction, 2)
|
| 117 |
+
|
| 118 |
+
if prediction >= 75:
|
| 119 |
+
risk = "High Risk"
|
| 120 |
+
elif prediction >= 50:
|
| 121 |
+
risk = "Moderate Risk"
|
| 122 |
+
else:
|
| 123 |
+
risk = "Low Risk"
|
| 124 |
+
|
| 125 |
+
insight = f"Phase: {phase}, Weather: {weather}, Absenteeism: {absentee_pct}%, Previous Delay: {delay_log} → Risk: {risk}"
|
| 126 |
+
|
| 127 |
+
pdf_url, pdf_path = generate_pdf(phase, weather, absentee_pct, delay_log, prediction, risk, insight)
|
| 128 |
+
return prediction, risk, insight, pdf_url
|
| 129 |
+
|
| 130 |
+
# FastAPI for Salesforce
|
| 131 |
+
api_app = FastAPI()
|
| 132 |
+
|
| 133 |
+
# Create static_files directory at startup
|
| 134 |
+
os.makedirs("./static_files", exist_ok=True)
|
| 135 |
+
|
| 136 |
+
# Mount static files directory
|
| 137 |
+
api_app.mount("/static", StaticFiles(directory="./static_files"), name="static")
|
| 138 |
+
|
| 139 |
+
@api_app.post("/predict")
|
| 140 |
+
async def predict_from_salesforce(request: Request):
|
| 141 |
+
try:
|
| 142 |
+
data = await request.json()
|
| 143 |
+
phase = data.get("phase", "Framing")
|
| 144 |
+
weather = data.get("weather", "Sunny")
|
| 145 |
+
absentee_pct = float(data.get("absentee_pct", 0))
|
| 146 |
+
delay_log = float(data.get("delay_log", 0))
|
| 147 |
+
|
| 148 |
+
prediction, risk, insight, pdf_url = predict_delay(phase, weather, absentee_pct, delay_log)
|
| 149 |
+
|
| 150 |
+
return JSONResponse(content={
|
| 151 |
+
"delay_probability": prediction,
|
| 152 |
+
"risk_alert": risk,
|
| 153 |
+
"ai_insight": insight,
|
| 154 |
+
"pdf_url": pdf_url,
|
| 155 |
+
"status": "success"
|
| 156 |
+
})
|
| 157 |
+
except Exception as e:
|
| 158 |
+
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
|
| 159 |
+
|
| 160 |
+
# Gradio UI for manual testing
|
| 161 |
+
with gr.Blocks() as demo:
|
| 162 |
+
gr.Markdown("## �(:construction:) Delay Predictor")
|
| 163 |
+
with gr.Row():
|
| 164 |
+
phase_input = gr.Textbox(label="Phase (Framing/Foundation/Finishing)")
|
| 165 |
+
weather_input = gr.Textbox(label="Weather (Sunny/Rainy/Cloudy)")
|
| 166 |
+
with gr.Row():
|
| 167 |
+
absentee_input = gr.Number(label="Absentee %")
|
| 168 |
+
delay_input = gr.Number(label="Previous Delay Log")
|
| 169 |
+
output = gr.Textbox(label="Prediction Summary")
|
| 170 |
+
pdf_output = gr.File(label="Download PDF Report")
|
| 171 |
+
|
| 172 |
+
def predict_and_format(phase, weather, absentee, delay_log):
|
| 173 |
+
try:
|
| 174 |
+
prediction, risk, insight, pdf_url = predict_delay(phase, weather, absentee, delay_log)
|
| 175 |
+
return (
|
| 176 |
+
f"Predicted Delay: {prediction}%\nRisk Level: {risk}\nInsight: {insight}\nPDF URL: {pdf_url}",
|
| 177 |
+
pdf_url
|
| 178 |
+
)
|
| 179 |
+
except Exception as e:
|
| 180 |
+
return f"Error: {str(e)}", None
|
| 181 |
+
|
| 182 |
+
submit = gr.Button("Predict")
|
| 183 |
+
submit.click(
|
| 184 |
+
predict_and_format,
|
| 185 |
+
inputs=[phase_input, weather_input, absentee_input, delay_input],
|
| 186 |
+
outputs=[output, pdf_output]
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
app = gr.mount_gradio_app(api_app, demo, path="/")
|
| 190 |
+
|
| 191 |
+
if __name__ == "__main__":
|
| 192 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|