Upload 3 files
Browse files- app.py +163 -0
- brain_tumor_classifier.h5 +3 -0
- gen-lang-client-0651086807-7a7c02723fa8.json +13 -0
app.py
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import tensorflow as tf
|
5 |
+
from tensorflow.keras.preprocessing import image
|
6 |
+
import numpy as np
|
7 |
+
import tempfile
|
8 |
+
from reportlab.pdfgen import canvas
|
9 |
+
from vertexai.language_models import ChatModel
|
10 |
+
import vertexai
|
11 |
+
|
12 |
+
# Load environment variables from .env
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Set Google Cloud credentials from JSON key file
|
16 |
+
json_key_file = "gen-lang-client-0651086807-7a7c02723fa8.json" # Replace with your actual JSON key file name
|
17 |
+
|
18 |
+
# If the JSON key file is in the current working directory, set its path
|
19 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = json_key_file
|
20 |
+
|
21 |
+
# Initialize Vertex AI
|
22 |
+
project = "gen-lang-client-0651086807" # Replace with your Vertex AI project ID
|
23 |
+
location = "us-central1" # Replace with your Vertex AI location
|
24 |
+
vertexai.init(project=project, location=location)
|
25 |
+
|
26 |
+
# Load the Vertex AI ChatModel
|
27 |
+
chat_model = ChatModel.from_pretrained("chat-bison@002")
|
28 |
+
|
29 |
+
# Class labels for brain tumor types
|
30 |
+
class_labels = ['glioma', 'meningioma', 'notumor', 'pituitary']
|
31 |
+
|
32 |
+
# Function to preprocess image for the model
|
33 |
+
def preprocess_image(img):
|
34 |
+
img = image.load_img(img, target_size=(150, 150))
|
35 |
+
img_array = image.img_to_array(img)
|
36 |
+
img_array = np.expand_dims(img_array, axis=0)
|
37 |
+
img_array /= 255.0 # Normalize to [0, 1]
|
38 |
+
return img_array
|
39 |
+
|
40 |
+
# Function to load the CNN model
|
41 |
+
@st.cache(allow_output_mutation=True)
|
42 |
+
def load_model():
|
43 |
+
model_path = 'brain_tumor_classifier.h5' # Replace with your actual model path
|
44 |
+
model = tf.keras.models.load_model(model_path)
|
45 |
+
return model
|
46 |
+
|
47 |
+
# Function to predict brain tumor type and generate medical advice
|
48 |
+
def predict_and_generate_text(uploaded_file):
|
49 |
+
try:
|
50 |
+
if uploaded_file is None:
|
51 |
+
raise ValueError("No image file uploaded.")
|
52 |
+
|
53 |
+
# Load the model
|
54 |
+
model = load_model()
|
55 |
+
|
56 |
+
# Preprocess uploaded image
|
57 |
+
processed_img = preprocess_image(uploaded_file)
|
58 |
+
|
59 |
+
# Predict using the model
|
60 |
+
prediction = model.predict(processed_img)
|
61 |
+
predicted_class_index = np.argmax(prediction)
|
62 |
+
predicted_class = class_labels[predicted_class_index]
|
63 |
+
|
64 |
+
# Debugging: Print prediction details
|
65 |
+
st.write(f"Predicted class: {predicted_class}")
|
66 |
+
st.write(f"Prediction probabilities: {prediction}")
|
67 |
+
|
68 |
+
# Generate medical advice only for specific tumor classes
|
69 |
+
if predicted_class in ['glioma', 'meningioma', 'pituitary']:
|
70 |
+
# Initialize Vertex AI ChatModel
|
71 |
+
chat_session = chat_model.start_chat(context="")
|
72 |
+
|
73 |
+
# Constructing the prompt based on the predicted class
|
74 |
+
prompt = f"The patient has been diagnosed with a {predicted_class} brain tumor. Provide detailed medical advice including:"
|
75 |
+
|
76 |
+
if predicted_class == 'glioma':
|
77 |
+
prompt += (
|
78 |
+
" 1. Medical Condition: You have a glioma located in the frontal lobe.\n"
|
79 |
+
" 2. Medication Recommendations: Suggest suitable medications including chemotherapy drugs like Temozolomide, steroids to reduce brain swelling.\n"
|
80 |
+
" 3. Treatment Guidance: Recommend necessary treatments such as surgery to remove the tumor, followed by radiation therapy.\n"
|
81 |
+
" 4. Lifestyle Changes: Suggest lifestyle modifications to manage symptoms or improve overall health, such as regular exercise and a balanced diet.\n"
|
82 |
+
" 5. Monitoring and Specialist Referrals: Recommend ongoing monitoring by neurologists and oncologists for further evaluation and treatment."
|
83 |
+
)
|
84 |
+
|
85 |
+
elif predicted_class == 'meningioma':
|
86 |
+
prompt += (
|
87 |
+
" 1. Medical Condition: You have a meningioma, typically located in the meninges surrounding the brain.\n"
|
88 |
+
" 2. Medication Recommendations: Suggest medications to control symptoms, possibly including anticonvulsants and corticosteroids.\n"
|
89 |
+
" 3. Treatment Guidance: Recommend surgical removal of the tumor, with radiation therapy considered if complete removal isn't feasible.\n"
|
90 |
+
" 4. Lifestyle Changes: Advise on lifestyle adjustments such as stress reduction techniques and adequate sleep.\n"
|
91 |
+
" 5. Monitoring and Specialist Referrals: Recommend regular follow-up with neurosurgeons and neurologists for monitoring and management."
|
92 |
+
)
|
93 |
+
|
94 |
+
elif predicted_class == 'pituitary':
|
95 |
+
prompt += (
|
96 |
+
" 1. Medical Condition: You have a pituitary tumor affecting the pituitary gland.\n"
|
97 |
+
" 2. Medication Recommendations: Suggest hormone replacement therapy to manage hormone imbalances caused by the tumor.\n"
|
98 |
+
" 3. Treatment Guidance: Recommend surgical intervention to remove the tumor, or radiation therapy depending on the tumor's size and location.\n"
|
99 |
+
" 4. Lifestyle Changes: Recommend dietary adjustments and regular physical activity to support overall health.\n"
|
100 |
+
" 5. Monitoring and Specialist Referrals: Refer to endocrinologists and neurosurgeons for specialized care and ongoing monitoring."
|
101 |
+
)
|
102 |
+
|
103 |
+
# Additional prompts for all tumor types
|
104 |
+
prompt += (
|
105 |
+
" 6. Emotional Support: Emphasize the importance of emotional support and counseling throughout the treatment process.\n"
|
106 |
+
" 7. Patient Education: Educate the patient about the tumor type, its potential complications, and treatment options.\n"
|
107 |
+
" 8. Family Involvement: Encourage involvement of family members in decision-making and support.\n"
|
108 |
+
" 9. Research and Clinical Trials: Discuss opportunities for participation in clinical trials for new treatments.\n"
|
109 |
+
"10. Rehabilitation: Outline potential rehabilitation programs post-treatment to aid recovery and improve quality of life."
|
110 |
+
)
|
111 |
+
|
112 |
+
response = chat_session.send_message(prompt)
|
113 |
+
return response.text
|
114 |
+
else:
|
115 |
+
return "No specific medical advice available for tumors classified as 'notumor'."
|
116 |
+
|
117 |
+
except ValueError as ve:
|
118 |
+
st.error(f"Error: {ve}")
|
119 |
+
except Exception as e:
|
120 |
+
st.error(f"An error occurred during prediction: {e}")
|
121 |
+
|
122 |
+
# Function to generate PDF report
|
123 |
+
def generate_pdf(description):
|
124 |
+
if description is None:
|
125 |
+
return None
|
126 |
+
|
127 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
|
128 |
+
c = canvas.Canvas(tmpfile.name)
|
129 |
+
c.setFont("Helvetica", 12)
|
130 |
+
text_lines = description.split('\n') # Ensure description is not None before splitting
|
131 |
+
y = 750
|
132 |
+
for line in text_lines:
|
133 |
+
c.drawString(100, y, line)
|
134 |
+
y -= 20
|
135 |
+
c.save()
|
136 |
+
tmpfile.close()
|
137 |
+
return tmpfile.name
|
138 |
+
|
139 |
+
# Streamlit app
|
140 |
+
def main():
|
141 |
+
st.title('Brain Tumor Classifier and Treatment Advisor')
|
142 |
+
st.markdown('Upload a brain MRI image for analysis.')
|
143 |
+
|
144 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"]) # Allow jpg, png, and jpeg files
|
145 |
+
if uploaded_file is not None:
|
146 |
+
st.image(uploaded_file, caption='Uploaded MRI Image', use_column_width=True)
|
147 |
+
if st.button('Predict and Recommend'):
|
148 |
+
try:
|
149 |
+
with st.spinner('Analyzing the image...'):
|
150 |
+
output_text = predict_and_generate_text(uploaded_file)
|
151 |
+
st.success("Medical Advice:")
|
152 |
+
st.write(output_text)
|
153 |
+
|
154 |
+
# Generate and download PDF report
|
155 |
+
pdf_filename = generate_pdf(output_text)
|
156 |
+
with open(pdf_filename, "rb") as pdf_file:
|
157 |
+
st.download_button(label="Download PDF", data=pdf_file, file_name="medical_advice.pdf", mime="application/pdf")
|
158 |
+
|
159 |
+
except Exception as e:
|
160 |
+
st.error(f"An error occurred: {e}")
|
161 |
+
|
162 |
+
if __name__ == "__main__":
|
163 |
+
main()
|
brain_tumor_classifier.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:28ab14dd82d2d60586378868305ed17a358388a0426219d6c620e7d3f6b3d350
|
3 |
+
size 72870296
|
gen-lang-client-0651086807-7a7c02723fa8.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"type": "service_account",
|
3 |
+
"project_id": "gen-lang-client-0651086807",
|
4 |
+
"private_key_id": "7a7c02723fa878dce92e58ce097bf5ed7c82bb21",
|
5 |
+
"private_key": "-----BEGIN PRIVATE KE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q+1VDReg8PenHbQBqv/6S1iidXxXvKvEsaD4pIHS8gIP\n0uWyG8eLaQKBgQDN4RitS5W13171wMTsUTL73mWcvVJKI74eZxX4l8torZIGtA4Q\nmlvZoQ4AywiFTGGNUFAfhT+k7RfGiVIMdPGf6Gw/NHejZRBG4MML+rBupDXXyxxQ\n1ty3F3qZMu3KKKa5gaQfR+QbQ97O+isdXQgb2F70wC8qfL0udDq/UNk1oQKBgGqI\nxW76Ya/RVgC/dP2ID5gR5XS6BTYufuJtxeTCLV1NvcW+nvsF3riGwRi78/OjtWNk\n3pnxgz35S+45xnt14CZneWhRjrmWThp2x3QijFEPkCcz6hXLNjU8VBS1HZYDGOMY\n31FuCmTlB8zrkkiGPTU/vBkSM/GcsGLTgzFyHnLRAoGBAO0tNoe79N2ovYrXkNjF\nxolQNa7JOz4tFBkZd/1e/hPGj3BtOTEkg/VbDJv0cp0a3ZrdTIlg3+FxWuzvrnN7\nlbw07KbA8yP9cv5R6Gqv3OizcWU3U+ZBWm9lABN+3LtJHUrXzaIisXRu+dDJ7QLH\nTL9YYij4O4mu3PRyHUGe+xjA\n-----END PRIVATE KEY-----\n",
|
6 |
+
"client_email": "sadik-27@gen-lang-client-0651086807.iam.gserviceaccount.com",
|
7 |
+
"client_id": "100824630135105586057",
|
8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/sadik-27%40gen-lang-client-0651086807.iam.gserviceaccount.com",
|
12 |
+
"universe_domain": "googleapis.com"
|
13 |
+
}
|