File size: 1,396 Bytes
215c17e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf01ac9
215c17e
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoFeatureExtractor, AutoModelForImageClassification

# Load the feature extractor and model directly
feature_extractor = AutoFeatureExtractor.from_pretrained("Devarshi/Brain_Tumor_Classification")
model = AutoModelForImageClassification.from_pretrained("Devarshi/Brain_Tumor_Classification")

# Define the prediction function using the loaded model
def classify_image(image):
    # Preprocess the image and obtain features
    inputs = feature_extractor(images=image, return_tensors="pt")
    # Make prediction using the model
    outputs = model(**inputs)
    logits = outputs.logits
    # Get the predicted class and confidence of the prediction
    predicted_class = logits.argmax(dim=1).item()
    confidence = logits.softmax(dim=1).max().item()

    # Map the predicted class to the correct names
    class_names = ["glioma_tumor", "meningioma_tumor", "no_tumor", "pituitary_tumor"]
    predicted_class_text = class_names[predicted_class]

    return {"prediction": predicted_class_text, "confidence": confidence}

# Define the Gradio interface
iface = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(),
    outputs="json",
    title="Brain Tumor Image Classification",
    description="This app classifies images of brain tumors into different classes.",
)

# Launch the Gradio interface
if __name__ == "__main__":
    iface.launch()