merve HF staff commited on
Commit
ece2105
1 Parent(s): 9fb5714

Handle history

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -47,11 +47,20 @@ def bot_streaming(message, history):
47
  elif len(message.files) > 1:
48
  image = [msg.path for msg in message.files]
49
  else:
50
- # if there's no image uploaded for this turn, look for images in the past turns
51
- # kept inside tuples, take the last one
52
- for hist in history:
53
- if type(hist[0])==tuple:
54
- image = hist[0][0]
 
 
 
 
 
 
 
 
 
55
 
56
  if message.files is None:
57
  gr.Error("You need to upload an image or video for LLaVA to work.")
 
47
  elif len(message.files) > 1:
48
  image = [msg.path for msg in message.files]
49
  else:
50
+
51
+ def has_file_data(lst):
52
+ return any(isinstance(item, FileData) for sublist in lst if isinstance(sublist, tuple) for item in sublist)
53
+
54
+ def extract_paths(lst):
55
+ return [item.path for sublist in lst if isinstance(sublist, tuple) for item in sublist if isinstance(item, FileData)]
56
+
57
+ latest_text_only_index = -1
58
+
59
+ for i, item in enumerate(history):
60
+ if all(isinstance(sub_item, str) for sub_item in item):
61
+ latest_text_only_index = i
62
+
63
+ image = [path for i, item in enumerate(history) if i < latest_text_only_index and has_file_data(item) for path in extract_paths(item)]
64
 
65
  if message.files is None:
66
  gr.Error("You need to upload an image or video for LLaVA to work.")