Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -260,23 +260,36 @@ async def validate_model(
|
|
260 |
|
261 |
from predict_utils import run_prediction
|
262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
@app.post("/v1/xgb/predict")
|
264 |
async def predict_model(
|
265 |
file: UploadFile = File(...),
|
266 |
model_name: str = "xgb_models"
|
267 |
):
|
|
|
268 |
if not file.filename.endswith('.csv'):
|
269 |
raise HTTPException(status_code=400, detail="Only CSV files are allowed")
|
270 |
|
271 |
try:
|
|
|
272 |
file_path = UPLOAD_DIR / file.filename
|
273 |
with file_path.open("wb") as buffer:
|
274 |
shutil.copyfileobj(file.file, buffer)
|
275 |
|
|
|
276 |
data_df = pd.read_csv(file_path)
|
|
|
|
|
277 |
if TEXT_COLUMN not in data_df.columns:
|
278 |
raise HTTPException(status_code=400, detail=f"Missing required text column: {TEXT_COLUMN}")
|
279 |
|
|
|
280 |
predictions = run_prediction(data_df, model_name)
|
281 |
|
282 |
return {
|
|
|
260 |
|
261 |
from predict_utils import run_prediction
|
262 |
|
263 |
+
from fastapi import UploadFile, File, HTTPException
|
264 |
+
from predict_utils import run_prediction
|
265 |
+
import pandas as pd
|
266 |
+
import os
|
267 |
+
import shutil
|
268 |
+
import traceback
|
269 |
+
|
270 |
@app.post("/v1/xgb/predict")
|
271 |
async def predict_model(
|
272 |
file: UploadFile = File(...),
|
273 |
model_name: str = "xgb_models"
|
274 |
):
|
275 |
+
# Validate uploaded file
|
276 |
if not file.filename.endswith('.csv'):
|
277 |
raise HTTPException(status_code=400, detail="Only CSV files are allowed")
|
278 |
|
279 |
try:
|
280 |
+
# Save uploaded file to disk
|
281 |
file_path = UPLOAD_DIR / file.filename
|
282 |
with file_path.open("wb") as buffer:
|
283 |
shutil.copyfileobj(file.file, buffer)
|
284 |
|
285 |
+
# Read CSV
|
286 |
data_df = pd.read_csv(file_path)
|
287 |
+
|
288 |
+
# Check required text column exists
|
289 |
if TEXT_COLUMN not in data_df.columns:
|
290 |
raise HTTPException(status_code=400, detail=f"Missing required text column: {TEXT_COLUMN}")
|
291 |
|
292 |
+
# Run prediction
|
293 |
predictions = run_prediction(data_df, model_name)
|
294 |
|
295 |
return {
|