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="sharren/vit-beta2-0.99") 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) # import gradio as gr # from transformers import pipeline # pipe = pipeline("image-classification", "sharren/vit-beta2-0.99") # def image_classifier(image): # outputs = pipe(image) # results = {} # for result in outputs: # results[result['label']] = result['score'] # return results # title = "Skin Cancer ViT Classifier" # description = """ # 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%. # """ # demo = gr.Interface(fn=image_classifier, inputs=gr.Image(type="pil"), outputs="label", title=title, description=description, # examples = "./assets", # theme = "gstaff/sketch") # demo.launch(show_api=False)