File size: 2,442 Bytes
6e6f109
 
 
 
 
 
 
 
 
 
 
869580a
6e6f109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#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("blood_cancer_model.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)