File size: 3,273 Bytes
1f01f64
 
d778190
1f01f64
2383d45
 
1f01f64
d778190
1f01f64
 
 
 
 
 
 
 
 
 
 
2383d45
 
1f01f64
 
 
 
 
 
 
 
 
 
 
 
 
 
2383d45
 
1f01f64
 
 
 
 
d778190
 
 
1f01f64
 
 
 
 
 
 
d778190
1f01f64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# DEV reloads require a refresh on the browser.
# https://gist.github.com/brandon8863/3ee552870a066eba463d37f0b39a547a
import gradio as gr
import webpages.blip_image_captioning as blip_image_captioning 
import webpages.text_generation as text_generation 

import home as home

 
def routs():
    with gr.Column():
                # anchor = gr.HTML("<h1>🏠</h1>")
                anchor = gr.HTML("<br/><br/><br/><br/>")
                #
                # BUTTONS FOR PAGE NAVIGATION
                #
                with gr.Column() as result:
                    gr.Button("🏠 Home", link="/?page=home")
                    gr.Button("πŸ‘€ Find out what in the photo", link="/?page=blip_image_captioning")
                    gr.Button("πŸ“ Text Generation", link="/?page=text_generation")

 
def handePages(local_state):
     with gr.Column(scale=30):
                #
                # SIMPLE PAGE ROUTING HERE
                #
                if (
                    local_state == None
                ):
                    return home.get_landing_page(local_state), local_state
                elif local_state  == "home":
                    return home.get(), local_state
                elif local_state  == "blip_image_captioning":
                    return blip_image_captioning.get(local_state), local_state
                elif local_state  == "text_generation":
                    return text_generation.get(local_state), local_state
                else:
                    return (
                        home.get_not_found_page(local_state),
                        local_state,
                    )



# =======================================================================================================
# APP_SHELL - for multiple pages
#
with gr.Blocks(fill_height=True   , css=".contain { display: flex !important; flex-direction: column !important; }"
    "#component-0, #component-3, #component-10, #component-8  { height: 100% !important; }"
    "#chatbot { flex-grow: 1 !important; overflow: auto !important;}"
    "#col { height: calc(100vh - 112px - 16px) !important; }") as demo:

    def init_state(request: gr.Request):
        #
        # PULL URL PARAMS HERE
        #
        page =  request.query_params.get("page")
        print(f"** page: {page}")
        if (
                    page == None
                ):
            page = "home" 
        # result["page"] = request.query_params.get("page")
        return page  # this result populates "state"

    state = gr.State()

    #
    # POPULATE user "state" with request data
    #
    demo.load(
        fn=init_state,
        inputs=None,
        outputs=state,
        queue=True,
        show_progress=False,
    )

    content = gr.HTML("...")

    @gr.render(inputs=[state], triggers=[state.change])
    def page_content(local_state):
        print(f"** local_state: {local_state}")
        with gr.Row(variant="panel") as result:
            routs()
            handePages(local_state)
            
    #
    # HACK: Would be nice to delay rendering until state is populated
    #
    def page_content_update(local_state):
        return gr.HTML("...",visible=False )

    state.change(fn=page_content_update, inputs=state, outputs=content)


demo.launch()