# app.py import streamlit as st from PIL import Image import torch from torchvision import transforms from transformers import AutoModelForImageClassification, AutoTokenizer import os from transformers import AutoModel model = AutoModel.from_pretrained("rameye/1") # Define the class labels class_labels = { 0: "Healthy eyes", 1: "Mild DR", 2: "Moderate DR", 3: "NPDR", 4: "Severe DR", 5: "Glaucoma", 6: "AMD", 7: "Hypertensive retinopathy" } # Load the model and tokenizer from the Hugging Face model hub # model = AutoModelForImageClassification.from_pretrained(model_name) # tokenizer = AutoTokenizer.from_pretrained(model_name) # Load model directly # Load the model with the access token passed as an environment variable # model_name = "your_model_name_in_hugging_face" # model = AutoModelForImageClassification.from_pretrained(model_name) model = AutoModel.from_pretrained("rameye/1") tokenizer = AutoTokenizer.from_pretrained(model) # Define image transformations transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) # Function to make predictions def predict(image): image = transform(image).unsqueeze(0) with torch.no_grad(): outputs = model(image) predicted_class = torch.argmax(outputs.logits).item() return class_labels[predicted_class] # Streamlit UI def main(): st.title("Image Classifier") st.write("Upload an image and I'll classify it.") uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: image = Image.open(uploaded_file) st.image(image, caption="Uploaded Image", use_column_width=True) if st.button("Classify"): with st.spinner('Classifying...'): prediction = predict(image) st.success(f"Prediction: {prediction}") if __name__ == "__main__": main()