sharren commited on
Commit
ae0582e
β€’
1 Parent(s): f5f0514

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ from dotenv import load_dotenv
5
+
6
+ from transformers import pipeline
7
+ from PIL import Image
8
+
9
+ from info import pneumonia, covid19, vit_base_patch_16
10
+
11
+ load_dotenv()
12
+
13
+ URL = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTcY7VeTAy72aEPJbHmABvnGzW5gzrvSKRzOg&usqp=CAU'
14
+
15
+
16
+ def download_image():
17
+ if st.session_state.img_url:
18
+ st.session_state['image'] = Image.open(
19
+ requests.get(st.session_state.img_url, stream=True).raw)
20
+ else:
21
+ del st.session_state['image']
22
+
23
+
24
+ def file_upload():
25
+ if st.session_state.file_upload:
26
+ st.session_state['image'] = Image.open(st.session_state.file_upload)
27
+ else:
28
+ del st.session_state['image']
29
+
30
+
31
+ def cam_upload():
32
+ if st.session_state.camera:
33
+ st.session_state['image'] = st.session_state.camera
34
+ else:
35
+ del st.session_state['image']
36
+
37
+
38
+ if 'image' not in st.session_state:
39
+ st.session_state['image'] = Image.open(requests.get(URL, stream=True).raw)
40
+
41
+ st.header("Pneumonia and Covid19 Detector")
42
+
43
+ with st.sidebar:
44
+ img_upload, cam_upload, url_upload = st.tabs(
45
+ ['πŸ“‚ Upload', 'πŸ“Έ CAMERA', 'πŸ”— URL'])
46
+
47
+ with img_upload:
48
+ uploaded_img = st.file_uploader(
49
+ label="Upload an X-ray image", on_change=file_upload, key='file_upload'
50
+ )
51
+ with cam_upload:
52
+ camera_img = st.camera_input(
53
+ label='Take a picture of X-ray', on_change=cam_upload, key='camera'
54
+ )
55
+ with url_upload:
56
+ img_url = st.text_input(
57
+ label="Enter the X-ray URL", value=URL, on_change=download_image, key="img_url"
58
+ )
59
+
60
+ st.image(st.session_state.image)
61
+
62
+ analyze_btn = st.button(label='Analyze X-ray', type='primary',
63
+ use_container_width=True, key='analyze_btn')
64
+
65
+ if st.session_state.image and st.session_state.analyze_btn:
66
+ with st.spinner():
67
+ pipe = pipeline("image-classification",
68
+ model="sharren/vit-beta2-0.99")
69
+ response = pipe(st.session_state.image)
70
+
71
+ df = pd.DataFrame(response)
72
+ result = df.nlargest(n=1, columns='score')
73
+ result_body = f'Model predicts : {result["label"].item()} with {result["score"].item()*100 :0.2f}% confidence'
74
+
75
+ with st.expander(label=result_body, expanded=True):
76
+ st.subheader(body=f':red[{result["label"].item()}] Detected')
77
+ st.bar_chart(data=df, x='label', y='score')
78
+
79
+ with st.expander(label="X-ray image analyzed"):
80
+ st.image(st.session_state.image)
81
+
82
+ with st.expander(label="Model Details"):
83
+ st.markdown(body=vit_base_patch_16)
84
+
85
+ else:
86
+ tab_1, tab_2 = st.tabs(['Pneumonia', 'Coronavirus'])
87
+ with tab_1:
88
+ st.subheader('Pneumonia')
89
+ st.markdown(body=pneumonia)
90
+ with tab_2:
91
+ st.subheader('Coronavirus')
92
+ st.markdown(body=covid19)
93
+
94
+ # import gradio as gr
95
+ # from transformers import pipeline
96
+
97
+ # pipe = pipeline("image-classification", "sharren/vit-beta2-0.99")
98
+
99
+ # def image_classifier(image):
100
+ # outputs = pipe(image)
101
+ # results = {}
102
+ # for result in outputs:
103
+ # results[result['label']] = result['score']
104
+ # return results
105
+
106
+ # title = "Skin Cancer ViT Classifier"
107
+ # description = """
108
+ # This application serves to classify skin lesion images based on their skin cancer type. Trained using Vision Transformer (ViT), it has achieved a validation accuracy of 86%.
109
+ # """
110
+
111
+ # demo = gr.Interface(fn=image_classifier, inputs=gr.Image(type="pil"), outputs="label", title=title, description=description,
112
+ # examples = "./assets",
113
+ # theme = "gstaff/sketch")
114
+ # demo.launch(show_api=False)