Story5204 commited on
Commit
483536a
·
verified ·
1 Parent(s): 79a26f5

Upload 5 files

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +30 -0
  3. README.md +5 -5
  4. app.py +92 -0
  5. requirements.txt +6 -0
  6. tuned_ai_model_best_lat.keras +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tuned_ai_model_best_lat.keras filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a minimal base image with Python 3.9 installed
2
+ FROM python:3.9
3
+
4
+ # Set the working directory inside the container to /app
5
+ WORKDIR /app
6
+
7
+ # Copy requirement files first for better Docker caching
8
+ COPY requirements.txt .
9
+
10
+ # Install Python dependencies listed in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # --- CRITICAL FIX: Explicitly set Keras backend ---
14
+ ENV KERAS_BACKEND=tensorflow
15
+
16
+ # Copy application files and the model file (must be named tuned_ai_model_best_lat.keras)
17
+ COPY app.py .
18
+ COPY tuned_ai_model_best_lat.keras .
19
+
20
+ # Recommended security practice
21
+ RUN useradd -m -u 1000 user
22
+ USER user
23
+ ENV HOME=/home/user \
24
+ PATH=/home/user/.local/bin:$PATH
25
+ WORKDIR $HOME/app
26
+ COPY --chown=user . $HOME/app
27
+
28
+ # Define the command to run the Streamlit app, re-including the CRITICAL security flag
29
+ # This flag should prevent the 403 Forbidden error caused by XSRF protection in proxy environments.
30
+ CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: COVID Example
3
- emoji: 🏃
4
- colorFrom: green
5
- colorTo: green
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Covid Detection App
3
+ emoji: 🦀
4
+ colorFrom: indigo
5
+ colorTo: blue
6
  sdk: docker
7
  pinned: false
8
  ---
9
 
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ from PIL import Image
4
+ import tensorflow as tf
5
+ from tensorflow.keras.models import load_model
6
+
7
+ # --- CONFIGURATION ---
8
+ # Define the three possible outcomes
9
+ CLASS_NAMES = {0: 'Normal', 1: 'Viral Pneumonia', 2: 'Covid'}
10
+ # The image size the model expects (224x224 pixels)
11
+ IMAGE_SIZE = 224
12
+
13
+ # --- MODEL LOADING ---
14
+ # Use cache so the model only loads once
15
+ @st.cache_resource
16
+ def load_tuned_model():
17
+ # Load the Keras model file.
18
+ # We include custom_objects to correctly load the VGG16 base.
19
+ return tf.keras.models.load_model(
20
+ "tuned_ai_model_best_lat.keras",
21
+ custom_objects={'VGG16': tf.keras.applications.VGG16}
22
+ )
23
+
24
+ # --- PREDICTION LOGIC ---
25
+ def run_prediction(image_file, model):
26
+ """Processes the image and gets the diagnosis from the model."""
27
+ try:
28
+ # 1. Load and prepare the image
29
+ image = Image.open(image_file).convert("RGB")
30
+ img_array = np.array(image.resize((IMAGE_SIZE, IMAGE_SIZE)))
31
+
32
+ # Add a dimension for the batch (1, 224, 224, 3)
33
+ img_array = np.expand_dims(img_array, axis=0)
34
+
35
+ # Normalize pixel values (0 to 1)
36
+ img_array = img_array / 255.0
37
+
38
+ # 2. Make prediction
39
+ # The result is an array of probabilities for all three classes
40
+ prediction_probabilities = model.predict(img_array).flatten()
41
+
42
+ # 3. Find the most likely class
43
+ class_index = np.argmax(prediction_probabilities)
44
+ predicted_name = CLASS_NAMES[class_index]
45
+ predicted_prob = prediction_probabilities[class_index]
46
+
47
+ return predicted_name, predicted_prob
48
+
49
+ except Exception as e:
50
+ st.error(f"An error occurred during prediction: {e}")
51
+ # Return None if any error happens
52
+ return None, None
53
+
54
+ # --- STREAMLIT INTERFACE ---
55
+
56
+ st.title("COVID Detection from Chest X-ray")
57
+ st.markdown("Upload a chest X-ray image for diagnosis (Normal, Viral Pneumonia, or COVID).")
58
+
59
+ # Attempt to load the model and stop if it fails
60
+ try:
61
+ model = load_tuned_model()
62
+ except Exception as e:
63
+ st.error("Model Loading Failed. Please check dependencies and model file.")
64
+ st.stop()
65
+
66
+ # --- UPLOAD SECTION ---
67
+ uploaded_file = st.file_uploader("Choose an X-ray image...", type=["jpg", "jpeg", "png"])
68
+
69
+ if uploaded_file is not None:
70
+ # Display the uploaded image
71
+ image = Image.open(uploaded_file)
72
+ st.image(image, caption="Uploaded X-ray Image", use_container_width=True)
73
+
74
+ # Run prediction when the button is clicked
75
+ if st.button("Predict Diagnosis", type="primary"):
76
+
77
+ # Run the prediction logic
78
+ predicted_name, predicted_prob = run_prediction(uploaded_file, model)
79
+
80
+ if predicted_name:
81
+ st.markdown("---")
82
+ st.subheader("Predicted Diagnosis")
83
+
84
+ # Display the result simply (no emojis)
85
+ if predicted_name == 'Covid':
86
+ st.error(f"Result: **{predicted_name}**")
87
+ else:
88
+ st.success(f"Result: **{predicted_name}**")
89
+
90
+ # Use the expander to show probability on click
91
+ with st.expander(f"View Confidence Score for {predicted_name}"):
92
+ st.markdown(f"Confidence: **{predicted_prob*100:.2f}%**")
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit==1.49.1
2
+ tensorflow==2.19.0
3
+ numpy==2.0.2
4
+ pandas==2.2.2
5
+ keras==3.10.0
6
+ Pillow==11.3.0
tuned_ai_model_best_lat.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cff545bd9106fa2ec69c99533b8051beda8e95c85d78a5cb3e153c9b3d7d5d2e
3
+ size 61335781