-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (37 loc) · 1.55 KB
/
app.py
File metadata and controls
44 lines (37 loc) · 1.55 KB
1
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
# main.py (FastAPI)
import os
from fastapi import FastAPI, HTTPException, Header
from pydantic import BaseModel, conint, constr
from predict import predict_score_and_proba
API_KEY = os.getenv("AI_API_KEY", "")
THRESH = float(os.getenv("AI_PASS_THRESHOLD", "0.6"))
class CandidateIn(BaseModel):
name: constr(strip_whitespace=True, min_length=1)
interview_score: conint(ge=0, le=100)
skill_score: conint(ge=0, le=100)
personality_score: conint(ge=0, le=100)
education_level: constr(strip_whitespace=True)
recruitment_strategy: constr(strip_whitespace=True)
experience_level: constr(strip_whitespace=True)
status: constr(strip_whitespace=True)
class ScoreOut(BaseModel):
ai_score: int
ai_notes: str
proba: float | None = None
passed: bool
app = FastAPI(title="TemanHire AI Service")
@app.post("/score", response_model=ScoreOut)
def score(payload: CandidateIn, x_api_key: str = Header(default="")):
if API_KEY and x_api_key != API_KEY:
raise HTTPException(401, "Invalid API key")
ai_score, proba = predict_score_and_proba(payload.dict())
# aturan pass:
passed = False
if proba is not None:
passed = proba >= THRESH
else:
passed = ai_score >= int(THRESH * 100)
notes = (f"Skor dihitung dari Interview={payload.interview_score}, "
f"Skill={payload.skill_score}, Personality={payload.personality_score}, "
f"Edu={payload.education_level}, Exp={payload.experience_level}.")
return {"ai_score": ai_score, "ai_notes": notes, "proba": proba, "passed": passed}