heboya8 commited on
Commit
bfd489d
·
verified ·
1 Parent(s): a458b77

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +48 -53
main.py CHANGED
@@ -1,63 +1,58 @@
1
- # Import key libraries and packages
2
- from fastapi import FastAPI
3
- import pickle
4
- import uvicorn
5
- import pandas as pd
6
- import sklearn
7
  import os
8
- import sys
 
 
9
 
10
- DIRPATH = os.path.dirname(os.path.realpath(__file__))
11
- ml_component= "./ml_sepsis.pkl"
 
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
- # Endpoints
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
- return prediction
 
49
 
50
- #Prediction endpoints
51
- def make_prediction(data_prepared):
 
52
 
53
- output_pred = data_prepared
 
 
54
 
55
- if output_pred == 0:
56
- output_pred = "Sepsis status is Negative"
57
- else:
58
- output_pred = "Sepsis status is Positive"
 
 
59
 
60
- return output_pred
61
-
62
- if __name__=='__main__':
63
- uvicorn.run('main:app', reload=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )