File size: 6,758 Bytes
a61e7e9
185cbf5
 
 
 
 
 
 
158b021
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185cbf5
0fb9d8d
 
 
 
 
 
 
 
 
 
 
185cbf5
 
771ee97
185cbf5
 
62d9e2f
185cbf5
62d9e2f
185cbf5
 
 
 
 
 
 
62d9e2f
185cbf5
62d9e2f
185cbf5
62d9e2f
185cbf5
 
 
 
 
62d9e2f
 
185cbf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62d9e2f
 
185cbf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
                                                                           
import os
import subprocess
import time
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="AIzaSyAzv4VAm5hsr-Lek3ARywH6wcY6zEGVhSw")

# 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)