File size: 1,004 Bytes
c46c672
07952a9
 
22a9bc1
 
07952a9
 
 
 
c46c672
3edf187
07952a9
 
 
 
 
 
 
 
 
 
 
3edf187
22a9bc1
e36a241
60c94e7
3edf187
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
import gradio as gr
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
import torch
from PIL import Image

# Load the model and feature extractor once during initialization
model_name = "amjadfqs/finalProject"
model = AutoModelForImageClassification.from_pretrained(model_name)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)

def predict(image):
    # Preprocess the image
    inputs = feature_extractor(images=image, return_tensors="pt")
    # Make prediction
    with torch.no_grad():
        outputs = model(**inputs)
    logits = outputs.logits
    # Get the predicted class
    predicted_class = logits.argmax(-1).item()
    # You may need to adjust the following line based on your class labels
    class_names = ["class1", "class2", "class3", "class4"]
    return predicted_class

# Set up the Gradio interface
image_cp = gr.Image(type="pil", label='Brain')
interface = gr.Interface(fn=predict, inputs=image_cp, outputs="text")
interface.launch()