zaman855 commited on
Commit
d4b6c32
·
verified ·
1 Parent(s): f7cff33

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +53 -48
main.py CHANGED
@@ -1,48 +1,53 @@
1
- from fastapi import FastAPI, File, UploadFile
2
- from fastapi.middleware.cors import CORSMiddleware
3
- import tensorflow as tf
4
- import numpy as np
5
- from vit_keras import vit
6
- import tensorflow_addons as tfa
7
- from io import BytesIO
8
- from PIL import Image
9
-
10
- app = FastAPI()
11
-
12
- # CORS
13
- app.add_middleware(
14
- CORSMiddleware,
15
- allow_origins=["*"],
16
- allow_credentials=True,
17
- allow_methods=["*"],
18
- allow_headers=["*"],
19
- )
20
-
21
- # Model setup
22
- vit_model = vit.vit_b16(image_size=224, activation='softmax', pretrained=True, include_top=False, pretrained_top=False, classes=7)
23
- model1 = tf.keras.Sequential([
24
- vit_model,
25
- tf.keras.layers.Flatten(),
26
- tf.keras.layers.BatchNormalization(),
27
- tf.keras.layers.Dense(11, activation=tfa.activations.gelu),
28
- tf.keras.layers.BatchNormalization(),
29
- tf.keras.layers.Dense(7, activation='softmax')
30
- ])
31
- model1.load_weights('vit_model_weights.h5')
32
-
33
- labels = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']
34
-
35
- def preprocess_image(image: Image.Image):
36
- image = image.resize((224, 224))
37
- image = np.array(image) / 255.0
38
- image = np.expand_dims(image, axis=0)
39
- return image
40
-
41
- @app.post("/predict/")
42
- async def predict(file: UploadFile = File(...)):
43
- image = Image.open(BytesIO(await file.read()))
44
- processed_image = preprocess_image(image)
45
- predictions = model1.predict(processed_image)
46
- predicted_class = labels[np.argmax(predictions)]
47
- confidence = np.max(predictions)
48
- return {"predicted_class": predicted_class, "confidence": float(confidence)}
 
 
 
 
 
 
1
+ # main.py
2
+ from fastapi import FastAPI, File, UploadFile
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import tensorflow as tf
5
+ import numpy as np
6
+ from vit_keras import vit
7
+ import tensorflow_addons as tfa
8
+ from io import BytesIO
9
+ from PIL import Image
10
+
11
+ app = FastAPI()
12
+
13
+ # CORS configuration
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"], # Adjust this according to your security needs
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+ # Load the model
23
+ vit_model = vit.vit_b16(image_size=224, activation='softmax', pretrained=True, include_top=False, pretrained_top=False, classes=7)
24
+ model1 = tf.keras.Sequential([
25
+ vit_model,
26
+ tf.keras.layers.Flatten(),
27
+ tf.keras.layers.BatchNormalization(),
28
+ tf.keras.layers.Dense(11, activation=tfa.activations.gelu),
29
+ tf.keras.layers.BatchNormalization(),
30
+ tf.keras.layers.Dense(7, activation='softmax')
31
+ ])
32
+ model1.load_weights('vit_model_weights.h5')
33
+
34
+ labels = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']
35
+
36
+ def preprocess_image(image: Image.Image):
37
+ image = image.resize((224, 224))
38
+ image = np.array(image) / 255.0
39
+ image = np.expand_dims(image, axis=0)
40
+ return image
41
+
42
+ @app.post("/predict/")
43
+ async def predict(file: UploadFile = File(...)):
44
+ image = Image.open(BytesIO(await file.read()))
45
+ processed_image = preprocess_image(image)
46
+ predictions = model1.predict(processed_image)
47
+ predicted_class = labels[np.argmax(predictions)]
48
+ confidence = np.max(predictions)
49
+ return {"predicted_class": predicted_class, "confidence": float(confidence)}
50
+
51
+ if __name__ == "__main__":
52
+ import uvicorn
53
+ uvicorn.run(app, host="0.0.0.0", port=8000)