Issue wth Session State

Hi Guys,

I wanted to create an application where users can select any model from the list and apply on images. For models I am using HuggingFace models.

Here is my config file. I used hydra to read the config file:

Vit_Models:
  vit-base-patch16-224-in21k: "google/vit-base-patch16-224-in21k"
  vit-base-patch32-224-in21k: "google/vit-base-patch32-224-in21k"
  vit-large-patch16-224-in21k: "google/vit-large-patch16-224-in21k"
  dinov2-base: "facebook/dinov2-base"
  dino-vitb8: "facebook/dino-vitb8"
  dino-vits8: "facebook/dino-vits8"

Here is the code

import streamlit as st
import hydra
from omegaconf import DictConfig

from PIL import Image
import requests
from transformers import ViTImageProcessor, ViTModel
# Title of the app
st.title('Classification of Tumor Infiltrating Lymphocytes in Whole Slide Images of 23 Types of Cancer using Hugging Face')


@hydra.main(version_base=None, config_path="./config", config_name="models")
def model_selection(cfg: DictConfig) -> str:
    Vit_Models = cfg.Vit_Models

    if "selected_model" not in st.session_state:
        st.session_state.selected_model = None


    # Create the select box with model options
    selected_model = st.selectbox(
        "Select any model from the following:",
        ["None"]  + list(Vit_Models.values())
    )

    if selected_model != "None":
        st.session_state.selected_model = selected_model


    # Display the selected model
    st.write(f"Selected model: {selected_model}")
    return selected_model

if __name__ == "__main__":
    selected_model = model_selection()

    url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
    image = Image.open(requests.get(url, stream=True).raw)

    
    processor = ViTImageProcessor.from_pretrained(selected_model)
    model = ViTModel.from_pretrained(selected_model)

    inputs = processor(images=image, return_tensors="pt")
    outputs = model(**inputs)
    print(outputs.last_hidden_state)
    st.write(outputs.last_hidden_state)

For some reason, the selected_model got None in the main. What is the reasons?
Thanks for your support.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.