File size: 6,801 Bytes
dc7c3c1
3a62692
1510f49
 
 
 
dc7c3c1
fc2712b
dc7c3c1
 
fc2712b
dc7c3c1
 
1510f49
dc7c3c1
 
 
 
 
 
 
 
 
 
 
1510f49
3a62692
1510f49
dc7c3c1
 
1510f49
 
dc7c3c1
 
3a62692
 
 
 
 
1510f49
3a62692
 
 
 
 
 
1510f49
3a62692
 
 
 
 
 
 
1510f49
 
 
 
 
3a62692
 
1510f49
 
 
 
 
 
3a62692
 
 
1510f49
3a62692
 
 
 
 
1510f49
 
 
 
 
3a62692
 
1510f49
 
 
 
 
 
3a62692
 
 
1510f49
3a62692
 
dc7c3c1
 
1510f49
 
 
 
dc7c3c1
 
 
1510f49
 
 
 
 
 
 
 
dc7c3c1
 
 
 
 
1510f49
 
dc7c3c1
 
1510f49
dc7c3c1
1510f49
dc7c3c1
 
1510f49
dc7c3c1
 
 
 
1510f49
 
 
 
 
 
dc7c3c1
 
 
 
 
1510f49
 
dc7c3c1
1510f49
 
 
dc7c3c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1510f49
dc7c3c1
 
1510f49
dc7c3c1
 
1510f49
dc7c3c1
 
 
 
 
 
1510f49
 
 
 
 
 
dc7c3c1
 
 
 
 
 
 
 
3a62692
 
 
 
dc7c3c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1510f49
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
import os
import sys

import gradio as gr

sys.path.append("../CTM/")
from ctm.ctms.ctm_base import BaseConsciousnessTuringMachine

ctm = BaseConsciousnessTuringMachine()
ctm.add_supervisor("gpt4_supervisor")

DEPLOYED = os.getenv("DEPLOYED", "true").lower() == "true"


def introduction():
    with gr.Column(scale=2):
        gr.Image(
            "images/sotopia.jpg", elem_id="banner-image", show_label=False
        )
    with gr.Column(scale=5):
        gr.Markdown(
            """Consciousness Turing Machine Demo
            """
        )


def add_processor(processor_name, display_name, state):
    print("add processor ", processor_name)
    ctm.add_processor(processor_name)
    print(len(ctm.processor_list))
    return display_name + " (added)"


def processor_tab():
    # Categorized model names
    text_processors = [
        "gpt4_text_emotion_processor",
        "gpt4_text_summary_processor",
        "gpt4_speaker_intent_processor",
        "roberta_text_sentiment_processor",
    ]
    vision_processors = [
        "gpt4v_cloth_fashion_processor",
        "gpt4v_face_emotion_processor",
        "gpt4v_ocr_processor",
        "gpt4v_posture",
        "gpt4v_scene_location_processor",
    ]

    with gr.Blocks():
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### Text Processors")
                for model_name in text_processors:
                    display_name = (
                        model_name.replace("processor", "")
                        .replace("_", " ")
                        .title()
                    )

                    button = gr.Button(display_name)
                    processor_name = gr.Textbox(
                        value=model_name, visible=False
                    )
                    display_name = gr.Textbox(
                        value=display_name, visible=False
                    )
                    button.click(
                        fn=add_processor,
                        inputs=[processor_name, display_name, gr.State()],
                        outputs=[button],
                    )

            with gr.Column(scale=1):
                gr.Markdown("### Vision Processors")
                for model_name in vision_processors:
                    display_name = (
                        model_name.replace("processor", "")
                        .replace("_", " ")
                        .title()
                    )

                    button = gr.Button(display_name)
                    processor_name = gr.Textbox(
                        value=model_name, visible=False
                    )
                    display_name = gr.Textbox(
                        value=display_name, visible=False
                    )
                    button.click(
                        fn=add_processor,
                        inputs=[processor_name, display_name, gr.State()],
                        outputs=[button],
                    )


def forward(query, content, image, state):
    state["question"] = query
    ask_processors_output_info, state = ask_processors(
        query, content, image, state
    )
    uptree_competition_output_info, state = uptree_competition(state)
    ask_supervisor_output_info, state = ask_supervisor(state)

    ctm.downtree_broadcast(state["winning_output"])
    ctm.link_form(state["processor_output"])
    return (
        ask_processors_output_info,
        uptree_competition_output_info,
        ask_supervisor_output_info,
        state,
    )


def ask_processors(query, content, image, state):
    # Simulate processing here
    processor_output = ctm.ask_processors(
        question=query,
        context=content,
        image_path=None,
        audio_path=None,
        video_path=None,
    )
    output_info = ""
    for name, info in processor_output.items():
        output_info += f"{name}: {info['gist']}\n"
    state["processor_output"] = processor_output
    return output_info, state


def uptree_competition(state):
    winning_output = ctm.uptree_competition(state["processor_output"])
    state["winning_output"] = winning_output
    output_info = (
        "The winning processor is: {}\nThe winning gist is: {}\n".format(
            winning_output["name"], winning_output["gist"]
        )
    )
    return output_info, state


def ask_supervisor(state):
    question = state["question"]
    winning_output = state["winning_output"]
    answer, score = ctm.ask_supervisor(question, winning_output)
    output_info = f'The answer to the query "{question}" is: {answer}\nThe confidence for answering is: {score}\n'
    state["answer"] = answer
    state["score"] = score
    return output_info, state


def interface_tab():
    with gr.Blocks() as interface_tab:
        state = gr.State({})  # State to hold and pass values

        with gr.Column():
            # Inputs
            content = gr.Textbox(label="Enter your text here")
            query = gr.Textbox(label="Enter your query here")
            image = gr.Image(label="Upload your image")
            audio = gr.Audio(label="Upload or Record Audio")
            video = gr.Video(label="Upload or Record Video")

            # Processing buttons
            forward_button = gr.Button("Start CTM forward process")

            # Outputs
            processors_output = gr.Textbox(
                label="Processors Output", visible=True
            )
            competition_output = gr.Textbox(
                label="Up-tree Competition Output", visible=True
            )
            supervisor_output = gr.Textbox(
                label="Supervisor Output", visible=True
            )

        # Set up button to start or continue processing
        forward_button.click(
            fn=forward,
            inputs=[query, content, image, state],
            outputs=[
                processors_output,
                competition_output,
                supervisor_output,
                state,
            ],
        )

    return interface_tab


def main():
    with gr.Blocks(
        css="""#chat_container {height: 820px; width: 1000px; margin-left: auto; margin-right: auto;}
            #chatbot {height: 600px; overflow: auto;}
            #create_container {height: 750px; margin-left: 0px; margin-right: 0px;}
            #tokenizer_renderer span {white-space: pre-wrap}
        """
    ) as demo:
        with gr.Row():
            introduction()
        with gr.Row():
            processor_tab()
        with gr.Row():
            interface_tab()

    return demo


def start_demo():
    demo = main()
    if DEPLOYED:
        demo.queue(api_open=False).launch(show_api=False)
    else:
        demo.queue()
        demo.launch(share=False, server_name="0.0.0.0")


if __name__ == "__main__":
    start_demo()