Team Ai
Apppublic

johnwesley756/instance-segmentation

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
app.py117 linesDownload Raw Back to root
1import os2import sys3 4# โœ… Absolute path fix (Docker + HF safe)5BASE_DIR = os.path.dirname(os.path.abspath(__file__))6sys.path.insert(0, BASE_DIR)7 8import streamlit as st9import requests10import cv211import numpy as np12from PIL import Image13import io14import base6415 16from ui import run_inference  # direct inference17 18# Internal FastAPI endpoint19API_URL = "http://127.0.0.1:8000/predict"20 21# Page config22st.set_page_config(23    page_title="Tooth Decay Detection",24    page_icon="๐Ÿฆท",25    layout="centered"26)27 28st.title("๐Ÿฆท Tooth Decay Detection")29st.write("Detect **Tooth**, **Caries**, or **Cavity** from dental images.")30 31# ๐Ÿ”€ Mode selector32mode = st.radio(33    "Select inference mode",34    ["Direct (Streamlit)", "API (FastAPI)"],35    help="Direct = faster | API = full-stack architecture"36)37 38uploaded_file = st.file_uploader(39    "Upload dental image",40    type=["jpg", "jpeg", "png"]41)42 43if uploaded_file:44    image = Image.open(uploaded_file)45    st.subheader("๐Ÿ“ท Original Image")46    st.image(image, use_container_width=True)47 48    img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)49 50    if st.button("๐Ÿ” Analyze Image"):51        with st.spinner("Analyzing image..."):52 53            # ======================================================54            # ๐Ÿ”ต MODE 1: DIRECT INFERENCE55            # ======================================================56            if mode == "Direct (Streamlit)":57                severity, summary, detections, annotated = run_inference(img_bgr)58                annotated_rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)59                result_image = Image.fromarray(annotated_rgb)60 61            # ======================================================62            # ๐ŸŸข MODE 2: FASTAPI INFERENCE63            # ======================================================64            else:65                files = {66                    "file": (67                        uploaded_file.name,68                        uploaded_file.getvalue(),69                        uploaded_file.type70                    )71                }72 73                try:74                    response = requests.post(API_URL, files=files, timeout=120)75                except Exception as e:76                    st.error(f"โŒ Failed to connect to API: {e}")77                    st.stop()78 79                if response.status_code != 200:80                    st.error(f"โŒ API Error: {response.status_code}")81                    st.text(response.text)82                    st.stop()83 84                data = response.json()85 86                severity = data["severity"]87                summary = data["summary"]88                detections = data["detections"]89 90                annotated_bytes = base64.b64decode(data["annotated_image"])91                result_image = Image.open(io.BytesIO(annotated_bytes))92 93        # ===========================94        # ๐Ÿ“Š DISPLAY RESULTS95        # ===========================96        st.subheader("๐ŸŽฏ Detection Result")97        st.image(98            result_image,99            caption=f"Severity: {severity}",100            use_container_width=True101        )102 103        st.subheader("๐Ÿ“‹ Summary")104        st.info(summary)105 106        if detections:107            st.subheader(f"๐Ÿ”Ž Detections ({len(detections)})")108            st.json(detections)109        else:110            st.warning("No detections found.")111 112else:113    st.info("๐Ÿ‘† Upload an image to begin.")114 115st.markdown("---")116st.caption("Powered by YOLOv8 ยท FastAPI ยท Streamlit")117