Prasanna commited on
Commit
1e51aa0
·
1 Parent(s): 2d72593

Add app files

Browse files
Files changed (3) hide show
  1. app.py +66 -0
  2. requirements.txt +6 -2
  3. src/streamlit_app.py +0 -40
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import tensorflow as tf
4
+ from tensorflow.keras.models import load_model
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ from huggingface_hub import hf_hub_download
9
+
10
+ st.title("lung cancer detection")
11
+ st.write("Upload an image of a lung X-ray to detect lung cancer.")
12
+
13
+ # Download model from Hugging Face model hub
14
+ model_path = hf_hub_download(
15
+ repo_id="your-username/lung_cancer_model", # 👈 replace with your actual username and repo
16
+ filename="lung_cancer_model.keras"
17
+ )
18
+
19
+ model = tf.keras.models.load_model(model_path)
20
+ #model = load_model('lung_cancer_model.keras')
21
+ #for uploading a image
22
+
23
+ img = st.file_uploader("Choose a image file", type=["jpg", "jpeg", "png","webp"])
24
+ # Check if an image file is uploaded
25
+ if img is not None and img.name.endswith(('jpg', 'jpeg', 'png','webp')):
26
+ # Display the image
27
+ image = Image.open(img)
28
+ st.image(image, caption='Uploaded Image', use_container_width=True)
29
+ # --- Image preprocessing steps ---
30
+ image = image.resize((256, 256)) # replace with your model’s input size
31
+ image_array = np.array(image)
32
+
33
+ #🎯 Optional: Auto-detect input size
34
+ #You can dynamically get the expected input shape like this:
35
+ #input_size = model.input_shape[1:3] # (height, width)
36
+ #Image = image.resize(input_size)
37
+
38
+
39
+ if image_array.shape[-1] == 4: # RGBA to RGB
40
+ image_array = image_array[:, :, :3]
41
+
42
+ image_array = image_array / 255.0 # normalize if model was trained on normalized images
43
+ image_array = np.expand_dims(image_array, axis=0) # add batch dimension
44
+ class_name_map = {
45
+ "lung_acc": "Adenocarcinoma (Cancerous)",
46
+ "lung_n": "Normal (Non-Cancerous)",
47
+ "lung_scc": "Squamous Carcinoma (Cancerous)"
48
+ }
49
+ # List must match order in which the model was trained
50
+ original_class_names = ["lung_acc", "lung_n", "lung_scc"]
51
+
52
+ # Make prediction
53
+ prediction = model.predict(image_array)
54
+ predicted_class = np.argmax(prediction)
55
+ predicted_key = original_class_names[predicted_class]
56
+ predicted_label = class_name_map[predicted_key]
57
+
58
+ # Show result
59
+ st.success(f"Prediction: {predicted_label} (Confidence: {prediction[0][predicted_class]:.2f})")
60
+
61
+
62
+
63
+
64
+
65
+
66
+
requirements.txt CHANGED
@@ -1,3 +1,7 @@
1
- altair
 
2
  pandas
3
- streamlit
 
 
 
 
1
+ numpy
2
+ tensorflow
3
  pandas
4
+ scikit-learn
5
+ Image
6
+ streamlit
7
+ huggingface_hub
src/streamlit_app.py DELETED
@@ -1,40 +0,0 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))