Spaces:
No application file
No application file
#Code was Designed and Developed by 'SKAV TECH' Company | |
import os | |
import tensorflow as tf | |
from tensorflow.keras.preprocessing.image import load_img, img_to_array | |
import numpy as np | |
import gradio as gr | |
# Force TensorFlow to use CPU | |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" | |
# Load the pre-trained model | |
model = tf.keras.models.load_model("maheshbabu.h5") | |
# Define class labels | |
classes = ["Normal", "Cancerous"] | |
# Prediction function | |
def predict(images): | |
results = [] | |
cancerous_count = 0 | |
for image in images: | |
try: | |
# Load and preprocess the image | |
img = load_img(image, target_size=(224, 224)) # Resize image | |
img_array = img_to_array(img) / 255.0 # Normalize pixel values | |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension | |
# Perform prediction | |
predictions = model.predict(img_array) | |
class_idx = np.argmax(predictions[0]) # Get index of highest probability | |
confidence = predictions[0][class_idx] # Get confidence score | |
result = f"{classes[class_idx]} ({confidence:.2f})" | |
results.append(result) | |
if classes[class_idx] == "Cancerous": | |
cancerous_count += 1 | |
except Exception as e: | |
results.append(f"Error processing image: {str(e)}") | |
# Generate final summary | |
if cancerous_count > 0: | |
summary = f"Warning: {cancerous_count} out of {len(images)} samples are Cancerous. Please consult a doctor." | |
else: | |
summary = "All samples are Normal. No signs of cancer detected." | |
return results, summary | |
# Set up the Gradio interface | |
interface = gr.Interface( | |
fn=predict, | |
inputs=gr.Image(type="filepath", label="Upload Blood Cell Images", tool=None, shape=None, source="upload", multiple=True), # Allow multiple images | |
outputs=[ | |
gr.JSON(label="Detailed Results"), | |
gr.Textbox(label="Final Summary") | |
], | |
title="Enhanced Blood Cancer Detection", | |
description=( | |
"Upload 5-10 blood cell images to detect whether they are Normal or Cancerous. " | |
"The application uses a deep learning model to analyze each sample. " | |
"[Learn more about early cancer detection](https://www.cancer.org)." | |
), | |
live=True, | |
theme="compact" | |
) | |
if __name__ == "__main__": | |
interface.launch(server_port=7860, server_name="0.0.0.0", share=True) | |