Lab_5 / streamlit_app.py
MuahammadHamza's picture
Upload 4 files
6198aa8 verified
Raw History Blame Contribute Delete
2.04 kB
import os
import sys
import tempfile
import streamlit as st
from PIL import Image
# Ensure we can import the local `gradio/app.py` module (model + graph logic).
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
GRADIO_DIR = os.path.join(BASE_DIR, "gradio")
if GRADIO_DIR not in sys.path:
sys.path.append(GRADIO_DIR)
from app import predict_aml # type: ignore
st.set_page_config(
page_title="AML vs non-AML GAT (H_100X_C2)",
layout="wide",
)
st.title("AML vs non-AML GAT (H_100X_C2) – Streamlit")
st.write(
"Upload a blood smear image from the H_100X_C2 dataset. "
"The app looks up its annotations, builds a graph, and runs the trained GAT model."
)
uploaded_file = st.file_uploader(
"Upload blood smear image (must be one of the H_100X_C2 images, e.g. `1_11_1000_ALL.png`)",
type=["png", "jpg", "jpeg"],
)
def save_uploaded_with_original_name(uploaded) -> str:
"""
Save the uploaded file to a temporary directory, preserving its original
filename. The prediction pipeline only uses the basename to look up
annotations in train/test JSON.
"""
if uploaded is None:
return ""
original_name = os.path.basename(uploaded.name)
tmp_dir = tempfile.mkdtemp(prefix="aml_streamlit_")
dest_path = os.path.join(tmp_dir, original_name)
with open(dest_path, "wb") as f:
f.write(uploaded.read())
return dest_path
if uploaded_file is not None:
image_path = save_uploaded_with_original_name(uploaded_file)
if image_path:
col_img, col_pred = st.columns([2, 1])
with col_img:
st.subheader("Uploaded image")
st.image(Image.open(image_path), caption=os.path.basename(image_path), use_column_width=True)
with col_pred:
st.subheader("Prediction / Status")
status, confidence = predict_aml(image_path)
st.write(status)
st.subheader("Confidence")
st.write(confidence)