Spaces:
Sleeping
Sleeping
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 ( | |
"<h3 style='text-align: center;'> 🎗️ Welcome to Our Breast Cancer System 🎗️ </h3>\n\n" | |
"Breast cancer is one of the most common causes of death among women worldwide.\n\n " | |
"Early detection plays a crucial role in reducing mortality rates.\n\n " | |
"This project includes two main components:\n\n" | |
"- **Ultrasound Image Classification**: \n\n We classify breast ultrasound images into three categories: normal, benign, and malignant. \n\n" | |
"The dataset consists of 780 ultrasound images collected in 2018 from 600 female patients, aged 25 to 75. \n\n " | |
" Each image is in PNG format with an average size of 500x500 pixels.\n\n\n" | |
"- **Breast Cancer Information Chatbot**: \n\n Our chatbot is designed to provide reliable information and answer questions about breast cancer, helping users to understand the disease better.\n\n\n" | |
"For additional assistance, you can interact with our chatbot or upload images for classification." | |
) | |
# Create the Gradio interface for both functionalities | |
chatbot_interface = gr.Interface( | |
fn=query_model, | |
inputs=gr.Textbox(label="Enter your question about breast cancer:", placeholder="e.g., What are the symptoms of breast cancer?", lines=2), | |
outputs=gr.Textbox(label="Response:", placeholder="Your answer will appear here..."), | |
title="Breast Cancer Chatbot 🎗️", | |
description="Ask your questions related to breast cancer. Our chatbot provides information and guidance based on your inquiries.", | |
) | |
breast_cancer_interface = gr.Interface( | |
fn=predict, | |
inputs=gr.Image(type="filepath", label="Upload an Image"), | |
outputs=[ | |
gr.Textbox(label="Prediction"), | |
gr.Image(label="Your Partner in Breast Health Awareness 🎗️", value=fixed_image_url) | |
], | |
title="Breast Cancer Detection", | |
description="Predicting Your Breast Health: Is it Benign, Malignant, or Normal?", | |
examples=examples, | |
) | |
# Create the information display as a separate Markdown element | |
info_markdown = gr.Markdown(show_info()) | |
# Combine interfaces into a themed Blocks app | |
with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.neutral)) as demo: | |
combined_interface = gr.TabbedInterface( | |
[info_markdown,chatbot_interface, breast_cancer_interface], | |
["Project Information","Breast Cancer Chatbot", "Breast Cancer Detection"] | |
) | |
# Launch the combined interface | |
if __name__ == "__main__": | |
demo.launch(debug=True) |