Spaces:
Sleeping
Sleeping
File size: 5,663 Bytes
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 |
!curl https://ollama.ai/install.sh | sh
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
# Set the environment variable for the Ollama model
OLLAMA_MODEL = 'dolphin-mistral:v2.8'
os.environ['OLLAMA_MODEL'] = OLLAMA_MODEL
# Start Ollama as a background process
command = "nohup ollama serve &"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(5) # Wait for the server to start
# Function to interact with the Ollama model
def query_model(input_text):
response = subprocess.run(
["ollama", "run", OLLAMA_MODEL, input_text],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
return response.stdout if response.returncode == 0 else response.stderr
# 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) |