import os import gradio as gr import numpy as np import tensorflow as tf from tensorflow.keras.preprocessing.image import load_img, img_to_array import h5py import google.generativeai as genai genai.configure(api_key=os.getenv("GENAI_API_KEY")) # Set up generation configuration generation_config = { "temperature": 1, "top_p": 0.95, "top_k": 0, "max_output_tokens": 8192, } # Safety settings to filter harmful content safety_settings = [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, ] # Set system message for the model system_instruction = ( "You are Dr. Bot, a medical assistant specializing in breast cancer. " "Your role is to provide accurate information about breast cancer types, symptoms, " "screening guidelines, treatment options, and supportive resources. " "Offer compassionate support and respond to patient inquiries with empathy and evidence-based information." ) # Load Gemini model model = genai.GenerativeModel( model_name="gemini-1.5-pro-latest", generation_config=generation_config, system_instruction=system_instruction, safety_settings=safety_settings ) # Function to interact with the Gemini model def query_model(input_text): convo = model.start_chat(history=[{"role": "user", "parts": [input_text]}]) response = convo.send_message("YOUR_USER_INPUT") return convo.last.text f = h5py.File("best_model_2.h5", mode="r+") model_config_string = f.attrs.get("model_config") if model_config_string.find('"groups": 1,') != -1: model_config_string = model_config_string.replace('"groups": 1,', '') f.attrs.modify('model_config', model_config_string) f.flush() model_config_string = f.attrs.get("model_config") assert model_config_string.find('"groups": 1,') == -1 f.close() # Load the breast cancer detection model incept_model = tf.keras.models.load_model('best_model_2.h5') fixed_image_url = "breast-cancer-awareness-month-1200x834.jpg" # Example images and their descriptions examples = [ ["malignant.png", "Malignant X-ray image."], ["normal.png", "X-ray image indicating normal."], ["benign.png", "X-ray image showing no signs of benign."] ] IMAGE_SHAPE = (224, 224) classes = ['benign', 'malignant', 'normal'] # Function to prepare the image for prediction def prepare_image(file): img = load_img(file, target_size=IMAGE_SHAPE) img_array = img_to_array(img) img_array = np.expand_dims(img_array, axis=0) return tf.keras.applications.efficientnet.preprocess_input(img_array) # Prediction function for breast cancer detection def predict(file): if file is None: return "Please upload an image.", fixed_image_url img = prepare_image(file) res = incept_model.predict(img) pred_index = np.argmax(res) pred = classes[pred_index] # Specific advice for each prediction if pred == 'malignant': advice = "As a healthcare professional, I recommend immediate further evaluation. Malignant findings can indicate the presence of cancer. Please consult a specialist." elif pred == 'benign': advice = "The results show benign characteristics, which is a positive outcome. This means there are no cancerous cells. However, itβs essential to have regular follow-ups with your healthcare provider to ensure that there are no changes over time." else: # pred == 'normal' advice = "The results appear normal. Continue with regular check-ups and maintain a healthy lifestyle." return advice, fixed_image_url # Function to provide project information def show_info(): return ( "