mohamed20003 commited on
Commit
ed624e0
·
verified ·
1 Parent(s): a970ee9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -15
app.py CHANGED
@@ -1,22 +1,67 @@
1
- from fastapi import FastAPI
2
- from pydantic import BaseModel
3
- import uvicorn
 
 
 
 
4
 
5
- # تعريف الـ app
6
- app = FastAPI(title="My AI API", description="Permanent FastAPI endpoint", version="1.0")
7
 
8
- # نموذج البيانات اللي هتجيلك من المستخدم
9
- class InputData(BaseModel):
10
- text: str
 
 
 
 
 
11
 
12
- # الدالة اللي بتتعامل مع البيانات
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  @app.post("/predict")
14
- def predict(data: InputData):
15
- text = data.text
16
- # هنا بتحط كود الموديل بتاعك الحقيقي 👇
17
- result = f"Prediction for: {text}"
18
- return {"result": result}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # لتشغيل السيرفر محليًا
21
  if __name__ == "__main__":
 
22
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
+ import pandas as pd
4
+ from sklearn.linear_model import LogisticRegression
5
+ from sklearn.preprocessing import StandardScaler
6
+ import io
7
+ import joblib
8
 
9
+ # ===== FastAPI app =====
10
+ app = FastAPI(title="Employee of the Month API", version="1.0")
11
 
12
+ # ===== تدريب الموديل مرة واحدة عند تشغيل السيرفر =====
13
+ df_train = pd.DataFrame({
14
+ 'EmployeeID': [101, 102, 103, 104, 105, 106],
15
+ 'PerformanceScore': [90, 85, 95, 80, 88, 92],
16
+ 'ProjectsCompleted': [5, 6, 7, 4, 6, 5],
17
+ 'Attendance': [98, 92, 95, 90, 97, 96],
18
+ 'EmployeeOfTheMonth': [0, 0, 1, 0, 0, 0]
19
+ })
20
 
21
+ X_train = df_train[['PerformanceScore', 'ProjectsCompleted', 'Attendance']]
22
+ y_train = df_train['EmployeeOfTheMonth']
23
+
24
+ scaler = StandardScaler()
25
+ X_train_scaled = scaler.fit_transform(X_train)
26
+
27
+ model = LogisticRegression(class_weight='balanced', random_state=42)
28
+ model.fit(X_train_scaled, y_train)
29
+
30
+ # Optional: حفظ الموديل والمقياس
31
+ joblib.dump(model, 'model.pkl')
32
+ joblib.dump(scaler, 'scaler.pkl')
33
+
34
+ # ===== Endpoint =====
35
  @app.post("/predict")
36
+ async def predict_employee(file: UploadFile = File(...)):
37
+ try:
38
+ # قراءة Excel
39
+ contents = await file.read()
40
+ df_new = pd.read_excel(io.BytesIO(contents))
41
+
42
+ # Scale البيانات الجديدة
43
+ X_new_scaled = scaler.transform(df_new[['PerformanceScore', 'ProjectsCompleted', 'Attendance']])
44
+
45
+ # توقع الاحتمالات
46
+ probs = model.predict_proba(X_new_scaled)[:,1]
47
+ df_new['ProbabilityOfBeingBest'] = probs
48
+
49
+ # اختيار أفضل موظف
50
+ best_employee = df_new.loc[df_new['ProbabilityOfBeingBest'].idxmax()]
51
+
52
+ # معاملات الموديل
53
+ coef_dict = dict(zip(['PerformanceScore','ProjectsCompleted','Attendance'], model.coef_[0].round(3)))
54
+
55
+ return JSONResponse({
56
+ "predictions_all": df_new.to_dict(orient="records"),
57
+ "best_employee": best_employee.to_dict(),
58
+ "feature_coefficients": coef_dict
59
+ })
60
+
61
+ except Exception as e:
62
+ return JSONResponse({"error": str(e)})
63
 
64
+ # ===== تشغيل السيرفر محليًا =====
65
  if __name__ == "__main__":
66
+ import uvicorn
67
  uvicorn.run(app, host="0.0.0.0", port=7860)