File size: 7,562 Bytes
38167d4
3b2483d
 
f0f4b86
2c910b8
86d5177
10892df
303e258
5bf37af
38167d4
ba3027e
d9a178d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6bb045
38167d4
 
 
7665ccf
1395bd5
38167d4
 
1395bd5
38167d4
 
10892df
 
 
8a5ab3e
03f10c9
d9a178d
 
 
 
 
 
 
 
 
 
03f10c9
38167d4
 
8a5ab3e
d9a178d
 
 
 
 
 
 
 
 
 
951484b
810a2b0
f28eb9c
5a3f0a1
f319617
 
 
cb8ebbd
f319617
 
 
 
1266228
38167d4
8a5ab3e
38167d4
d9a178d
 
 
 
 
 
 
 
 
 
8a5ab3e
38167d4
 
 
8a5ab3e
d9a178d
 
 
 
 
 
 
fac1f70
 
8a5ab3e
86d5177
8a5ab3e
d9a178d
 
 
 
 
 
 
 
 
8a5ab3e
71cdd36
f319617
 
 
edf48ac
38167d4
8a5ab3e
d9a178d
 
 
 
 
 
 
 
 
e0f05ce
d286ccb
 
78040a5
2c910b8
 
38167d4
d9a178d
 
 
 
 
 
 
 
 
5bf37af
 
 
 
 
 
 
8a5ab3e
d9a178d
 
 
 
 
 
 
 
 
e0f05ce
cb8b3fe
86d5177
 
 
 
68c35ff
9c15077
 
f0f4b86
 
38167d4
8a5ab3e
d9a178d
 
 
 
 
 
 
 
 
e0f05ce
38167d4
 
ba3027e
38167d4
 
 
df685db
38167d4
 
 
8a5ab3e
d9a178d
 
 
 
 
 
 
8a5ab3e
38167d4
fac1f70
125b496
38167d4
8a5ab3e
d9a178d
 
 
 
 
 
 
8a5ab3e
38167d4
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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(
                                """
                                <div style="text-align: center;">
                                    <a href="https://www.linkedin.com/in/m7mdal7aj" style="font-weight: bold; text-decoration: none;">Mohammed Bin Ali AlHaj</a>
                                </div>
                                """,
                                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("<h1 style='text-align: center;'>Multimodal Learning for Visual Question Answering using World Knowledge</h1>", unsafe_allow_html=True)
        st.markdown("<h2 style='text-align: center;'>Knowledge-Based Visual Question Answering - KBVQA</h2>", 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.")