Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,49 @@
|
|
| 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
|
|
|
|
|
|
| 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
|
| 49 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|