Hussein El-Hadidy commited on
Commit
7ee8e47
Β·
1 Parent(s): 98484e9

ECG endpoint

Browse files
ECG/ECG.py β†’ ECG.py RENAMED
File without changes
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import os
2
  import pickle
 
 
3
  from fastapi import FastAPI, File, UploadFile
4
  from fastapi.responses import JSONResponse
5
  from pymongo.mongo_client import MongoClient
@@ -10,6 +12,10 @@ from cloudinary.utils import cloudinary_url
10
  from SkinBurns_Classification import extract_features
11
  from SkinBurns_Segmentation import segment_skin_burns
12
  import requests
 
 
 
 
13
 
14
  app = FastAPI()
15
 
@@ -117,9 +123,6 @@ async def predict_burn(file: UploadFile = File(...)):
117
 
118
 
119
 
120
-
121
-
122
-
123
  # βœ… Optimize and transform image URL
124
  @app.get("/cloudinary/transform")
125
  def transform_image():
@@ -132,3 +135,35 @@ def transform_image():
132
  }
133
  except Exception as e:
134
  return {"error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import pickle
3
+ import shutil
4
+ import uuid
5
  from fastapi import FastAPI, File, UploadFile
6
  from fastapi.responses import JSONResponse
7
  from pymongo.mongo_client import MongoClient
 
12
  from SkinBurns_Classification import extract_features
13
  from SkinBurns_Segmentation import segment_skin_burns
14
  import requests
15
+ import joblib
16
+ import numpy as np
17
+ from ECG import classify_new_ecg
18
+
19
 
20
  app = FastAPI()
21
 
 
123
 
124
 
125
 
 
 
 
126
  # βœ… Optimize and transform image URL
127
  @app.get("/cloudinary/transform")
128
  def transform_image():
 
135
  }
136
  except Exception as e:
137
  return {"error": str(e)}
138
+
139
+
140
+ @app.post("/classify-ecg")
141
+ async def classify_ecg(files: list[UploadFile] = File(...)):
142
+ # Load the pre-trained model
143
+ model_path = "voting_classifier.pkl"
144
+ with open(model_path, "rb") as model_file:
145
+ model = pickle.load(model_file)
146
+
147
+ temp_dir = f"temp_ecg_{uuid.uuid4()}"
148
+ os.makedirs(temp_dir, exist_ok=True)
149
+
150
+ try:
151
+ for file in files:
152
+ file_path = os.path.join(temp_dir, file.filename)
153
+ with open(file_path, "wb") as f:
154
+ shutil.copyfileobj(file.file, f)
155
+
156
+ # Assume both .hea and .dat have same base name
157
+ base_names = set(os.path.splitext(file.filename)[0] for file in files)
158
+ if len(base_names) != 1:
159
+ return JSONResponse(content={"error": "Files must have the same base name"}, status_code=400)
160
+
161
+ base_name = list(base_names)[0]
162
+ file_path = os.path.join(temp_dir, base_name)
163
+
164
+ result = classify_new_ecg(file_path, model)
165
+ return {"result": result}
166
+
167
+ finally:
168
+ shutil.rmtree(temp_dir, ignore_errors=True)
169
+
requirements.txt CHANGED
@@ -16,3 +16,5 @@ wfdb
16
  PyWavelets
17
  xgboost
18
  joblib==1.4.2
 
 
 
16
  PyWavelets
17
  xgboost
18
  joblib==1.4.2
19
+ uuid
20
+ shutil
ECG/voting_classifier.pkl β†’ voting_classifier.pkl RENAMED
File without changes