ClownRat commited on
Commit
7e442ac
1 Parent(s): 8c55b8f

Update demo.

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -130,7 +130,7 @@ def save_video_to_local(video_path):
130
 
131
 
132
  @spaces.GPU(duration=120)
133
- def generate(image, video, first_run, state, state_, textbox_in, tensor, modals, dtype=torch.float16):
134
  flag = 1
135
  if not textbox_in:
136
  if len(state_.messages) > 0:
@@ -144,11 +144,12 @@ def generate(image, video, first_run, state, state_, textbox_in, tensor, modals,
144
  video = video if video else "none"
145
  assert not (os.path.exists(image) and os.path.exists(video))
146
 
 
 
 
147
  if type(state) is not Conversation:
148
  state = conv_templates[conv_mode].copy()
149
  state_ = conv_templates[conv_mode].copy()
150
- tensor = []
151
- modals = []
152
 
153
  first_run = False if len(state.messages) > 0 else True
154
 
@@ -192,29 +193,25 @@ def generate(image, video, first_run, state, state_, textbox_in, tensor, modals,
192
  state.append_message(state.roles[1], textbox_out)
193
 
194
  return (gr.update(value=image if os.path.exists(image) else None, interactive=True), gr.update(value=video if os.path.exists(video) else None, interactive=True),
195
- state.to_gradio_chatbot(), False, state, state_, gr.update(value=None, interactive=True),
196
- # tensor, modals
197
- )
198
 
199
 
200
- def regenerate(state, state_, textbox, tensor, modals):
201
  state.messages.pop(-1)
202
  state_.messages.pop(-1)
203
- tensor.pop(-1)
204
- modals.pop(-1)
205
  textbox = gr.update(value=None, interactive=True)
206
  if len(state.messages) > 0:
207
- return state, state_, textbox, state.to_gradio_chatbot(), False, tensor, modals
208
- return state, state_, textbox, state.to_gradio_chatbot(), True, tensor, modals
209
 
210
 
211
- def clear_history(state, state_, tensor, modals):
212
  state = conv_templates[conv_mode].copy()
213
  state_ = conv_templates[conv_mode].copy()
214
  return (gr.update(value=None, interactive=True),
215
  gr.update(value=None, interactive=True), \
216
  state.to_gradio_chatbot(), \
217
- True, state, state_, gr.update(value=None, interactive=True), [], [])
218
 
219
 
220
  conv_mode = "llama_2"
@@ -261,8 +258,8 @@ with gr.Blocks(title='VideoLLaMA2🚀', theme=gr.themes.Default(), css=block_css
261
  state = gr.State()
262
  state_ = gr.State()
263
  first_run = gr.State()
264
- tensor = gr.State()
265
- modals = gr.State()
266
 
267
  with gr.Row():
268
  with gr.Column(scale=3):
@@ -306,15 +303,24 @@ with gr.Blocks(title='VideoLLaMA2🚀', theme=gr.themes.Default(), css=block_css
306
  gr.Markdown(tos_markdown)
307
  gr.Markdown(learn_more_markdown)
308
 
309
- submit_btn.click(generate, [image, video, first_run, state, state_, textbox, tensor, modals],
310
- [image, video, chatbot, first_run, state, state_, textbox,
 
 
311
  # tensor, modals
312
  ])
313
 
314
- regenerate_btn.click(regenerate, [state, state_, textbox, tensor, modals], [state, state_, textbox, chatbot, first_run, tensor, modals]).then(
315
- generate, [image, video, first_run, state, state_, textbox, tensor, modals], [image, video, chatbot, first_run, state, state_, textbox, tensor, modals])
316
-
317
- clear_btn.click(clear_history, [state, state_, tensor, modals],
318
- [image, video, chatbot, first_run, state, state_, textbox, tensor, modals])
 
 
 
 
 
 
 
319
 
320
  demo.launch()
 
130
 
131
 
132
  @spaces.GPU(duration=120)
133
+ def generate(image, video, first_run, state, state_, textbox_in, dtype=torch.float16):
134
  flag = 1
135
  if not textbox_in:
136
  if len(state_.messages) > 0:
 
144
  video = video if video else "none"
145
  assert not (os.path.exists(image) and os.path.exists(video))
146
 
147
+ tensor = []
148
+ modals = []
149
+
150
  if type(state) is not Conversation:
151
  state = conv_templates[conv_mode].copy()
152
  state_ = conv_templates[conv_mode].copy()
 
 
153
 
154
  first_run = False if len(state.messages) > 0 else True
155
 
 
193
  state.append_message(state.roles[1], textbox_out)
194
 
195
  return (gr.update(value=image if os.path.exists(image) else None, interactive=True), gr.update(value=video if os.path.exists(video) else None, interactive=True),
196
+ state.to_gradio_chatbot(), False, state, state_, gr.update(value=None, interactive=True))
 
 
197
 
198
 
199
+ def regenerate(state, state_, textbox):
200
  state.messages.pop(-1)
201
  state_.messages.pop(-1)
 
 
202
  textbox = gr.update(value=None, interactive=True)
203
  if len(state.messages) > 0:
204
+ return state, state_, textbox, state.to_gradio_chatbot(), False
205
+ return state, state_, textbox, state.to_gradio_chatbot(), True
206
 
207
 
208
+ def clear_history(state, state_):
209
  state = conv_templates[conv_mode].copy()
210
  state_ = conv_templates[conv_mode].copy()
211
  return (gr.update(value=None, interactive=True),
212
  gr.update(value=None, interactive=True), \
213
  state.to_gradio_chatbot(), \
214
+ True, state, state_, gr.update(value=None, interactive=True))
215
 
216
 
217
  conv_mode = "llama_2"
 
258
  state = gr.State()
259
  state_ = gr.State()
260
  first_run = gr.State()
261
+ # tensor = gr.State()
262
+ # modals = gr.State()
263
 
264
  with gr.Row():
265
  with gr.Column(scale=3):
 
303
  gr.Markdown(tos_markdown)
304
  gr.Markdown(learn_more_markdown)
305
 
306
+ submit_btn.click(
307
+ generate,
308
+ [image, video, first_run, state, state_, textbox],
309
+ [image, video, chatbot, first_run, state, state_, textbox,
310
  # tensor, modals
311
  ])
312
 
313
+ regenerate_btn.click(
314
+ regenerate,
315
+ [state, state_, textbox],
316
+ [state, state_, textbox, chatbot, first_run]).then(
317
+ generate,
318
+ [image, video, first_run, state, state_, textbox],
319
+ [image, video, chatbot, first_run, state, state_, textbox])
320
+
321
+ clear_btn.click(
322
+ clear_history,
323
+ [state, state_],
324
+ [image, video, chatbot, first_run, state, state_, textbox])
325
 
326
  demo.launch()