Spaces:
Sleeping
Sleeping
| 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") | |
| 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) | |