Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Form | |
| from fastapi.responses import HTMLResponse | |
| from fastapi.templating import Jinja2Templates | |
| from fastapi.requests import Request | |
| import pickle | |
| import pandas as pd | |
| from typing import Optional | |
| app = FastAPI() | |
| templates = Jinja2Templates(directory="templates") | |
| model = pickle.load(open("hiring_model.pkl", "rb")) | |
| def normalize(value, cast=None): | |
| if value in (None, "", " "): | |
| return None | |
| return cast(value) if cast else value | |
| def predict_job(job_data): | |
| import numpy as np | |
| # Replace None with np.nan to ensure numeric dtype | |
| clean_data = {k: (np.nan if v is None else v) for k, v in job_data.items()} | |
| df = pd.DataFrame([clean_data]) | |
| # Force all columns to float64 to match training data | |
| df = df.astype('float64') | |
| # Predict | |
| probability = model.predict_proba(df)[0][1] | |
| prediction = "HIRED" if probability > 0.5 else "NOT_HIRED" | |
| return { | |
| 'prediction': prediction, | |
| 'hire_probability': f"{probability*100:.1f}", | |
| 'confidence': 'High' if probability > 0.7 or probability < 0.3 else 'Medium' | |
| } | |
| def home(request: Request): | |
| return templates.TemplateResponse("index.html", {"request": request}) | |
| def predict( | |
| request: Request, | |
| client_hire_rate: Optional[float] = Form(None), | |
| client_age_years: Optional[float] = Form(None), | |
| client_active_hires: Optional[int] = Form(None), | |
| client_avg_hourly_rate: Optional[float] = Form(None), | |
| client_total_reviews: Optional[int] = Form(None), | |
| client_total_hires: Optional[int] = Form(None), | |
| client_total_hours: Optional[int] = Form(None), | |
| client_total_spent: Optional[float] = Form(None), | |
| client_rating: Optional[float] = Form(None), | |
| ): | |
| job_data = { | |
| "client_hire_rate": normalize(client_hire_rate, float), | |
| "client_age_years": normalize(client_age_years, float), | |
| "client_active_hires": normalize(client_active_hires, int), | |
| "client_avg_hourly_rate": normalize(client_avg_hourly_rate, float), | |
| "client_total_reviews": normalize(client_total_reviews, int), | |
| "client_total_hires": normalize(client_total_hires, int), | |
| "client_total_hours": normalize(client_total_hours, int), | |
| "client_total_spent": normalize(client_total_spent, float), | |
| "client_rating": normalize(client_rating, float), | |
| } | |
| print(f"job data: {job_data}") | |
| result = predict_job(job_data) | |
| return templates.TemplateResponse( | |
| "index.html", | |
| {"request": request, "result": result} | |
| ) | |