Spaces:
Build error
Build error
Jason Adrian commited on
Commit ·
b7303c6
1
Parent(s): a3c498f
initial commit
Browse files- app.py +88 -0
- requirements.txt +69 -0
- samples/1.2.840.113564.1921681202.202011100756242032.1203801020003.dcm.jpeg +0 -0
- utils/page_utils.py +51 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision.transforms import transforms
|
| 4 |
+
import numpy as np
|
| 5 |
+
from typing import Optional
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import os
|
| 8 |
+
from utils import page_utils
|
| 9 |
+
|
| 10 |
+
from ultralytics import YOLO
|
| 11 |
+
|
| 12 |
+
# Load a model
|
| 13 |
+
model = YOLO('model_- 14 december 2023 12_01.pt') # pretrained YOLOv8n model
|
| 14 |
+
|
| 15 |
+
class_names = ['abdominal', 'adult', 'others', 'pediatric', 'spine']
|
| 16 |
+
class_names.sort()
|
| 17 |
+
|
| 18 |
+
examples_dir = "samples"
|
| 19 |
+
|
| 20 |
+
def image_classifier(inp):
|
| 21 |
+
"""Image Classifier Function.
|
| 22 |
+
Parameters
|
| 23 |
+
----------
|
| 24 |
+
inp: Optional[np.ndarray] = None
|
| 25 |
+
Input image from callback
|
| 26 |
+
Returns
|
| 27 |
+
-------
|
| 28 |
+
Dict
|
| 29 |
+
A dictionary class names and its probability
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
# If input not valid, return dummy data or raise error
|
| 33 |
+
if inp is None:
|
| 34 |
+
return {'cat': 0.3, 'dog': 0.7}
|
| 35 |
+
|
| 36 |
+
result = model(inp)
|
| 37 |
+
|
| 38 |
+
# postprocess
|
| 39 |
+
labeled_result = {class_names[label]: confidence for label, confidence in zip(result.top5, result.top5conf)}
|
| 40 |
+
|
| 41 |
+
return labeled_result
|
| 42 |
+
|
| 43 |
+
# gradio code block for input and output
|
| 44 |
+
with gr.Blocks() as app:
|
| 45 |
+
gr.Markdown("# Lung Cancer Classification")
|
| 46 |
+
|
| 47 |
+
with open('index.html', encoding="utf-8") as f:
|
| 48 |
+
description = f.read()
|
| 49 |
+
|
| 50 |
+
# gradio code block for input and output
|
| 51 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue=page_utils.KALBE_THEME_COLOR, secondary_hue=page_utils.KALBE_THEME_COLOR).set(
|
| 52 |
+
button_primary_background_fill="*primary_600",
|
| 53 |
+
button_primary_background_fill_hover="*primary_500",
|
| 54 |
+
button_primary_text_color="white",
|
| 55 |
+
)) as app:
|
| 56 |
+
with gr.Column():
|
| 57 |
+
gr.HTML(description)
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
inp_img = gr.Image()
|
| 62 |
+
with gr.Row():
|
| 63 |
+
clear_btn = gr.Button(value="Clear")
|
| 64 |
+
process_btn = gr.Button(value="Process", variant="primary")
|
| 65 |
+
with gr.Column():
|
| 66 |
+
out_txt = gr.Label(label="Probabilities", num_top_classes=5)
|
| 67 |
+
|
| 68 |
+
process_btn.click(image_classifier, inputs=inp_img, outputs=out_txt)
|
| 69 |
+
clear_btn.click(lambda:(
|
| 70 |
+
gr.update(value=None),
|
| 71 |
+
gr.update(value=None)
|
| 72 |
+
),
|
| 73 |
+
inputs=None,
|
| 74 |
+
outputs=[inp_img, out_txt])
|
| 75 |
+
|
| 76 |
+
gr.Markdown("## Image Examples")
|
| 77 |
+
gr.Examples(
|
| 78 |
+
examples=[os.path.join(examples_dir, "1.2.840.113564.1921681202.202011100756242032.1203801020003.dcm.jpeg")
|
| 79 |
+
],
|
| 80 |
+
inputs=inp_img,
|
| 81 |
+
outputs=out_txt,
|
| 82 |
+
fn=image_classifier,
|
| 83 |
+
cache_examples=False,
|
| 84 |
+
)
|
| 85 |
+
gr.Markdown(line_breaks=True, value='Author: Jason Adrian (jasonadriann6@gmail.com) <div class="row"><a href="https://github.com/jasonadriann?tab=repositories"><img alt="GitHub" src="https://img.shields.io/badge/Jason%20Adrian-000000?logo=github"> </div>')
|
| 86 |
+
|
| 87 |
+
# demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label")
|
| 88 |
+
app.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
asttokens==2.4.1
|
| 2 |
+
certifi==2023.11.17
|
| 3 |
+
charset-normalizer==3.3.2
|
| 4 |
+
colorama==0.4.6
|
| 5 |
+
comm==0.2.0
|
| 6 |
+
contourpy==1.2.0
|
| 7 |
+
cycler==0.12.1
|
| 8 |
+
debugpy==1.8.0
|
| 9 |
+
decorator==5.1.1
|
| 10 |
+
exceptiongroup==1.2.0
|
| 11 |
+
executing==2.0.1
|
| 12 |
+
filelock==3.13.1
|
| 13 |
+
fire==0.5.0
|
| 14 |
+
fonttools==4.46.0
|
| 15 |
+
fsspec==2023.12.1
|
| 16 |
+
idna==3.6
|
| 17 |
+
ipykernel==6.27.1
|
| 18 |
+
ipython==8.18.1
|
| 19 |
+
jedi==0.19.1
|
| 20 |
+
Jinja2==3.1.2
|
| 21 |
+
joblib==1.3.2
|
| 22 |
+
jupyter_client==8.6.0
|
| 23 |
+
jupyter_core==5.5.0
|
| 24 |
+
kiwisolver==1.4.5
|
| 25 |
+
MarkupSafe==2.1.3
|
| 26 |
+
matplotlib==3.8.2
|
| 27 |
+
matplotlib-inline==0.1.6
|
| 28 |
+
monai==1.3.0
|
| 29 |
+
mpmath==1.3.0
|
| 30 |
+
nest-asyncio==1.5.8
|
| 31 |
+
networkx==3.2.1
|
| 32 |
+
numpy==1.26.2
|
| 33 |
+
opencv-python==4.8.1.78
|
| 34 |
+
packaging==23.2
|
| 35 |
+
pandas==2.1.3
|
| 36 |
+
parso==0.8.3
|
| 37 |
+
Pillow==10.1.0
|
| 38 |
+
platformdirs==4.1.0
|
| 39 |
+
prompt-toolkit==3.0.43
|
| 40 |
+
psutil==5.9.6
|
| 41 |
+
pure-eval==0.2.2
|
| 42 |
+
py-cpuinfo==9.0.0
|
| 43 |
+
Pygments==2.17.2
|
| 44 |
+
pyparsing==3.1.1
|
| 45 |
+
python-dateutil==2.8.2
|
| 46 |
+
pytz==2023.3.post1
|
| 47 |
+
pywin32==306
|
| 48 |
+
PyYAML==6.0.1
|
| 49 |
+
pyzmq==25.1.2
|
| 50 |
+
requests==2.31.0
|
| 51 |
+
scikit-learn==1.3.2
|
| 52 |
+
scipy==1.11.4
|
| 53 |
+
seaborn==0.13.0
|
| 54 |
+
six==1.16.0
|
| 55 |
+
stack-data==0.6.3
|
| 56 |
+
sympy==1.12
|
| 57 |
+
termcolor==2.4.0
|
| 58 |
+
thop==0.1.1.post2209072238
|
| 59 |
+
threadpoolctl==3.2.0
|
| 60 |
+
torch==2.1.1
|
| 61 |
+
torchvision==0.16.1
|
| 62 |
+
tornado==6.4
|
| 63 |
+
tqdm==4.66.1
|
| 64 |
+
traitlets==5.14.0
|
| 65 |
+
typing_extensions==4.8.0
|
| 66 |
+
tzdata==2023.3
|
| 67 |
+
ultralytics==8.0.225
|
| 68 |
+
urllib3==2.1.0
|
| 69 |
+
wcwidth==0.2.12
|
samples/1.2.840.113564.1921681202.202011100756242032.1203801020003.dcm.jpeg
ADDED
|
utils/page_utils.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class ColorPalette:
|
| 5 |
+
"""Color Palette Container."""
|
| 6 |
+
all = []
|
| 7 |
+
|
| 8 |
+
def __init__(
|
| 9 |
+
self,
|
| 10 |
+
c50: str,
|
| 11 |
+
c100: str,
|
| 12 |
+
c200: str,
|
| 13 |
+
c300: str,
|
| 14 |
+
c400: str,
|
| 15 |
+
c500: str,
|
| 16 |
+
c600: str,
|
| 17 |
+
c700: str,
|
| 18 |
+
c800: str,
|
| 19 |
+
c900: str,
|
| 20 |
+
c950: str,
|
| 21 |
+
name: Optional[str] = None,
|
| 22 |
+
):
|
| 23 |
+
self.c50 = c50
|
| 24 |
+
self.c100 = c100
|
| 25 |
+
self.c200 = c200
|
| 26 |
+
self.c300 = c300
|
| 27 |
+
self.c400 = c400
|
| 28 |
+
self.c500 = c500
|
| 29 |
+
self.c600 = c600
|
| 30 |
+
self.c700 = c700
|
| 31 |
+
self.c800 = c800
|
| 32 |
+
self.c900 = c900
|
| 33 |
+
self.c950 = c950
|
| 34 |
+
self.name = name
|
| 35 |
+
ColorPalette.all.append(self)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
KALBE_THEME_COLOR = ColorPalette(
|
| 39 |
+
name='kalbe',
|
| 40 |
+
c50='#f2f9e8',
|
| 41 |
+
c100='#dff3c4',
|
| 42 |
+
c200='#c2e78d',
|
| 43 |
+
c300='#9fd862',
|
| 44 |
+
c400='#7fc93f',
|
| 45 |
+
c500='#3F831C',
|
| 46 |
+
c600='#31661a',
|
| 47 |
+
c700='#244c13',
|
| 48 |
+
c800='#18340c',
|
| 49 |
+
c900='#0c1b06',
|
| 50 |
+
c950='#050a02',
|
| 51 |
+
)
|