Hammad712 commited on
Commit
bd5d744
1 Parent(s): f3c0ffa

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +117 -0
streamlit_app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import torch
4
+ from transformers import ViTForImageClassification, ViTImageProcessor
5
+ import logging
6
+ import base64
7
+ from io import BytesIO
8
+
9
+ # Setup logging
10
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11
+
12
+ # Load the model and feature extractor from Hugging Face
13
+ repository_id = "EnDevSols/brainmri-vit-model"
14
+ model = ViTForImageClassification.from_pretrained(repository_id)
15
+ feature_extractor = ViTImageProcessor.from_pretrained(repository_id)
16
+
17
+ # Function to perform inference
18
+ def predict(image):
19
+ # Load and preprocess the image
20
+ image = image.convert("RGB")
21
+ inputs = feature_extractor(images=image, return_tensors="pt")
22
+
23
+ # Move the inputs to the appropriate device
24
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
25
+ model.to(device)
26
+ inputs = {k: v.to(device) for k, v in inputs.items()}
27
+
28
+ # Perform inference
29
+ with torch.no_grad():
30
+ outputs = model(**inputs)
31
+
32
+ # Get the predicted label
33
+ logits = outputs.logits
34
+ predicted_label = logits.argmax(-1).item()
35
+
36
+ # Map the label to "No" or "Yes"
37
+ label_map = {0: "No", 1: "Yes"}
38
+ diagnosis = label_map[predicted_label]
39
+
40
+ # Return a complete statement
41
+ if diagnosis == "Yes":
42
+ return "The diagnosis indicates that you have a brain tumor."
43
+ else:
44
+ return "The diagnosis indicates that you do not have a brain tumor."
45
+
46
+ # Custom CSS
47
+ def set_css(style):
48
+ st.markdown(f"<style>{style}</style>", unsafe_allow_html=True)
49
+
50
+ # Combined dark mode styles
51
+ combined_css = """
52
+ .main, .sidebar .sidebar-content { background-color: #1c1c1c; color: #f0f2f6; }
53
+ .block-container { padding: 1rem 2rem; background-color: #333; border-radius: 10px; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.5); }
54
+ .stButton>button, .stDownloadButton>button { background: linear-gradient(135deg, #ff7e5f, #feb47b); color: white; border: none; padding: 10px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 5px; }
55
+ .stSpinner { color: #4CAF50; }
56
+ .title {
57
+ font-size: 3rem;
58
+ font-weight: bold;
59
+ display: flex;
60
+ align-items: center;
61
+ justify-content: center;
62
+ }
63
+ .colorful-text {
64
+ background: -webkit-linear-gradient(135deg, #ff7e5f, #feb47b);
65
+ -webkit-background-clip: text;
66
+ -webkit-text-fill-color: transparent;
67
+ }
68
+ .black-white-text {
69
+ color: black;
70
+ }
71
+ .small-input .stTextInput>div>input {
72
+ height: 2rem;
73
+ font-size: 0.9rem;
74
+ }
75
+ .small-file-uploader .stFileUploader>div>div {
76
+ height: 2rem;
77
+ font-size: 0.9rem;
78
+ }
79
+ .custom-text {
80
+ font-size: 1.2rem;
81
+ color: #feb47b;
82
+ text-align: center;
83
+ margin-top: -20px;
84
+ margin-bottom: 20px;
85
+ }
86
+ """
87
+
88
+ # Streamlit application
89
+ st.set_page_config(layout="wide")
90
+
91
+ st.markdown(f"<style>{combined_css}</style>", unsafe_allow_html=True)
92
+
93
+ st.markdown('<div class="title"><span class="colorful-text">Brain MRI</span> <span class="black-white-text">Tumor Detection</span></div>', unsafe_allow_html=True)
94
+ st.markdown('<div class="custom-text">Upload an MRI image to detect brain tumor</div>', unsafe_allow_html=True)
95
+
96
+ # Uploading image
97
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
98
+
99
+ if uploaded_file is not None:
100
+ image = Image.open(uploaded_file)
101
+
102
+ # Resize the image for display
103
+ resized_image = image.resize((150, 150))
104
+
105
+ # Convert image to base64
106
+ buffered = BytesIO()
107
+ resized_image.save(buffered, format="JPEG")
108
+ img_str = base64.b64encode(buffered.getvalue()).decode()
109
+
110
+ # Display the image in the center
111
+ st.markdown(f"<div style='text-align: center;'><img src='data:image/jpeg;base64,{img_str}' alt='Uploaded Image' width='300'></div>", unsafe_allow_html=True)
112
+
113
+ st.write("")
114
+ st.write("Result...")
115
+
116
+ diagnosis = predict(image)
117
+ st.write(diagnosis)