Haofei Yu commited on
Commit
1510f49
1 Parent(s): 3a62692

support pre-commit (#10)

Browse files
Files changed (3) hide show
  1. .gitignore +1 -1
  2. .pre-commit-config.yaml +27 -0
  3. app.py +73 -45
.gitignore CHANGED
@@ -157,4 +157,4 @@ cython_debug/
157
  # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
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/
 
157
  # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
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/
.pre-commit-config.yaml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v3.2.0
4
+ hooks:
5
+ - id: trailing-whitespace
6
+ - id: end-of-file-fixer
7
+ - id: check-yaml
8
+ - id: check-added-large-files
9
+ - repo: https://github.com/pre-commit/mirrors-prettier
10
+ rev: v3.0.1 # Use the sha / tag you want to point at
11
+ hooks:
12
+ - id: prettier
13
+ types_or: [html]
14
+ - repo: https://github.com/psf/black
15
+ rev: 22.12.0
16
+ hooks:
17
+ - id: black
18
+ args: [--line-length=79]
19
+ - repo: https://github.com/pycqa/isort
20
+ rev: 5.12.0
21
+ hooks:
22
+ - id: isort
23
+ args: ["--profile", "black", --line-length=72]
24
+ - repo: https://github.com/kynan/nbstripout
25
+ rev: 0.6.0
26
+ hooks:
27
+ - id: nbstripout
app.py CHANGED
@@ -1,16 +1,17 @@
1
  import os
2
- import gradio as gr
3
  import sys
4
- sys.path.append('../CTM/')
 
 
 
5
  from ctm.ctms.ctm_base import BaseConsciousnessTuringMachine
6
 
7
  ctm = BaseConsciousnessTuringMachine()
8
- ctm.add_processor("gpt4_text_emotion_processor", group_name="group_1")
9
- ctm.add_processor("gpt4_text_summary_processor", group_name="group_1")
10
  ctm.add_supervisor("gpt4_supervisor")
11
 
12
  DEPLOYED = os.getenv("DEPLOYED", "true").lower() == "true"
13
 
 
14
  def introduction():
15
  with gr.Column(scale=2):
16
  gr.Image(
@@ -22,11 +23,13 @@ def introduction():
22
  """
23
  )
24
 
 
25
  def add_processor(processor_name, display_name, state):
26
- print('add processor ', processor_name)
27
  ctm.add_processor(processor_name)
28
  print(len(ctm.processor_list))
29
- return display_name + ' (added)'
 
30
 
31
  def processor_tab():
32
  # Categorized model names
@@ -34,14 +37,14 @@ def processor_tab():
34
  "gpt4_text_emotion_processor",
35
  "gpt4_text_summary_processor",
36
  "gpt4_speaker_intent_processor",
37
- "roberta_text_sentiment_processor"
38
  ]
39
  vision_processors = [
40
  "gpt4v_cloth_fashion_processor",
41
  "gpt4v_face_emotion_processor",
42
  "gpt4v_ocr_processor",
43
  "gpt4v_posture",
44
- "gpt4v_scene_location_processor"
45
  ]
46
 
47
  with gr.Blocks():
@@ -49,77 +52,100 @@ def processor_tab():
49
  with gr.Column(scale=1):
50
  gr.Markdown("### Text Processors")
51
  for model_name in text_processors:
52
- display_name = model_name.replace("processor", "").replace("_", " ").title()
 
 
 
 
53
 
54
  button = gr.Button(display_name)
55
- processor_name = gr.Textbox(value=model_name, visible=False)
56
- display_name = gr.Textbox(value=display_name, visible=False)
 
 
 
 
57
  button.click(
58
  fn=add_processor,
59
  inputs=[processor_name, display_name, gr.State()],
60
- outputs=[button]
61
  )
62
 
63
  with gr.Column(scale=1):
64
  gr.Markdown("### Vision Processors")
65
  for model_name in vision_processors:
66
- display_name = model_name.replace("processor", "").replace("_", " ").title()
 
 
 
 
67
 
68
  button = gr.Button(display_name)
69
- processor_name = gr.Textbox(value=model_name, visible=False)
70
- display_name = gr.Textbox(value=display_name, visible=False)
 
 
 
 
71
  button.click(
72
  fn=add_processor,
73
  inputs=[processor_name, display_name, gr.State()],
74
- outputs=[button]
75
  )
76
 
77
 
78
-
79
-
80
  def forward(query, content, image, state):
81
- state['question'] = query
82
- ask_processors_output_info, state = ask_processors(query, content, image, state)
 
 
83
  uptree_competition_output_info, state = uptree_competition(state)
84
  ask_supervisor_output_info, state = ask_supervisor(state)
85
 
86
- ctm.downtree_broadcast(state['winning_output'])
87
- ctm.link_form(state['processor_output'])
88
- return ask_processors_output_info, uptree_competition_output_info, ask_supervisor_output_info, state
 
 
 
 
 
89
 
90
 
91
  def ask_processors(query, content, image, state):
92
  # Simulate processing here
93
  processor_output = ctm.ask_processors(
94
- question=query,
95
- context=content,
96
  image_path=None,
97
  audio_path=None,
98
- video_path=None
99
  )
100
- output_info = ''
101
  for name, info in processor_output.items():
102
  output_info += f"{name}: {info['gist']}\n"
103
- state['processor_output'] = processor_output
104
  return output_info, state
105
 
106
 
107
  def uptree_competition(state):
108
- winning_output = ctm.uptree_competition(
109
- state['processor_output']
 
 
 
 
110
  )
111
- state['winning_output'] = winning_output
112
- output_info = 'The winning processor is: {}\nThe winning gist is: {}\n'.format(winning_output['name'], winning_output['gist'])
113
  return output_info, state
114
 
115
 
116
  def ask_supervisor(state):
117
- question = state['question']
118
- winning_output = state['winning_output']
119
  answer, score = ctm.ask_supervisor(question, winning_output)
120
- output_info = f"The answer to the query \"{question}\" is: {answer}\nThe confidence for answering is: {score}\n"
121
- state['answer'] = answer
122
- state['score'] = score
123
  return output_info, state
124
 
125
 
@@ -140,23 +166,25 @@ def interface_tab():
140
 
141
  # Outputs
142
  processors_output = gr.Textbox(
143
- label="Processors Output",
144
- visible=True
145
  )
146
  competition_output = gr.Textbox(
147
- label="Up-tree Competition Output",
148
- visible=True
149
  )
150
  supervisor_output = gr.Textbox(
151
- label="Supervisor Output",
152
- visible=True
153
  )
154
 
155
  # Set up button to start or continue processing
156
  forward_button.click(
157
  fn=forward,
158
  inputs=[query, content, image, state],
159
- outputs=[processors_output, competition_output, supervisor_output, state]
 
 
 
 
 
160
  )
161
 
162
  return interface_tab
@@ -190,4 +218,4 @@ def start_demo():
190
 
191
 
192
  if __name__ == "__main__":
193
- start_demo()
 
1
  import os
 
2
  import sys
3
+
4
+ import gradio as gr
5
+
6
+ sys.path.append("../CTM/")
7
  from ctm.ctms.ctm_base import BaseConsciousnessTuringMachine
8
 
9
  ctm = BaseConsciousnessTuringMachine()
 
 
10
  ctm.add_supervisor("gpt4_supervisor")
11
 
12
  DEPLOYED = os.getenv("DEPLOYED", "true").lower() == "true"
13
 
14
+
15
  def introduction():
16
  with gr.Column(scale=2):
17
  gr.Image(
 
23
  """
24
  )
25
 
26
+
27
  def add_processor(processor_name, display_name, state):
28
+ print("add processor ", processor_name)
29
  ctm.add_processor(processor_name)
30
  print(len(ctm.processor_list))
31
+ return display_name + " (added)"
32
+
33
 
34
  def processor_tab():
35
  # Categorized model names
 
37
  "gpt4_text_emotion_processor",
38
  "gpt4_text_summary_processor",
39
  "gpt4_speaker_intent_processor",
40
+ "roberta_text_sentiment_processor",
41
  ]
42
  vision_processors = [
43
  "gpt4v_cloth_fashion_processor",
44
  "gpt4v_face_emotion_processor",
45
  "gpt4v_ocr_processor",
46
  "gpt4v_posture",
47
+ "gpt4v_scene_location_processor",
48
  ]
49
 
50
  with gr.Blocks():
 
52
  with gr.Column(scale=1):
53
  gr.Markdown("### Text Processors")
54
  for model_name in text_processors:
55
+ display_name = (
56
+ model_name.replace("processor", "")
57
+ .replace("_", " ")
58
+ .title()
59
+ )
60
 
61
  button = gr.Button(display_name)
62
+ processor_name = gr.Textbox(
63
+ value=model_name, visible=False
64
+ )
65
+ display_name = gr.Textbox(
66
+ value=display_name, visible=False
67
+ )
68
  button.click(
69
  fn=add_processor,
70
  inputs=[processor_name, display_name, gr.State()],
71
+ outputs=[button],
72
  )
73
 
74
  with gr.Column(scale=1):
75
  gr.Markdown("### Vision Processors")
76
  for model_name in vision_processors:
77
+ display_name = (
78
+ model_name.replace("processor", "")
79
+ .replace("_", " ")
80
+ .title()
81
+ )
82
 
83
  button = gr.Button(display_name)
84
+ processor_name = gr.Textbox(
85
+ value=model_name, visible=False
86
+ )
87
+ display_name = gr.Textbox(
88
+ value=display_name, visible=False
89
+ )
90
  button.click(
91
  fn=add_processor,
92
  inputs=[processor_name, display_name, gr.State()],
93
+ outputs=[button],
94
  )
95
 
96
 
 
 
97
  def forward(query, content, image, state):
98
+ state["question"] = query
99
+ ask_processors_output_info, state = ask_processors(
100
+ query, content, image, state
101
+ )
102
  uptree_competition_output_info, state = uptree_competition(state)
103
  ask_supervisor_output_info, state = ask_supervisor(state)
104
 
105
+ ctm.downtree_broadcast(state["winning_output"])
106
+ ctm.link_form(state["processor_output"])
107
+ return (
108
+ ask_processors_output_info,
109
+ uptree_competition_output_info,
110
+ ask_supervisor_output_info,
111
+ state,
112
+ )
113
 
114
 
115
  def ask_processors(query, content, image, state):
116
  # Simulate processing here
117
  processor_output = ctm.ask_processors(
118
+ question=query,
119
+ context=content,
120
  image_path=None,
121
  audio_path=None,
122
+ video_path=None,
123
  )
124
+ output_info = ""
125
  for name, info in processor_output.items():
126
  output_info += f"{name}: {info['gist']}\n"
127
+ state["processor_output"] = processor_output
128
  return output_info, state
129
 
130
 
131
  def uptree_competition(state):
132
+ winning_output = ctm.uptree_competition(state["processor_output"])
133
+ state["winning_output"] = winning_output
134
+ output_info = (
135
+ "The winning processor is: {}\nThe winning gist is: {}\n".format(
136
+ winning_output["name"], winning_output["gist"]
137
+ )
138
  )
 
 
139
  return output_info, state
140
 
141
 
142
  def ask_supervisor(state):
143
+ question = state["question"]
144
+ winning_output = state["winning_output"]
145
  answer, score = ctm.ask_supervisor(question, winning_output)
146
+ output_info = f'The answer to the query "{question}" is: {answer}\nThe confidence for answering is: {score}\n'
147
+ state["answer"] = answer
148
+ state["score"] = score
149
  return output_info, state
150
 
151
 
 
166
 
167
  # Outputs
168
  processors_output = gr.Textbox(
169
+ label="Processors Output", visible=True
 
170
  )
171
  competition_output = gr.Textbox(
172
+ label="Up-tree Competition Output", visible=True
 
173
  )
174
  supervisor_output = gr.Textbox(
175
+ label="Supervisor Output", visible=True
 
176
  )
177
 
178
  # Set up button to start or continue processing
179
  forward_button.click(
180
  fn=forward,
181
  inputs=[query, content, image, state],
182
+ outputs=[
183
+ processors_output,
184
+ competition_output,
185
+ supervisor_output,
186
+ state,
187
+ ],
188
  )
189
 
190
  return interface_tab
 
218
 
219
 
220
  if __name__ == "__main__":
221
+ start_demo()