Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -57,11 +57,11 @@ class BatchPredictionRequest(BaseModel):
|
|
| 57 |
class DayPrediction(BaseModel):
|
| 58 |
day: int
|
| 59 |
prediction: int
|
| 60 |
-
probability: float
|
| 61 |
|
| 62 |
class SinglePredictionResponse(BaseModel):
|
| 63 |
prediction: int
|
| 64 |
-
probability: float
|
| 65 |
|
| 66 |
class BatchPredictionResponse(BaseModel):
|
| 67 |
predictions: List[DayPrediction]
|
|
@@ -105,13 +105,22 @@ def predict(request: SinglePredictionRequest):
|
|
| 105 |
# Convert input to numpy array
|
| 106 |
features = np.array(request.features).reshape(1, -1)
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
return SinglePredictionResponse(
|
| 113 |
prediction=int(prediction),
|
| 114 |
-
probability=
|
| 115 |
)
|
| 116 |
except Exception as e:
|
| 117 |
raise HTTPException(status_code=400, detail=f"Prediction error: {str(e)}")
|
|
@@ -129,17 +138,26 @@ def predict_batch(request: BatchPredictionRequest):
|
|
| 129 |
if features.ndim != 2:
|
| 130 |
raise ValueError(f"Expected 2D array, got shape {features.shape}")
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
predictions = model.predict(features)
|
| 134 |
probabilities = model.predict_proba(features)
|
| 135 |
|
| 136 |
# Format results
|
| 137 |
results = []
|
| 138 |
-
for i,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
results.append(DayPrediction(
|
| 140 |
day=i,
|
| 141 |
-
prediction=int(
|
| 142 |
-
probability=
|
| 143 |
))
|
| 144 |
|
| 145 |
return BatchPredictionResponse(predictions=results)
|
|
|
|
| 57 |
class DayPrediction(BaseModel):
|
| 58 |
day: int
|
| 59 |
prediction: int
|
| 60 |
+
probability: float # Probability of HEADACHE (class 1), regardless of prediction
|
| 61 |
|
| 62 |
class SinglePredictionResponse(BaseModel):
|
| 63 |
prediction: int
|
| 64 |
+
probability: float # Probability of HEADACHE (class 1), regardless of prediction
|
| 65 |
|
| 66 |
class BatchPredictionResponse(BaseModel):
|
| 67 |
predictions: List[DayPrediction]
|
|
|
|
| 105 |
# Convert input to numpy array
|
| 106 |
features = np.array(request.features).reshape(1, -1)
|
| 107 |
|
| 108 |
+
# Get probability array for both classes
|
| 109 |
+
prob_array = model.predict_proba(features)[0]
|
| 110 |
+
|
| 111 |
+
# Always return probability of headache (class 1)
|
| 112 |
+
headache_probability = float(prob_array[1])
|
| 113 |
+
|
| 114 |
+
# Make prediction using threshold if available
|
| 115 |
+
if isinstance(model, dict) and 'optimal_threshold' in model:
|
| 116 |
+
threshold = model['optimal_threshold']
|
| 117 |
+
prediction = 1 if headache_probability >= threshold else 0
|
| 118 |
+
else:
|
| 119 |
+
prediction = model.predict(features)[0]
|
| 120 |
|
| 121 |
return SinglePredictionResponse(
|
| 122 |
prediction=int(prediction),
|
| 123 |
+
probability=headache_probability
|
| 124 |
)
|
| 125 |
except Exception as e:
|
| 126 |
raise HTTPException(status_code=400, detail=f"Prediction error: {str(e)}")
|
|
|
|
| 138 |
if features.ndim != 2:
|
| 139 |
raise ValueError(f"Expected 2D array, got shape {features.shape}")
|
| 140 |
|
| 141 |
+
# Get probabilities for all days
|
|
|
|
| 142 |
probabilities = model.predict_proba(features)
|
| 143 |
|
| 144 |
# Format results
|
| 145 |
results = []
|
| 146 |
+
for i, prob_array in enumerate(probabilities, 1):
|
| 147 |
+
# Always use probability of headache (class 1)
|
| 148 |
+
headache_probability = float(prob_array[1])
|
| 149 |
+
|
| 150 |
+
# Make prediction using threshold if available
|
| 151 |
+
if isinstance(model, dict) and 'optimal_threshold' in model:
|
| 152 |
+
threshold = model['optimal_threshold']
|
| 153 |
+
prediction = 1 if headache_probability >= threshold else 0
|
| 154 |
+
else:
|
| 155 |
+
prediction = model.predict(features[i-1:i])[0]
|
| 156 |
+
|
| 157 |
results.append(DayPrediction(
|
| 158 |
day=i,
|
| 159 |
+
prediction=int(prediction),
|
| 160 |
+
probability=headache_probability
|
| 161 |
))
|
| 162 |
|
| 163 |
return BatchPredictionResponse(predictions=results)
|