Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +48 -0
- model.pkl +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.linear_model import LogisticRegression
|
| 4 |
+
from sklearn.preprocessing import StandardScaler
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Employee of the Month API")
|
| 8 |
+
|
| 9 |
+
# ------------------------
|
| 10 |
+
# تدريب الموديل الأساسي
|
| 11 |
+
# ------------------------
|
| 12 |
+
df_train = pd.DataFrame({
|
| 13 |
+
'EmployeeID': [101, 102, 103, 104, 105, 106],
|
| 14 |
+
'PerformanceScore': [90, 85, 95, 80, 88, 92],
|
| 15 |
+
'ProjectsCompleted': [5, 6, 7, 4, 6, 5],
|
| 16 |
+
'Attendance': [98, 92, 95, 90, 97, 96],
|
| 17 |
+
'EmployeeOfTheMonth': [0, 0, 1, 0, 0, 0]
|
| 18 |
+
})
|
| 19 |
+
|
| 20 |
+
X_train = df_train[['PerformanceScore', 'ProjectsCompleted', 'Attendance']]
|
| 21 |
+
y_train = df_train['EmployeeOfTheMonth']
|
| 22 |
+
|
| 23 |
+
scaler = StandardScaler()
|
| 24 |
+
X_train_scaled = scaler.fit_transform(X_train)
|
| 25 |
+
|
| 26 |
+
model = LogisticRegression(class_weight='balanced')
|
| 27 |
+
model.fit(X_train_scaled, y_train)
|
| 28 |
+
|
| 29 |
+
# ------------------------
|
| 30 |
+
# API Endpoint
|
| 31 |
+
# ------------------------
|
| 32 |
+
@app.post("/predict")
|
| 33 |
+
async def predict_employee(file: UploadFile = File(...)):
|
| 34 |
+
# قراءة ملف Excel
|
| 35 |
+
contents = await file.read()
|
| 36 |
+
df_new = pd.read_excel(io.BytesIO(contents))
|
| 37 |
+
|
| 38 |
+
# تجهيز البيانات الجديدة
|
| 39 |
+
X_new_scaled = scaler.transform(df_new[['PerformanceScore', 'ProjectsCompleted', 'Attendance']])
|
| 40 |
+
probs = model.predict_proba(X_new_scaled)[:, 1]
|
| 41 |
+
df_new['ProbabilityOfBeingBest'] = probs
|
| 42 |
+
|
| 43 |
+
best_employee = df_new.loc[df_new['ProbabilityOfBeingBest'].idxmax()]
|
| 44 |
+
result = {
|
| 45 |
+
"BestEmployeeID": int(best_employee['EmployeeID']),
|
| 46 |
+
"Probability": float(best_employee['ProbabilityOfBeingBest']),
|
| 47 |
+
}
|
| 48 |
+
return result
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8f840b09c44c3e57c2b4b35fbfa1cc8efbc245b451929ccb69958563727b7910
|
| 3 |
+
size 895
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
pandas
|
| 4 |
+
scikit-learn
|
| 5 |
+
openpyxl
|