nehda10 commited on
Commit
70a53ec
Β·
verified Β·
1 Parent(s): 94a8841

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -28
app.py CHANGED
@@ -13,6 +13,7 @@ import numpy as np
13
  from tensorflow.keras.models import load_model
14
  from tensorflow.keras.preprocessing import image
15
  import matplotlib.pyplot as plt
 
16
 
17
 
18
  # ------------------------- PAGE CONFIG -------------------------
@@ -44,35 +45,37 @@ if app_mode == "🏠 Home":
44
  elif app_mode == "🩻 X-ray Classifier":
45
  st.header("🩺 Chest X-ray COVID Prediction")
46
 
47
- uploaded_image = st.file_uploader("πŸ“€ Upload Chest X-ray", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- if uploaded_image:
50
- with st.spinner("πŸ” Predicting..."):
51
- # βœ… Show files in directory to debug model path
52
- st.write("πŸ“ Files in directory:", os.listdir())
53
-
54
- try:
55
- # βœ… Attempt to load model
56
- model = load_model("covid_xray_model.keras")
57
- st.success("βœ… Model loaded successfully!")
58
-
59
- # βœ… Process image and predict
60
- img = image.load_img(uploaded_image, target_size=(224, 224))
61
- img_array = image.img_to_array(img) / 255.0
62
- img_array = np.expand_dims(img_array, axis=0)
63
-
64
- pred = model.predict(img_array)[0]
65
- labels = ['COVID', 'NORMAL', 'Viral Pneumonia']
66
- result = labels[np.argmax(pred)]
67
-
68
- col1, col2 = st.columns([1, 2])
69
- with col1:
70
- st.image(uploaded_image, caption="Uploaded X-ray", use_container_width=True)
71
- with col2:
72
- st.success(f"🧠 **Predicted Condition:** `{result}`")
73
-
74
- except Exception as e:
75
- st.error(f"🚫 Prediction failed. Error: `{e}`")
76
 
77
 
78
 
 
13
  from tensorflow.keras.models import load_model
14
  from tensorflow.keras.preprocessing import image
15
  import matplotlib.pyplot as plt
16
+ from huggingface_hub import hf_hub_download
17
 
18
 
19
  # ------------------------- PAGE CONFIG -------------------------
 
45
  elif app_mode == "🩻 X-ray Classifier":
46
  st.header("🩺 Chest X-ray COVID Prediction")
47
 
48
+ uploaded_image = st.file_uploader("πŸ“€ Upload Chest X-ray", type=["jpg", "jpeg", "png"])
49
+
50
+ if uploaded_image:
51
+ with st.spinner("πŸ” Predicting..."):
52
+ try:
53
+ # βœ… Download model from Hugging Face Hub (only once, then cached)
54
+ model_path = hf_hub_download(
55
+ repo_id="nehda10/covid-xray-model", # πŸ‘ˆ Your HF model repo
56
+ filename="covid_xray_model.keras"
57
+ )
58
+ model = load_model(model_path)
59
+ st.success("βœ… Model loaded successfully!")
60
+
61
+ # βœ… Process image and predict
62
+ img = image.load_img(uploaded_image, target_size=(224, 224))
63
+ img_array = image.img_to_array(img) / 255.0
64
+ img_array = np.expand_dims(img_array, axis=0)
65
+
66
+ pred = model.predict(img_array)[0]
67
+ labels = ['COVID', 'NORMAL', 'Viral Pneumonia']
68
+ result = labels[np.argmax(pred)]
69
+
70
+ col1, col2 = st.columns([1, 2])
71
+ with col1:
72
+ st.image(uploaded_image, caption="Uploaded X-ray", use_container_width=True)
73
+ with col2:
74
+ st.success(f"🧠 **Predicted Condition:** `{result}`")
75
+
76
+ except Exception as e:
77
+ st.error(f"🚫 Prediction failed. Error: `{e}`")
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
 
81