| | import os |
| |
|
| | |
| | os.environ["HF_HOME"] = "/tmp/huggingface" |
| | os.environ["XDG_CACHE_HOME"] = "/tmp/.cache" |
| | os.environ["STREAMLIT_HOME"] = "/tmp/.streamlit" |
| | import streamlit as st |
| | import pandas as pd |
| | import tensorflow as tf |
| | from tensorflow.keras.models import load_model |
| | from PIL import Image |
| | import numpy as np |
| |
|
| |
|
| | from huggingface_hub import hf_hub_download |
| |
|
| | st.title("lung cancer detection") |
| | st.write("Upload an image of a lung X-ray to detect lung cancer.") |
| |
|
| | |
| | model_path = hf_hub_download( |
| | repo_id="lp128396/lung_cancer_model", |
| | filename="lung_cancer_model.keras", |
| | cache_dir=os.getenv("HF_HOME") |
| | ) |
| |
|
| | model = tf.keras.models.load_model(model_path) |
| | |
| | |
| |
|
| | img = st.file_uploader("Choose a image file", type=["jpg", "jpeg", "png","webp"]) |
| | |
| | if img is not None and img.name.endswith(('jpg', 'jpeg', 'png','webp')): |
| | |
| | image = Image.open(img) |
| | st.image(image, caption='Uploaded Image', use_container_width=True) |
| | |
| | image = image.resize((256, 256)) |
| | image_array = np.array(image) |
| |
|
| | |
| | |
| | |
| | |
| |
|
| |
|
| | if image_array.shape[-1] == 4: |
| | image_array = image_array[:, :, :3] |
| |
|
| | image_array = image_array / 255.0 |
| | image_array = np.expand_dims(image_array, axis=0) |
| | class_name_map = { |
| | "lung_acc": "Adenocarcinoma (Cancerous)", |
| | "lung_n": "Normal (Non-Cancerous)", |
| | "lung_scc": "Squamous Carcinoma (Cancerous)" |
| | } |
| | |
| | original_class_names = ["lung_acc", "lung_n", "lung_scc"] |
| |
|
| | |
| | prediction = model.predict(image_array) |
| | predicted_class = np.argmax(prediction) |
| | predicted_key = original_class_names[predicted_class] |
| | predicted_label = class_name_map[predicted_key] |
| |
|
| | |
| | st.success(f"Prediction: {predicted_label} (Confidence: {prediction[0][predicted_class]:.2f})") |
| |
|
| | |
| | else: |
| | st.info("📷 Upload a lung microscope image to get started.") |
| |
|
| | |
| |
|
| | |