Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,6 +32,16 @@ y = df["Delay%"]
|
|
| 32 |
model = LinearRegression()
|
| 33 |
model.fit(X, y)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Function to generate LaTeX content for PDF
|
| 36 |
def generate_latex_content(phase, weather, absentee_pct, delay_log, prediction, risk, insight):
|
| 37 |
return f"""
|
|
@@ -65,20 +75,19 @@ def generate_latex_content(phase, weather, absentee_pct, delay_log, prediction,
|
|
| 65 |
|
| 66 |
# Function to generate PDF and return its URL
|
| 67 |
def generate_pdf(phase, weather, absentee_pct, delay_log, prediction, risk, insight):
|
| 68 |
-
|
|
|
|
| 69 |
pdf_filename = f"delay_report_{uuid.uuid4().hex}.pdf"
|
| 70 |
-
tex_filename = f"/
|
| 71 |
-
pdf_path = f"/
|
| 72 |
|
| 73 |
-
# Write LaTeX content to file
|
| 74 |
latex_content = generate_latex_content(phase, weather, absentee_pct, delay_log, prediction, risk, insight)
|
| 75 |
with open(tex_filename, "w") as f:
|
| 76 |
f.write(latex_content)
|
| 77 |
|
| 78 |
-
# Compile LaTeX to PDF
|
| 79 |
try:
|
| 80 |
subprocess.run(
|
| 81 |
-
["pdflatex", "-output-directory=/
|
| 82 |
check=True,
|
| 83 |
stdout=subprocess.PIPE,
|
| 84 |
stderr=subprocess.PIPE,
|
|
@@ -87,31 +96,25 @@ def generate_pdf(phase, weather, absentee_pct, delay_log, prediction, risk, insi
|
|
| 87 |
except subprocess.CalledProcessError as e:
|
| 88 |
raise Exception(f"PDF generation failed: {e.stderr}")
|
| 89 |
|
| 90 |
-
# Clean up auxiliary files
|
| 91 |
for ext in [".aux", ".log", ".tex"]:
|
| 92 |
try:
|
| 93 |
os.remove(tex_filename.replace(".tex", ext))
|
| 94 |
except OSError:
|
| 95 |
pass
|
| 96 |
|
| 97 |
-
|
| 98 |
-
# Assuming a static file serving endpoint or external storage
|
| 99 |
-
pdf_url = f"https://your-huggingface-space.hf.space/static/{pdf_filename}"
|
| 100 |
return pdf_url, pdf_path
|
| 101 |
|
| 102 |
# Main prediction function
|
| 103 |
def predict_delay(phase, weather, absentee_pct, delay_log):
|
| 104 |
-
# Encode phase as dummy variables
|
| 105 |
framing = 1 if phase == "Framing" else 0
|
| 106 |
foundation = 1 if phase == "Foundation" else 0
|
| 107 |
weather_encoded = weather_map.get(weather, 0)
|
| 108 |
input_data = [[framing, foundation, weather_encoded, absentee_pct, delay_log]]
|
| 109 |
|
| 110 |
-
# Model makes prediction
|
| 111 |
prediction = model.predict(input_data)[0]
|
| 112 |
prediction = round(prediction, 2)
|
| 113 |
|
| 114 |
-
# Risk tagging based on predicted delay percentage
|
| 115 |
if prediction >= 75:
|
| 116 |
risk = "High Risk"
|
| 117 |
elif prediction >= 50:
|
|
@@ -119,19 +122,14 @@ def predict_delay(phase, weather, absentee_pct, delay_log):
|
|
| 119 |
else:
|
| 120 |
risk = "Low Risk"
|
| 121 |
|
| 122 |
-
# AI reasoning for insights
|
| 123 |
insight = f"Phase: {phase}, Weather: {weather}, Absenteeism: {absentee_pct}%, Previous Delay: {delay_log} → Risk: {risk}"
|
| 124 |
|
| 125 |
-
# Generate PDF
|
| 126 |
pdf_url, pdf_path = generate_pdf(phase, weather, absentee_pct, delay_log, prediction, risk, insight)
|
| 127 |
-
|
| 128 |
return prediction, risk, insight, pdf_url
|
| 129 |
|
| 130 |
# FastAPI for Salesforce
|
| 131 |
api_app = FastAPI()
|
| 132 |
-
|
| 133 |
-
# Serve static files from /tmp (for PDFs)
|
| 134 |
-
api_app.mount("/static", StaticFiles(directory="/tmp"), name="static")
|
| 135 |
|
| 136 |
@api_app.post("/predict")
|
| 137 |
async def predict_from_salesforce(request: Request):
|
|
@@ -167,11 +165,14 @@ with gr.Blocks() as demo:
|
|
| 167 |
pdf_output = gr.File(label="Download PDF Report")
|
| 168 |
|
| 169 |
def predict_and_format(phase, weather, absentee, delay_log):
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
submit = gr.Button("Predict")
|
| 177 |
submit.click(
|
|
@@ -180,9 +181,7 @@ with gr.Blocks() as demo:
|
|
| 180 |
outputs=[output, pdf_output]
|
| 181 |
)
|
| 182 |
|
| 183 |
-
# Mount Gradio inside FastAPI
|
| 184 |
app = gr.mount_gradio_app(api_app, demo, path="/")
|
| 185 |
|
| 186 |
-
# Run locally (Hugging Face will ignore this)
|
| 187 |
if __name__ == "__main__":
|
| 188 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 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"""
|
|
|
|
| 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,
|
|
|
|
| 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:
|
|
|
|
| 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 |
+
api_app.mount("/static", StaticFiles(directory="./static_files"), name="static")
|
|
|
|
|
|
|
| 133 |
|
| 134 |
@api_app.post("/predict")
|
| 135 |
async def predict_from_salesforce(request: Request):
|
|
|
|
| 165 |
pdf_output = gr.File(label="Download PDF Report")
|
| 166 |
|
| 167 |
def predict_and_format(phase, weather, absentee, delay_log):
|
| 168 |
+
try:
|
| 169 |
+
prediction, risk, insight, pdf_url = predict_delay(phase, weather, absentee, delay_log)
|
| 170 |
+
return (
|
| 171 |
+
f"Predicted Delay: {prediction}%\nRisk Level: {risk}\nInsight: {insight}\nPDF URL: {pdf_url}",
|
| 172 |
+
pdf_url
|
| 173 |
+
)
|
| 174 |
+
except Exception as e:
|
| 175 |
+
return f"Error: {str(e)}", None
|
| 176 |
|
| 177 |
submit = gr.Button("Predict")
|
| 178 |
submit.click(
|
|
|
|
| 181 |
outputs=[output, pdf_output]
|
| 182 |
)
|
| 183 |
|
|
|
|
| 184 |
app = gr.mount_gradio_app(api_app, demo, path="/")
|
| 185 |
|
|
|
|
| 186 |
if __name__ == "__main__":
|
| 187 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|