Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,116 @@
|
|
| 1 |
-
from
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
app =
|
|
|
|
| 8 |
|
| 9 |
-
# Hugging Face
|
| 10 |
-
|
| 11 |
-
HF_TOKEN = "your_hugging_face_token"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
},
|
| 23 |
-
{
|
| 24 |
-
"project_id": "P002",
|
| 25 |
-
"task_type": "Masonry",
|
| 26 |
-
"weather_temp": 22.0,
|
| 27 |
-
"absenteeism_rate": 0.05,
|
| 28 |
-
"labour_count": 15
|
| 29 |
-
}
|
| 30 |
-
]
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
|
| 52 |
-
return f"https://example.com/pdf/{forecast['project_id']}_{forecast['date']}.pdf"
|
| 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 |
if __name__ == "__main__":
|
| 83 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Depends, Security
|
| 2 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.linear_model import LinearRegression
|
| 6 |
+
import uvicorn
|
| 7 |
+
import os
|
| 8 |
+
from typing import List, Dict
|
| 9 |
+
import time
|
| 10 |
|
| 11 |
+
app = FastAPI(title="AI Estimator API")
|
| 12 |
+
security = HTTPBearer()
|
| 13 |
|
| 14 |
+
# Mock token for authentication (replace with Hugging Face Space secrets or env variable)
|
| 15 |
+
API_TOKEN = os.getenv("API_TOKEN", "hf_mock_token_123")
|
|
|
|
| 16 |
|
| 17 |
+
# Pydantic models for request/response
|
| 18 |
+
class TaskInput(BaseModel):
|
| 19 |
+
project_id: str
|
| 20 |
+
date: str
|
| 21 |
+
task_type: str
|
| 22 |
+
historical_consumption: Dict[str, float] # e.g., {"cement": 100, "steel": 50}
|
| 23 |
+
weather: Dict[str, float] # e.g., {"temperature": 25, "rain_probability": 0.1}
|
| 24 |
+
absenteeism_rate: float
|
| 25 |
+
material_lead_time: Dict[str, float] # e.g., {"cement": 2, "steel": 3}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
class ForecastOutput(BaseModel):
|
| 28 |
+
project_id: str
|
| 29 |
+
date: str
|
| 30 |
+
task_type: str
|
| 31 |
+
material_needed: Dict[str, float]
|
| 32 |
+
labour_needed: Dict[str, float]
|
| 33 |
+
forecast_confidence: float
|
| 34 |
+
alert_flag: bool
|
| 35 |
+
|
| 36 |
+
# Mock trained model (replace with actual trained model in production)
|
| 37 |
+
class MockAIModel:
|
| 38 |
+
def __init__(self):
|
| 39 |
+
# Dummy coefficients for regression
|
| 40 |
+
self.material_model = LinearRegression()
|
| 41 |
+
self.labour_model = LinearRegression()
|
| 42 |
+
# Mock training (replace with actual training data)
|
| 43 |
+
X = np.array([[1, 25, 0.1, 0.05, 2], [2, 30, 0.2, 0.1, 1]]) # Features: task_type_id, temp, rain, absenteeism, lead_time
|
| 44 |
+
y_material = np.array([[100, 50], [120, 60]]) # cement, steel
|
| 45 |
+
y_labour = np.array([[10, 5], [12, 6]]) # trade1, trade2
|
| 46 |
+
self.material_model.fit(X, y_material)
|
| 47 |
+
self.labour_model.fit(X, y_labour)
|
| 48 |
+
|
| 49 |
+
def predict(self, features: np.ndarray):
|
| 50 |
+
material_pred = self.material_model.predict(features)[0]
|
| 51 |
+
labour_pred = self.labour_model.predict(features)[0]
|
| 52 |
+
return material_pred, labour_pred
|
| 53 |
|
| 54 |
+
# Initialize model
|
| 55 |
+
ai_model = MockAIModel()
|
|
|
|
| 56 |
|
| 57 |
+
# Token authentication
|
| 58 |
+
async def verify_token(credentials: HTTPAuthorizationCredentials = Security(security)):
|
| 59 |
+
if credentials.credentials != API_TOKEN:
|
| 60 |
+
raise HTTPException(status_code=401, detail="Invalid or missing API token")
|
| 61 |
+
return credentials.credentials
|
| 62 |
+
|
| 63 |
+
@app.post("/forecast", response_model=ForecastOutput, dependencies=[Depends(verify_token)])
|
| 64 |
+
async def generate_forecast(task: TaskInput):
|
| 65 |
+
start_time = time.time()
|
| 66 |
|
| 67 |
+
# Extract features for model
|
| 68 |
+
task_type_id = hash(task.task_type) % 100 # Mock task type encoding
|
| 69 |
+
features = np.array([[
|
| 70 |
+
task_type_id,
|
| 71 |
+
task.weather.get("temperature", 20),
|
| 72 |
+
task.weather.get("rain_probability", 0),
|
| 73 |
+
task.absenteeism_rate,
|
| 74 |
+
list(task.material_lead_time.values())[0] # Use first material lead time
|
| 75 |
+
]])
|
| 76 |
|
| 77 |
+
# Generate predictions
|
| 78 |
+
material_pred, labour_pred = ai_model.predict(features)
|
| 79 |
+
|
| 80 |
+
# Format predictions
|
| 81 |
+
material_needed = {
|
| 82 |
+
"cement": max(0, material_pred[0]),
|
| 83 |
+
"steel": max(0, material_pred[1]),
|
| 84 |
+
"brick": max(0, material_pred[0] * 2) # Mock derived material
|
| 85 |
+
}
|
| 86 |
+
labour_needed = {
|
| 87 |
+
"carpenter": max(0, labour_pred[0]),
|
| 88 |
+
"mason": max(0, labour_pred[1]),
|
| 89 |
+
"laborer": max(0, labour_pred[0] + labour_pred[1]) # Mock derived trade
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
# Calculate confidence (mock: based on input completeness)
|
| 93 |
+
confidence = 0.85 if all([task.historical_consumption, task.weather, task.material_lead_time]) else 0.75
|
| 94 |
+
|
| 95 |
+
# Alert if resource gap exceeds 15% (mock comparison with historical)
|
| 96 |
+
historical_total = sum(task.historical_consumption.values())
|
| 97 |
+
predicted_total = sum(material_needed.values())
|
| 98 |
+
alert_flag = abs(predicted_total - historical_total) / historical_total > 0.15 if historical_total > 0 else False
|
| 99 |
|
| 100 |
+
# Ensure latency < 10s
|
| 101 |
+
elapsed_time = time.time() - start_time
|
| 102 |
+
if elapsed_time > 10:
|
| 103 |
+
raise HTTPException(status_code=500, detail="Forecast generation exceeded 10s")
|
| 104 |
|
| 105 |
+
return ForecastOutput(
|
| 106 |
+
project_id=task.project_id,
|
| 107 |
+
date=task.date,
|
| 108 |
+
task_type=task.task_type,
|
| 109 |
+
material_needed=material_needed,
|
| 110 |
+
labour_needed=labour_needed,
|
| 111 |
+
forecast_confidence=confidence,
|
| 112 |
+
alert_flag=alert_flag
|
| 113 |
+
)
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|