Haofei Yu
commited on
init version (#7)
Browse files- .gitignore +1 -0
- app.py +162 -4
.gitignore
CHANGED
@@ -158,3 +158,4 @@ cython_debug/
|
|
158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
#.idea/
|
|
|
|
158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
#.idea/
|
161 |
+
**/ctm/*
|
app.py
CHANGED
@@ -1,7 +1,165 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
from ctm.ctms.ctm_base import BaseConsciousnessTuringMachine
|
4 |
|
5 |
+
ctm = BaseConsciousnessTuringMachine()
|
6 |
+
ctm.add_processor("gpt4_text_emotion_processor", group_name="group_1")
|
7 |
+
ctm.add_processor("gpt4_text_summary_processor", group_name="group_1")
|
8 |
+
ctm.add_supervisor("gpt4_supervisor")
|
9 |
|
10 |
+
DEPLOYED = os.getenv("DEPLOYED", "true").lower() == "true"
|
11 |
+
|
12 |
+
def introduction():
|
13 |
+
with gr.Column(scale=2):
|
14 |
+
gr.Image(
|
15 |
+
"images/sotopia.jpg", elem_id="banner-image", show_label=False
|
16 |
+
)
|
17 |
+
with gr.Column(scale=5):
|
18 |
+
gr.Markdown(
|
19 |
+
"""Consciousness Turing Machine Demo
|
20 |
+
"""
|
21 |
+
)
|
22 |
+
|
23 |
+
def add_processor(processor_name):
|
24 |
+
print('add processor ', processor_name)
|
25 |
+
ctm.add_processor(processor_name)
|
26 |
+
print(len(ctm.processor_list))
|
27 |
+
|
28 |
+
def processor_tab():
|
29 |
+
with gr.Row() as row:
|
30 |
+
button1 = gr.Button("Text Emotion Analyzer")
|
31 |
+
button2 = gr.Button("Text Summary Generator")
|
32 |
+
|
33 |
+
invisible_input1 = gr.Textbox(
|
34 |
+
value="gpt4_text_emotion_processor",
|
35 |
+
visible=False
|
36 |
+
)
|
37 |
+
invisible_input2 = gr.Textbox(
|
38 |
+
value="gpt4_text_summary_processor",
|
39 |
+
visible=False
|
40 |
+
)
|
41 |
+
|
42 |
+
button1.click(
|
43 |
+
fn=add_processor,
|
44 |
+
inputs=[invisible_input1],
|
45 |
+
)
|
46 |
+
button2.click(
|
47 |
+
fn=add_processor,
|
48 |
+
inputs=[invisible_input2],
|
49 |
+
)
|
50 |
+
|
51 |
+
|
52 |
+
def forward(query, content, image, state):
|
53 |
+
state['question'] = query
|
54 |
+
ask_processors_output_info, state = ask_processors(query, content, image, state)
|
55 |
+
uptree_competition_output_info, state = uptree_competition(state)
|
56 |
+
ask_supervisor_output_info, state = ask_supervisor(state)
|
57 |
+
|
58 |
+
ctm.downtree_broadcast(state['winning_output'])
|
59 |
+
ctm.link_form(state['processor_output'])
|
60 |
+
return ask_processors_output_info, uptree_competition_output_info, ask_supervisor_output_info, state
|
61 |
+
|
62 |
+
|
63 |
+
def ask_processors(query, content, image, state):
|
64 |
+
# Simulate processing here
|
65 |
+
processor_output = ctm.ask_processors(
|
66 |
+
question=query,
|
67 |
+
context=content,
|
68 |
+
image_path=None,
|
69 |
+
audio_path=None,
|
70 |
+
video_path=None
|
71 |
+
)
|
72 |
+
output_info = ''
|
73 |
+
for name, info in processor_output.items():
|
74 |
+
output_info += f"{name}: {info['gist']}\n"
|
75 |
+
state['processor_output'] = processor_output
|
76 |
+
return output_info, state
|
77 |
+
|
78 |
+
|
79 |
+
def uptree_competition(state):
|
80 |
+
winning_output = ctm.uptree_competition(
|
81 |
+
state['processor_output']
|
82 |
+
)
|
83 |
+
state['winning_output'] = winning_output
|
84 |
+
output_info = 'The winning processor is: {}\nThe winning gist is: {}\n'.format(winning_output['name'], winning_output['gist'])
|
85 |
+
return output_info, state
|
86 |
+
|
87 |
+
|
88 |
+
def ask_supervisor(state):
|
89 |
+
question = state['question']
|
90 |
+
winning_output = state['winning_output']
|
91 |
+
answer, score = ctm.ask_supervisor(question, winning_output)
|
92 |
+
output_info = f"The answer to the query \"{question}\" is: {answer}\nThe confidence for answering is: {score}\n"
|
93 |
+
state['answer'] = answer
|
94 |
+
state['score'] = score
|
95 |
+
return output_info, state
|
96 |
+
|
97 |
+
|
98 |
+
def interface_tab():
|
99 |
+
with gr.Blocks() as interface_tab:
|
100 |
+
state = gr.State({}) # State to hold and pass values
|
101 |
+
|
102 |
+
with gr.Column():
|
103 |
+
# Inputs
|
104 |
+
content = gr.Textbox(label="Enter your text here")
|
105 |
+
query = gr.Textbox(label="Enter your query here")
|
106 |
+
image = gr.Image(label="Upload your image")
|
107 |
+
audio = gr.Audio(label="Upload or Record Audio")
|
108 |
+
video = gr.Video(label="Upload or Record Video")
|
109 |
+
|
110 |
+
# Processing buttons
|
111 |
+
forward_button = gr.Button("Start CTM forward process")
|
112 |
+
|
113 |
+
# Outputs
|
114 |
+
processors_output = gr.Textbox(
|
115 |
+
label="Processors Output",
|
116 |
+
visible=True
|
117 |
+
)
|
118 |
+
competition_output = gr.Textbox(
|
119 |
+
label="Up-tree Competition Output",
|
120 |
+
visible=True
|
121 |
+
)
|
122 |
+
supervisor_output = gr.Textbox(
|
123 |
+
label="Supervisor Output",
|
124 |
+
visible=True
|
125 |
+
)
|
126 |
+
|
127 |
+
# Set up button to start or continue processing
|
128 |
+
forward_button.click(
|
129 |
+
fn=forward,
|
130 |
+
inputs=[query, content, image, state],
|
131 |
+
outputs=[processors_output, competition_output, supervisor_output, state]
|
132 |
+
)
|
133 |
+
|
134 |
+
return interface_tab
|
135 |
+
|
136 |
+
|
137 |
+
def main():
|
138 |
+
with gr.Blocks(
|
139 |
+
css="""#chat_container {height: 820px; width: 1000px; margin-left: auto; margin-right: auto;}
|
140 |
+
#chatbot {height: 600px; overflow: auto;}
|
141 |
+
#create_container {height: 750px; margin-left: 0px; margin-right: 0px;}
|
142 |
+
#tokenizer_renderer span {white-space: pre-wrap}
|
143 |
+
"""
|
144 |
+
) as demo:
|
145 |
+
with gr.Row():
|
146 |
+
introduction()
|
147 |
+
with gr.Row():
|
148 |
+
processor_tab()
|
149 |
+
with gr.Row():
|
150 |
+
interface_tab()
|
151 |
+
|
152 |
+
return demo
|
153 |
+
|
154 |
+
|
155 |
+
def start_demo():
|
156 |
+
demo = main()
|
157 |
+
if DEPLOYED:
|
158 |
+
demo.queue(api_open=False).launch(show_api=False)
|
159 |
+
else:
|
160 |
+
demo.queue()
|
161 |
+
demo.launch(share=False, server_name="0.0.0.0")
|
162 |
+
|
163 |
+
|
164 |
+
if __name__ == "__main__":
|
165 |
+
start_demo()
|