import gradio as gr from PIL import Image from transformers import ViTImageProcessor, ViTForImageClassification processor = ViTImageProcessor.from_pretrained('Rageshhf/fine-tuned-model') id2label = {0: 'Mild_Demented', 1: 'Moderate_Demented', 2: 'Non_Demented', 3: 'Very_Mild_Demented'} label2id = {'Mild_Demented': 0, 'Moderate_Demented': 1, 'Non_Demented': 2, 'Very_Mild_Demented': 3} model = ViTForImageClassification.from_pretrained( 'Rageshhf/fine-tuned-model', num_labels=4, id2label=id2label, label2id=label2id, ignore_mismatched_sizes=True) title = "Medi- classifier" description = """Trained to classify disease based on image data.""" def predict(image): inputs = processor(images=image, return_tensors="pt") outputs = model(**inputs) logits = outputs.logits # model predicts one of the 1000 ImageNet classes predicted_class_idx = logits.argmax(-1).item() return(model.config.id2label[predicted_class_idx]) demo = gr.Interface(fn=predict, inputs="image", outputs="text", title=title, examples=["examples/image_1.png", "examples/image_2.png", "examples/image_3.png"], description=description,).launch() # demo.launch(debug=True)