dai commited on
Commit
0a431d7
·
1 Parent(s): 35015b6

add modifiers. change default LM. change UI

Browse files
Files changed (1) hide show
  1. app.py +94 -72
app.py CHANGED
@@ -17,15 +17,18 @@ from consts import ALL_BLOCKS, ALL_BOWLS
17
  from md_logger import MarkdownLogger
18
 
19
  default_open_ai_key = os.getenv('OPEN_AI_SECRET')
 
 
20
 
21
- class DemoRunner:
 
22
 
23
  def __init__(self):
24
  self._cfg = OmegaConf.to_container(OmegaConf.load('cfg.yaml'), resolve=True)
25
  self._env = None
26
  self._model_name = ''
27
  self._md_logger = MarkdownLogger()
28
- self._temp_file_manager = processing_utils.TempFileManager()
29
 
30
  def make_LMP(self, env):
31
  # LMP env wrapper
@@ -114,7 +117,7 @@ class DemoRunner:
114
  for message in self._md_logger.get_messages():
115
  history.append((None, message))
116
  if self._env.cache_video:
117
- temp_name = self._temp_file_manager.make_temp_copy_if_needed(video_file_name)
118
  history.append((None, (temp_name, )))
119
 
120
  return self._md_logger.get_log(), self._env.get_camera_image(), video_file_name, history
@@ -130,7 +133,7 @@ def setup(api_key, model_name, n_blocks, n_bowls):
130
 
131
  info, img = demo_runner.setup(api_key, model_name, n_blocks, n_bowls)
132
  welcome_message = 'How can I help you?'
133
- return info, img, demo_runner, [(None, welcome_message)]
134
 
135
 
136
  def run(demo_runner, chat_history):
@@ -143,74 +146,93 @@ def submit_chat(chat_message, history):
143
  history += [[chat_message, None]]
144
  return '', history
145
 
146
- if __name__ == '__main__':
147
- with open('README.md', 'r') as f:
148
- for _ in range(12):
149
- next(f)
150
- readme_text = f.read()
151
-
152
- with gr.Blocks() as demo:
153
- state = gr.State(None)
154
-
155
- gr.Markdown(readme_text)
156
- gr.Markdown('# Interactive Demo')
157
- with gr.Row():
158
- with gr.Column():
159
- with gr.Row():
160
- inp_api_key = gr.Textbox(value=default_open_ai_key,
161
- label='OpenAI API Key (this is not stored anywhere)', lines=1)
162
- inp_model_name = gr.Dropdown(label='Model Name', choices=['code-davinci-002', 'text-davinci-002'], value='code-davinci-002')
163
- with gr.Row():
164
- inp_n_blocks = gr.Slider(label='Number of Blocks', minimum=0, maximum=5, value=3, step=1)
165
- inp_n_bowls = gr.Slider(label='Number of Bowls', minimum=0, maximum=5, value=3, step=1)
166
-
167
- btn_setup = gr.Button("Setup/Reset Simulation")
168
- info_setup = gr.Markdown(label='Setup Info')
169
- with gr.Column():
170
- img_setup = gr.Image(label='Current Simulation State')
171
-
172
- with gr.Row():
173
- with gr.Column():
174
- chat_box = gr.Chatbot()
175
- inp_instruction = gr.Textbox(label='Instruction', lines=1)
176
- examples = gr.Examples(
177
- [
178
- 'stack two of the blocks',
179
- 'what color is the rightmost block?',
180
- 'arrange the blocks into figure 3',
181
- 'put blocks into non-matching bowls'
182
- ],
183
- inp_instruction,
184
- )
185
- btn_run = gr.Button("Run (this may take 30+ seconds)")
186
- info_run = gr.Markdown(label='Generated Code')
187
- with gr.Column():
188
- video_run = gr.Video(label='Video of Last Instruction')
189
-
190
- btn_setup.click(
191
- setup,
192
- inputs=[inp_api_key, inp_model_name, inp_n_blocks, inp_n_bowls],
193
- outputs=[info_setup, img_setup, state, chat_box],
194
- )
195
- btn_run.click(
196
- submit_chat,
197
- [inp_instruction, chat_box],
198
- [inp_instruction, chat_box],
199
- ).then(
200
- run,
201
- inputs=[state, chat_box],
202
- outputs=[info_run, img_setup, video_run, chat_box, inp_instruction],
203
- )
204
- inp_instruction.submit(
205
- submit_chat,
206
- [inp_instruction, chat_box],
207
- [inp_instruction, chat_box],
208
- ).then(
209
- run,
210
- inputs=[state, chat_box],
211
- outputs=[info_run, img_setup, video_run,
212
- chat_box, inp_instruction],
213
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
 
215
  demo.queue()
216
  demo.launch()
 
17
  from md_logger import MarkdownLogger
18
 
19
  default_open_ai_key = os.getenv('OPEN_AI_SECRET')
20
+ chain_of_thought_affix = ' with a step by step explanation'
21
+ ask_for_clarification_affix = ' or ask for clarification if you feel unclear'
22
 
23
+
24
+ class DemoRunner(processing_utils.TempFileManager):
25
 
26
  def __init__(self):
27
  self._cfg = OmegaConf.to_container(OmegaConf.load('cfg.yaml'), resolve=True)
28
  self._env = None
29
  self._model_name = ''
30
  self._md_logger = MarkdownLogger()
31
+ processing_utils.TempFileManager.__init__(self)
32
 
33
  def make_LMP(self, env):
34
  # LMP env wrapper
 
117
  for message in self._md_logger.get_messages():
118
  history.append((None, message))
119
  if self._env.cache_video:
120
+ temp_name = self.make_temp_copy_if_needed(video_file_name)
121
  history.append((None, (temp_name, )))
122
 
123
  return self._md_logger.get_log(), self._env.get_camera_image(), video_file_name, history
 
133
 
134
  info, img = demo_runner.setup(api_key, model_name, n_blocks, n_bowls)
135
  welcome_message = 'How can I help you?'
136
+ return info, img, demo_runner, [(None, welcome_message)], None
137
 
138
 
139
  def run(demo_runner, chat_history):
 
146
  history += [[chat_message, None]]
147
  return '', history
148
 
149
+ def add_cot(chat_messsage):
150
+ return chat_messsage.strip() + chain_of_thought_affix
151
+
152
+ def add_clarification(chat_message):
153
+ return chat_message.strip() + ask_for_clarification_affix
154
+
155
+
156
+ with open('README.md', 'r') as f:
157
+ for _ in range(12):
158
+ next(f)
159
+ readme_text = f.read()
160
+
161
+ with gr.Blocks() as demo:
162
+ state = gr.State(None)
163
+
164
+ gr.Markdown(readme_text)
165
+ gr.Markdown('# Interactive Demo')
166
+ with gr.Row():
167
+ with gr.Column():
168
+ with gr.Row():
169
+ inp_api_key = gr.Textbox(value=default_open_ai_key,
170
+ label='OpenAI API Key (this is not stored anywhere)', lines=1)
171
+ inp_model_name = gr.Dropdown(label='Model Name', choices=['code-davinci-002', 'text-davinci-003', 'text-davinci-002'], value='text-davinci-003')
172
+ with gr.Row():
173
+ inp_n_blocks = gr.Slider(label='Number of Blocks', minimum=0, maximum=5, value=3, step=1)
174
+ inp_n_bowls = gr.Slider(label='Number of Bowls', minimum=0, maximum=5, value=3, step=1)
175
+
176
+ btn_setup = gr.Button("Setup/Reset Simulation")
177
+ info_setup = gr.Markdown(label='Setup Info')
178
+
179
+ with gr.Row():
180
+ with gr.Column():
181
+ chat_box = gr.Chatbot()
182
+ inp_instruction = gr.Textbox(label='Instruction', lines=1)
183
+ examples = gr.Examples(
184
+ [
185
+ 'stack two of the blocks',
186
+ 'what color is the rightmost block?',
187
+ 'arrange the blocks into figure 3',
188
+ 'put blocks into non-matching bowls',
189
+ 'swap the positions of one block and another',
190
+ ],
191
+ inp_instruction,
192
+ )
193
+ btn_add_cot = gr.Button(f'+{chain_of_thought_affix} (chain-of-thought)')
194
+ btn_add_cla = gr.Button(
195
+ f'+{ask_for_clarification_affix} (conversation)')
196
+ btn_run = gr.Button("Run (this may take 30+ seconds)")
197
+ info_run = gr.Markdown(label='Generated Code')
198
+ with gr.Column():
199
+ img_setup = gr.Image(label='Current Simulation State')
200
+ video_run = gr.Video(label='Most Recent Manipulation')
201
+
202
+ btn_setup.click(
203
+ setup,
204
+ inputs=[inp_api_key, inp_model_name, inp_n_blocks, inp_n_bowls],
205
+ outputs=[info_setup, img_setup, state, chat_box, video_run],
206
+ )
207
+ btn_add_cot.click(
208
+ add_cot,
209
+ inp_instruction,
210
+ inp_instruction,
211
+ )
212
+ btn_add_cla.click(
213
+ add_clarification,
214
+ inp_instruction,
215
+ inp_instruction,
216
+ )
217
+ btn_run.click(
218
+ submit_chat,
219
+ [inp_instruction, chat_box],
220
+ [inp_instruction, chat_box],
221
+ ).then(
222
+ run,
223
+ inputs=[state, chat_box],
224
+ outputs=[info_run, img_setup, video_run, chat_box, inp_instruction],
225
+ )
226
+ inp_instruction.submit(
227
+ submit_chat,
228
+ [inp_instruction, chat_box],
229
+ [inp_instruction, chat_box],
230
+ ).then(
231
+ run,
232
+ inputs=[state, chat_box],
233
+ outputs=[info_run, img_setup, video_run, chat_box, inp_instruction],
234
+ )
235
 
236
+ if __name__ == '__main__':
237
  demo.queue()
238
  demo.launch()