Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
@@ -1,63 +1,58 @@
|
|
1 |
-
|
2 |
-
from fastapi import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import pandas as pd
|
6 |
-
import sklearn
|
7 |
import os
|
8 |
-
import
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
# Function to load pickle file
|
14 |
-
def load_pickle(filename):
|
15 |
-
with open(filename, 'rb') as file:
|
16 |
-
data = pickle.load(file)
|
17 |
-
return data
|
18 |
-
|
19 |
-
# Load pickle file
|
20 |
-
ml_components = load_pickle(ml_component)
|
21 |
-
|
22 |
-
# Components in the pickle file
|
23 |
-
ml_model = ml_components['model']
|
24 |
-
pipeline_processing = ml_components['pipeline']
|
25 |
-
|
26 |
-
# API base configuration
|
27 |
app = FastAPI()
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
@app.get('/Predict_Sepsis')
|
32 |
-
async def predict(Plasma_glucose: int, Blood_Work_Result_1: int,
|
33 |
-
Blood_Pressure: int, Blood_Work_Result_2: int,
|
34 |
-
Blood_Work_Result_3: int, Body_mass_index: float,
|
35 |
-
Blood_Work_Result_4: float,Age: int, Insurance:float):
|
36 |
-
|
37 |
-
data = pd.DataFrame({'Plasma glucose': [Plasma_glucose], 'Blood Work Result-1': [Blood_Work_Result_1],
|
38 |
-
'Blood Pressure': [Blood_Pressure], 'Blood Work Result-2': [Blood_Work_Result_2],
|
39 |
-
'Blood Work Result-3': [Blood_Work_Result_3], 'Body mass index': [Body_mass_index],
|
40 |
-
'Blood Work Result-4': [Blood_Work_Result_4], 'Age': [Age], 'Insurance':[Insurance]})
|
41 |
-
|
42 |
-
data_prepared = pipeline_processing.transform(data)
|
43 |
-
|
44 |
-
model_output = ml_model.predict(data_prepared).tolist()
|
45 |
-
|
46 |
-
prediction = make_prediction(model_output)
|
47 |
|
48 |
-
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
|
|
52 |
|
53 |
-
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, Request
|
2 |
+
from fastapi.templating import Jinja2Templates
|
3 |
+
from fastapi.staticfiles import StaticFiles
|
4 |
+
from PIL import Image
|
|
|
|
|
5 |
import os
|
6 |
+
import uuid
|
7 |
+
import logging
|
8 |
+
import shutil
|
9 |
|
10 |
+
# Configure logging
|
11 |
+
logging.basicConfig(level=logging.INFO)
|
12 |
+
logger = logging.getLogger(__name__)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
app = FastAPI()
|
15 |
|
16 |
+
# Mount static files
|
17 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Set up templates
|
20 |
+
templates = Jinja2Templates(directory="templates")
|
21 |
|
22 |
+
# Set upload folder
|
23 |
+
UPLOAD_FOLDER = "static/uploads"
|
24 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
25 |
|
26 |
+
@app.get("/")
|
27 |
+
async def index(request: Request):
|
28 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
29 |
|
30 |
+
@app.post("/upload")
|
31 |
+
async def upload_file(request: Request, file: UploadFile = File(...)):
|
32 |
+
try:
|
33 |
+
# Save uploaded file
|
34 |
+
filename = f"{uuid.uuid4()}{os.path.splitext(file.filename)[1]}"
|
35 |
+
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
36 |
|
37 |
+
# Write file content
|
38 |
+
with open(filepath, "wb") as f:
|
39 |
+
shutil.copyfileobj(file.file, f)
|
40 |
+
logger.info(f"File saved: {filename}")
|
41 |
+
|
42 |
+
# Verify and resize image
|
43 |
+
image = Image.open(filepath).convert("RGB")
|
44 |
+
image = image.resize((800, 600)) # Resize for consistent display
|
45 |
+
image.save(filepath)
|
46 |
+
logger.info(f"Image resized and saved: {filename}")
|
47 |
+
|
48 |
+
return templates.TemplateResponse(
|
49 |
+
"results.html",
|
50 |
+
{"request": request, "image": f"/static/uploads/{filename}"}
|
51 |
+
)
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
logger.error(f"Error processing file: {str(e)}")
|
55 |
+
return templates.TemplateResponse(
|
56 |
+
"index.html",
|
57 |
+
{"request": request, "error": f"Error processing file: {str(e)}"}
|
58 |
+
)
|