Spaces:
Sleeping
Sleeping
Update modelLoanAPI.py
Browse files- modelLoanAPI.py +11 -14
modelLoanAPI.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
-
from fastapi.responses import JSONResponse
|
| 4 |
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
|
|
@@ -17,7 +15,7 @@ warnings.filterwarnings("ignore")
|
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
@app.post("/predict_worker_earnings/")
|
| 20 |
-
async def predict_worker_earnings(worker_id: int):
|
| 21 |
try:
|
| 22 |
# Initialize result dictionary
|
| 23 |
results = {
|
|
@@ -28,11 +26,10 @@ async def predict_worker_earnings(worker_id: int):
|
|
| 28 |
}
|
| 29 |
|
| 30 |
# Load data
|
| 31 |
-
df = pd.read_csv('extended_worker_dataset.csv')
|
| 32 |
|
| 33 |
# Filter for one worker_id
|
| 34 |
df = df[df['worker_id'] == worker_id].copy()
|
| 35 |
-
|
| 36 |
if df.empty:
|
| 37 |
raise HTTPException(status_code=404, detail=f"No data found for worker_id {worker_id}")
|
| 38 |
|
|
@@ -47,7 +44,6 @@ async def predict_worker_earnings(worker_id: int):
|
|
| 47 |
le = LabelEncoder()
|
| 48 |
df['job_type_encoded'] = le.fit_transform(df['job_type'])
|
| 49 |
|
| 50 |
-
# Split data
|
| 51 |
split_point = int(len(df) * 0.8)
|
| 52 |
train_df = df.iloc[:split_point].copy()
|
| 53 |
test_df = df.iloc[split_point:].copy()
|
|
@@ -62,14 +58,15 @@ async def predict_worker_earnings(worker_id: int):
|
|
| 62 |
for subset in [train_df, test_df]:
|
| 63 |
subset['dayofweek'] = subset['timestamp'].dt.dayofweek
|
| 64 |
subset['month'] = subset['timestamp'].dt.month
|
|
|
|
| 65 |
subset['year'] = subset['timestamp'].dt.year
|
| 66 |
subset['dayofyear'] = subset['timestamp'].dt.dayofyear
|
| 67 |
subset['is_weekend'] = subset['dayofweek'].isin([5, 6]).astype(int)
|
| 68 |
|
| 69 |
# Train classifier
|
| 70 |
X_train_class = train_df[['dayofweek', 'month', 'year', 'dayofyear',
|
| 71 |
-
|
| 72 |
-
|
| 73 |
y_train_class = train_df['has_job']
|
| 74 |
|
| 75 |
classifier = RandomForestClassifier(
|
|
@@ -80,8 +77,8 @@ async def predict_worker_earnings(worker_id: int):
|
|
| 80 |
# Train regressor
|
| 81 |
train_df_reg = train_df[train_df['has_job'] == 1].copy()
|
| 82 |
X_train_reg = train_df_reg[['dayofweek', 'month', 'year', 'dayofyear',
|
| 83 |
-
|
| 84 |
-
|
| 85 |
y_train_reg = train_df_reg['contracted_wage']
|
| 86 |
|
| 87 |
regressor = RandomForestRegressor(
|
|
@@ -91,7 +88,7 @@ async def predict_worker_earnings(worker_id: int):
|
|
| 91 |
|
| 92 |
# Prepare future dataframe
|
| 93 |
future_df = test_df[['timestamp', 'job_type', 'job_type_encoded',
|
| 94 |
-
|
| 95 |
|
| 96 |
future_df['dayofweek'] = future_df['ds'].dt.dayofweek
|
| 97 |
future_df['month'] = future_df['ds'].dt.month
|
|
@@ -210,10 +207,10 @@ async def predict_worker_earnings(worker_id: int):
|
|
| 210 |
return obj.tolist()
|
| 211 |
return obj
|
| 212 |
|
| 213 |
-
return
|
| 214 |
|
| 215 |
except Exception as e:
|
| 216 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 217 |
|
| 218 |
if __name__ == "__main__":
|
| 219 |
import uvicorn
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Query
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
|
|
|
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
@app.post("/predict_worker_earnings/")
|
| 18 |
+
async def predict_worker_earnings(worker_id: int = Query(...)):
|
| 19 |
try:
|
| 20 |
# Initialize result dictionary
|
| 21 |
results = {
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
# Load data
|
| 29 |
+
df = pd.read_csv('/app/data/extended_worker_dataset.csv')
|
| 30 |
|
| 31 |
# Filter for one worker_id
|
| 32 |
df = df[df['worker_id'] == worker_id].copy()
|
|
|
|
| 33 |
if df.empty:
|
| 34 |
raise HTTPException(status_code=404, detail=f"No data found for worker_id {worker_id}")
|
| 35 |
|
|
|
|
| 44 |
le = LabelEncoder()
|
| 45 |
df['job_type_encoded'] = le.fit_transform(df['job_type'])
|
| 46 |
|
|
|
|
| 47 |
split_point = int(len(df) * 0.8)
|
| 48 |
train_df = df.iloc[:split_point].copy()
|
| 49 |
test_df = df.iloc[split_point:].copy()
|
|
|
|
| 58 |
for subset in [train_df, test_df]:
|
| 59 |
subset['dayofweek'] = subset['timestamp'].dt.dayofweek
|
| 60 |
subset['month'] = subset['timestamp'].dt.month
|
| 61 |
+
Pin to content
|
| 62 |
subset['year'] = subset['timestamp'].dt.year
|
| 63 |
subset['dayofyear'] = subset['timestamp'].dt.dayofyear
|
| 64 |
subset['is_weekend'] = subset['dayofweek'].isin([5, 6]).astype(int)
|
| 65 |
|
| 66 |
# Train classifier
|
| 67 |
X_train_class = train_df[['dayofweek', 'month', 'year', 'dayofyear',
|
| 68 |
+
'is_weekend', 'job_type_encoded', 'feedback_score',
|
| 69 |
+
'years_of_experience']]
|
| 70 |
y_train_class = train_df['has_job']
|
| 71 |
|
| 72 |
classifier = RandomForestClassifier(
|
|
|
|
| 77 |
# Train regressor
|
| 78 |
train_df_reg = train_df[train_df['has_job'] == 1].copy()
|
| 79 |
X_train_reg = train_df_reg[['dayofweek', 'month', 'year', 'dayofyear',
|
| 80 |
+
'is_weekend', 'job_type_scaled', 'feedback_score',
|
| 81 |
+
'years_exp_scaled', 'job_exp_interaction']]
|
| 82 |
y_train_reg = train_df_reg['contracted_wage']
|
| 83 |
|
| 84 |
regressor = RandomForestRegressor(
|
|
|
|
| 88 |
|
| 89 |
# Prepare future dataframe
|
| 90 |
future_df = test_df[['timestamp', 'job_type', 'job_type_encoded',
|
| 91 |
+
'feedback_score', 'years_of_experience']].rename(columns={'timestamp': 'ds'})
|
| 92 |
|
| 93 |
future_df['dayofweek'] = future_df['ds'].dt.dayofweek
|
| 94 |
future_df['month'] = future_df['ds'].dt.month
|
|
|
|
| 207 |
return obj.tolist()
|
| 208 |
return obj
|
| 209 |
|
| 210 |
+
return json.loads(json.dumps(results, default=convert_to_serializable))
|
| 211 |
|
| 212 |
except Exception as e:
|
| 213 |
+
raise HTTPException(status_code=500, detail=f"Error processing request: {str(e)}")
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
import uvicorn
|