import streamlit as st
import streamlit.components.v1 as components
import pandas as pd
from my_model.tabs.run_inference import InferenceRunner
from my_model.tabs.results import run_demo
from my_model.tabs.home import run_home
from my_model.state_manager import StateManager
from my_model.tabs.dataset_analysis import run_dataset_analyzer
from my_model.tabs.model_arch import run_model_arch
class UIManager():
"""
Manages the user interface for the Streamlit application.
This class handles the creation and navigation of various tabs in the Streamlit app. It provides methods to add new
tabs, display the sidebar, and render the content of the selected tab.
Attributes:
tabs (dict): A dictionary mapping tab names to their corresponding display functions.
"""
def __init__(self)-> None:
"""
Initializes the UIManager with predefined tabs.
This method sets up the initial tabs for the application and initializes the state manager.
"""
self.tabs = {
"Home": self.display_home,
"Dataset Analysis": self.display_dataset_analysis,
"Model Architecture": self.display_model_arch,
"Results": self.display_results,
"Run Inference": self.display_run_inference,
"Dissertation Report": self.display_dissertation_report,
"Code": self.display_code
}
state_manager = StateManager()
state_manager.initialize_state()
def add_tab(self, tab_name: str, display_function: callable) -> None:
"""
Adds a new tab to the UI.
Args:
tab_name (str): The name of the new tab.
display_function (callable): The function to be called when the tab is selected.
Returns:
None
"""
self.tabs[tab_name] = display_function
def display_sidebar(self) -> str:
"""
Displays the sidebar for navigation.
This method creates a sidebar with navigation options and returns the user's selection.
Returns:
str: The name of the selected tab.
"""
st.sidebar.image("Files/logo.jpg")
st.sidebar.title("Navigation")
selection = st.sidebar.radio("Go to", list(self.tabs.keys()), disabled=st.session_state['loading_in_progress'])
st.sidebar.image("Files/mm.jpeg", use_column_width=True)
st.sidebar.markdown(
"""
""",
unsafe_allow_html=True
)
return selection
def display_selected_page(self, selection: str) -> None:
"""
Displays the selected page based on the user's choice.
Args:
selection (str): The name of the selected tab.
Returns:
None
"""
if selection in self.tabs:
self.tabs[selection]()
def display_home(self) -> None:
"""
Displays the Home page of the application.
Returns:
None
"""
st.markdown("Multimodal Learning for Visual Question Answering using World Knowledge
", unsafe_allow_html=True)
st.markdown("Knowledge-Based Visual Question Answering - KBVQA
", unsafe_allow_html=True)
run_home()
def display_dataset_analysis(self) -> None:
"""
Displays the Dataset Analysis page.
This page provides an overview of various KB-VQA datasets and analyses of the OK-VQA dataset.
Returns:
None
"""
st.title("Dataset Analysis")
st.write("""This page shows an overview of some of the KB-VQA datasets, and various analysis of
the [OK-VQA Dataset](https://okvqa.allenai.org/) that this KB-VQA model was fine-tuned
and evaluated on.""")
run_dataset_analyzer()
def display_results(self) -> None:
"""
Displays the Evaluation Results page.
This page demonstrates the model evaluation results and analyses in an interactive way.
Returns:
None
"""
st.title("Evaluation Results & Analyses")
st.write("This page demonstrates the model evaluation results and analyses in an interactive way.")
st.write("\n")
run_demo()
def display_model_arch(self) -> None:
"""
Displays the Model Architecture page.
This page provides detailed information about the model architecture.
Returns:
None
"""
st.title("Model Architecture")
st.write("This page shows the detailed Model Architecture.")
st.write("\n")
run_model_arch()
def display_run_inference(self) -> None:
"""
Displays the Run Inference page.
This page allows users to run inference using the fine-tuned model on the OK-VQA dataset.
Returns:
None
"""
st.title("Run Inference")
st.write("""Please note that this is not a general purpose model, it is specifically trained on
[OK-VQA Dataset](https://okvqa.allenai.org/) and desgined to give short and direct answers to the
given questions about the given image.\n""")
st.write("""**Note:** To load and run this model, the space must be configured to run on a GPU.
If the space is not set to run on a GPU, please contact me.""")
inference_runner = InferenceRunner()
inference_runner.run_inference()
def display_dissertation_report(self) -> None:
"""
Displays the Dissertation Report page.
This page provides a link to download the dissertation report PDF.
Returns:
None
"""
st.title("Dissertation Report")
st.write("Click the link below to view the PDF.")
# Error handling for file access should be considered here
st.download_button(
label="Download PDF",
data=open("Files/Dissertation Report.pdf", "rb"),
file_name="KB-VQA Dissertation Report for Mohammed Bin Ali Alhaj.pdf",
mime="application/octet-stream"
)
def display_code(self) -> None:
"""
Displays the Code page with a link to the project's code repository.
Returns:
None
"""
st.title("Code")
st.markdown("You can view the code for this project on HuggingFace repository.")
st.markdown("[View Code](https://huggingface.co/spaces/m7mdal7aj/KB-VQA/tree/main)", unsafe_allow_html=True)
def display_placeholder(self) -> None:
"""
Displays a placeholder for future content.
Returns:
None
"""
st.title("Stay Tuned")
st.write("This is a Place Holder until the contents are uploaded.")