ankush-003 commited on
Commit
951a669
1 Parent(s): e941397

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -53
app.py CHANGED
@@ -1,54 +1,58 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from fastapi.responses import JSONResponse
3
- # from tensorflow.keras.models import load_model
4
- # from tensorflow.keras.preprocessing import image
5
- import numpy as np
6
- from fastapi.middleware.cors import CORSMiddleware
7
- import io
8
- import os
9
-
10
- app = FastAPI()
11
-
12
- app.add_middleware(
13
- CORSMiddleware,
14
- allow_origins=["http://localhost:3000"], # Add your frontend URL
15
- allow_credentials=True,
16
- allow_methods=["*"],
17
- allow_headers=["*"],
18
- )
19
-
20
-
21
- # Load your trained model
22
- # model = load_model('flower_species_model.h5')
23
-
24
- # def preprocess_image(img_file):
25
- # img = image.load_img(img_file, target_size=(64, 64))
26
- # img_array = image.img_to_array(img)
27
- # img_array = np.expand_dims(img_array, axis=0)
28
- # img_array /= 255.0
29
- # return img_array
30
-
31
- @app.post("/predict")
32
- async def predict(files: list[UploadFile] = File(...)):
33
- if not files:
34
- return JSONResponse(content={"error": "No files uploaded"}, status_code=400)
35
-
36
- predictions = []
37
- for file in files:
38
- contents = await file.read()
39
- img = io.BytesIO(contents)
40
- # preprocessed_img = preprocess_image(img)
41
- # prediction = model.predict(preprocessed_img)
42
- # predictions.append(prediction[0][0])
43
- print("File uploaded")
44
-
45
- threshold = 0.5
46
- # predicted_classes = [1 if p > threshold else 0 for p in predictions]
47
- # percentage_class_1 = (predicted_classes.count(1) / len(predicted_classes)) * 100
48
-
49
- # return {"percentage_class_1": round(percentage_class_1, 2)}
50
- return {"message": "Files uploaded", "percentage": 100}
51
-
52
- if __name__ == "__main__":
53
- import uvicorn
 
 
 
 
54
  uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import JSONResponse
3
+ # from tensorflow.keras.models import load_model
4
+ # from tensorflow.keras.preprocessing import image
5
+ import numpy as np
6
+ from fastapi.middleware.cors import CORSMiddleware
7
+ import io
8
+ import os
9
+
10
+ app = FastAPI()
11
+
12
+ app.add_middleware(
13
+ CORSMiddleware,
14
+ allow_origins=["*"], # Add your frontend URL
15
+ allow_credentials=True,
16
+ allow_methods=["*"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
+
21
+ # Load your trained model
22
+ # model = load_model('flower_species_model.h5')
23
+
24
+ # def preprocess_image(img_file):
25
+ # img = image.load_img(img_file, target_size=(64, 64))
26
+ # img_array = image.img_to_array(img)
27
+ # img_array = np.expand_dims(img_array, axis=0)
28
+ # img_array /= 255.0
29
+ # return img_array
30
+
31
+ @app.post("/predict")
32
+ async def predict(files: list[UploadFile] = File(...)):
33
+ if not files:
34
+ return JSONResponse(content={"error": "No files uploaded"}, status_code=400)
35
+
36
+ predictions = []
37
+ for file in files:
38
+ contents = await file.read()
39
+ img = io.BytesIO(contents)
40
+ # preprocessed_img = preprocess_image(img)
41
+ # prediction = model.predict(preprocessed_img)
42
+ # predictions.append(prediction[0][0])
43
+ print("File uploaded")
44
+
45
+ threshold = 0.5
46
+ # predicted_classes = [1 if p > threshold else 0 for p in predictions]
47
+ # percentage_class_1 = (predicted_classes.count(1) / len(predicted_classes)) * 100
48
+
49
+ # return {"percentage_class_1": round(percentage_class_1, 2)}
50
+ return {"message": "Files uploaded", "percentage": 100}
51
+
52
+ @app.get("/")
53
+ async def main():
54
+ return {"message": "Hello World"}
55
+
56
+ if __name__ == "__main__":
57
+ import uvicorn
58
  uvicorn.run(app, host="0.0.0.0", port=8000)