File size: 2,924 Bytes
7e1d4ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

import streamlit as st
import pandas as pd
import requests
from dotenv import load_dotenv

from transformers import pipeline
from PIL import Image

from info import pneumonia, covid19, vit_base_patch_16

load_dotenv()

URL = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTcY7VeTAy72aEPJbHmABvnGzW5gzrvSKRzOg&usqp=CAU'


def download_image():
    if st.session_state.img_url:
        st.session_state['image'] = Image.open(
            requests.get(st.session_state.img_url, stream=True).raw)
    else:
        del st.session_state['image']


def file_upload():
    if st.session_state.file_upload:
        st.session_state['image'] = Image.open(st.session_state.file_upload)
    else:
        del st.session_state['image']


def cam_upload():
    if st.session_state.camera:
        st.session_state['image'] = st.session_state.camera
    else:
        del st.session_state['image']


if 'image' not in st.session_state:
    st.session_state['image'] = Image.open(requests.get(URL, stream=True).raw)

st.header("Pneumonia and Covid19 Detector")

with st.sidebar:
    img_upload, cam_upload, url_upload = st.tabs(
        ['๐Ÿ“‚ Upload', '๐Ÿ“ธ CAMERA', '๐Ÿ”— URL'])

    with img_upload:
        uploaded_img = st.file_uploader(
            label="Upload an X-ray image", on_change=file_upload, key='file_upload'
        )
    with cam_upload:
        camera_img = st.camera_input(
            label='Take a picture of X-ray', on_change=cam_upload, key='camera'
        )
    with url_upload:
        img_url = st.text_input(
            label="Enter the X-ray URL", value=URL, on_change=download_image, key="img_url"
        )

    st.image(st.session_state.image)

    analyze_btn = st.button(label='Analyze X-ray', type='primary',
                            use_container_width=True, key='analyze_btn')

if st.session_state.image and st.session_state.analyze_btn:
    with st.spinner():
        pipe = pipeline("image-classification",
                        model="Ajay-user/vit-base-patch16-224-finetuned-pneumonia-detection")
        response = pipe(st.session_state.image)

        df = pd.DataFrame(response)
        result = df.nlargest(n=1, columns='score')
        result_body = f'Model predicts : {result["label"].item()} with {result["score"].item()*100 :0.2f}% confidence'

        with st.expander(label=result_body, expanded=True):
            st.subheader(body=f':red[{result["label"].item()}] Detected')
            st.bar_chart(data=df, x='label', y='score')

        with st.expander(label="X-ray image analyzed"):
            st.image(st.session_state.image)

        with st.expander(label="Model Details"):
            st.markdown(body=vit_base_patch_16)

else:
    tab_1, tab_2 = st.tabs(['Pneumonia', 'Coronavirus'])
    with tab_1:
        st.subheader('Pneumonia')
        st.markdown(body=pneumonia)
    with tab_2:
        st.subheader('Coronavirus')
        st.markdown(body=covid19)