Viraj2307 commited on
Commit
4e0cd14
·
1 Parent(s): 158107d

Added app file

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ import io
6
+
7
+ # Set seed for reproducibility
8
+ np.random.seed(42)
9
+ tf.random.set_seed(42)
10
+
11
+ # Define the correct class labels mapping
12
+ class_labels = ['akiec', 'bcc', 'bkl', 'df', 'nv', 'vasc', 'mel']
13
+
14
+ # Load the trained model
15
+ @st.cache_resource
16
+ def load_model():
17
+ return tf.keras.models.load_model('final_model.h5')
18
+
19
+ model = load_model()
20
+
21
+ # Load mean and std from training (you'll need to provide these)
22
+ @st.cache_resource
23
+ def load_mean_std():
24
+ mean = np.load('mean.npy')
25
+ std = np.load('std.npy')
26
+ return mean, std
27
+
28
+ mean, std = load_mean_std()
29
+
30
+
31
+
32
+ # Define the image preprocessing function
33
+ def preprocess_image(image):
34
+ image = image.resize((28, 28))
35
+ image = np.asarray(image)
36
+ image = (image - mean) / std
37
+ image = np.expand_dims(image, axis=0)
38
+ return image
39
+
40
+ # Streamlit app
41
+ st.title('Skin Lesion Predictor')
42
+
43
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
44
+
45
+ if uploaded_file is not None:
46
+ image = Image.open(io.BytesIO(uploaded_file.read())).convert('RGB')
47
+ st.image(image, caption='Uploaded Image', use_contanier_width=True)
48
+
49
+ # Preprocess the image
50
+ processed_image = preprocess_image(image)
51
+
52
+ # Make prediction
53
+ predictions = model.predict(processed_image)
54
+
55
+ # Get predicted class index and name
56
+ predicted_class_idx = np.argmax(predictions, axis=1)[0]
57
+ predicted_class = class_labels[predicted_class_idx]
58
+ confidence = predictions[0][predicted_class_idx]
59
+
60
+ # Display results
61
+ st.write(f"Predicted Class: {predicted_class}")
62
+ st.write(f"Confidence: {confidence:.2f}")
63
+
64
+ # Display bar chart of all predictions
65
+ st.bar_chart(dict(zip(class_labels, predictions[0])))
66
+
67
+ st.write("Note: This is a demo application and should not be used for medical diagnosis. Always consult with a healthcare professional.")