omm7 commited on
Commit
fdd9af6
·
verified ·
1 Parent(s): 282f3c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image
4
+ from tensorflow.keras.models import load_model
5
+ import os
6
+ import time
7
+ import requests
8
+ from io import BytesIO
9
+
10
+ # Function to load the model (cached for efficiency)
11
+ @st.cache_resource
12
+ def load_vgg_model():
13
+ return load_model("tuned_ai_model_best_lat.model.keras")
14
+
15
+ # Function to run the prediction and show progress
16
+ def run_prediction(image_path, model, img_size):
17
+ # Create a progress bar and status text
18
+ progress_bar = st.progress(0)
19
+ status_text = st.empty()
20
+
21
+ for i in range(100):
22
+ progress_bar.progress(i + 1)
23
+ status_text.text(f"Processing... {i+1}%")
24
+ time.sleep(0.01)
25
+
26
+ # Process and predict
27
+ try:
28
+ # Check if the input is a file-like object or a string path
29
+ if isinstance(image_path, str):
30
+ image = Image.open(image_path).convert("RGB")
31
+ else:
32
+ image = Image.open(image_path).convert("RGB")
33
+
34
+ img_array = np.array(image.resize((img_size, img_size)))
35
+ img_array = np.expand_dims(img_array, axis=0)
36
+ img_array = img_array / 255.0
37
+
38
+ prediction = model.predict(img_array)
39
+ class_predicted = (prediction > 0.5).astype("int32")[0][0]
40
+
41
+ # Display the result
42
+ if class_predicted == 1:
43
+ st.error(f"Prediction: Covid Detected (Probability: {100*prediction[0][0]:.2f}%)")
44
+ else:
45
+ st.success(f"Prediction: No Covid Detected (Probability: {100*(1 - prediction[0][0]):.2f}%)")
46
+ except Exception as e:
47
+ st.error(f"An error occurred during classification: {e}")
48
+
49
+ # Clear the progress bar
50
+ progress_bar.empty()
51
+ status_text.empty()
52
+
53
+ # Streamlit UI
54
+ st.title("Covid Detection App")
55
+ st.write("Upload your own image to get a prediction.")
56
+
57
+ # Load the model
58
+ model = load_vgg_model()
59
+
60
+ # Define image size (must match the model's input size)
61
+ img_size = 150
62
+
63
+ # --- UI for user image upload ---
64
+ st.subheader("Upload Your Own Image")
65
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
66
+
67
+ if uploaded_file is not None:
68
+ st.image(uploaded_file, caption='Uploaded MRI Scan', use_container_width=True)
69
+
70
+ # Use a button to trigger the classification explicitly
71
+ if st.button("Check for Covid"):
72
+ run_prediction(uploaded_file, model, img_size)