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 ( "