File size: 2,081 Bytes
ed624e0
 
c493835
ed624e0
 
a970ee9
c493835
ed624e0
 
 
 
 
c493835
ed624e0
a970ee9
ed624e0
 
 
 
 
 
c493835
ed624e0
 
c493835
 
ed624e0
a970ee9
c493835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed624e0
c493835
 
 
 
 
a970ee9
 
ed624e0
a970ee9
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from fastapi import FastAPI, UploadFile, File
import pandas as pd
import io
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler

# === إعداد البيانات والموديل كما في Gradio ===
df_train = pd.DataFrame({
    'EmployeeID': [101, 102, 103, 104, 105, 106],
    'PerformanceScore': [90, 85, 95, 80, 88, 92],
    'ProjectsCompleted': [5, 6, 7, 4, 6, 5],
    'Attendance': [98, 92, 95, 90, 97, 96],
    'EmployeeOfTheMonth': [0, 0, 1, 0, 0, 0]  
})

X_train = df_train[['PerformanceScore', 'ProjectsCompleted', 'Attendance']]
y_train = df_train['EmployeeOfTheMonth']

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)

model = LogisticRegression(class_weight='balanced')
model.fit(X_train_scaled, y_train)

# === إنشاء FastAPI app ===
app = FastAPI(title="Employee of the Month API", version="1.0")

@app.post("/predict")
async def predict(file: UploadFile = File(...)):
    # قراءة الـ Excel
    contents = await file.read()
    df_new = pd.read_excel(io.BytesIO(contents))
    
    # scale البيانات الجديدة
    X_new_scaled = scaler.transform(df_new[['PerformanceScore', 'ProjectsCompleted', 'Attendance']])
    
    # التنبؤ بالاحتمالات
    probs = model.predict_proba(X_new_scaled)[:,1] 
    df_new['ProbabilityOfBeingBest'] = probs
    
    # أفضل موظف
    best_employee = df_new.loc[df_new['ProbabilityOfBeingBest'].idxmax()]
    
    # feature coefficients
    coef = model.coef_[0]
    features = ['PerformanceScore', 'ProjectsCompleted', 'Attendance']
    coef_dict = {f: round(c,3) for f,c in zip(features, coef)}
    
    # تحويل النتائج لقوائم/dict علشان JSON
    df_new_list = df_new.to_dict(orient='records')
    best_employee_dict = best_employee.to_dict()
    
    return {
        "predictions": df_new_list,
        "best_employee": best_employee_dict,
        "feature_coefficients": coef_dict
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)