merve HF staff commited on
Commit
80fa0a9
1 Parent(s): 831190e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -16,13 +16,14 @@ model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b
16
  def bot_streaming(message, history):
17
 
18
  txt = message.text
 
19
 
20
  if message.files:
21
  if len(message.files) == 1:
22
- img = [message.files[0].path]
23
- # interleaved images
24
  elif len(message.files) > 1:
25
- img = [msg.path for msg in message.files]
26
  else:
27
 
28
  def has_file_data(lst):
@@ -37,31 +38,32 @@ def bot_streaming(message, history):
37
  if all(isinstance(sub_item, str) for sub_item in item):
38
  latest_text_only_index = i
39
 
40
- img = [path for i, item in enumerate(history) if i < latest_text_only_index and has_file_data(item) for path in extract_paths(item)]
41
 
42
  if message.files is None:
43
- gr.Error("You need to upload an image or multiple images at least once for LLaVA to work.")
44
 
45
  image_extensions = Image.registered_extensions()
46
  image_extensions = tuple([ex for ex, f in image_extensions.items()])
47
- if len(img) == 1:
48
- image = Image.open(img[0]).convert("RGB")
49
- prompt = f"{txt}<image>"
50
 
51
- elif len(img) > 1:
52
  image_list = []
53
- user_prompt = txt
54
 
55
- for im in img:
56
- image_list.append(Image.open(im).convert("RGB"))
 
57
 
58
  toks = "<image>" * len(image_list)
59
  prompt = user_prompt + toks
60
 
61
- img = image_list
62
 
63
 
64
- inputs = processor(prompt, img, return_tensors="pt").to("cuda", torch.float16)
65
  streamer = TextIteratorStreamer(processor, {"skip_special_tokens": True})
66
  generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=250)
67
  generated_text = ""
@@ -76,7 +78,7 @@ def bot_streaming(message, history):
76
 
77
  buffer += new_text
78
 
79
- generated_text_without_prompt = buffer
80
  time.sleep(0.01)
81
  yield buffer
82
 
@@ -89,4 +91,4 @@ demo = gr.ChatInterface(fn=bot_streaming, title="Chameleon 🦎", examples=[
89
  textbox=gr.MultimodalTextbox(file_count="multiple"),
90
  description="Try [Chameleon-7B](https://huggingface.co/facebook/chameleon-7b) by Meta with transformers in this demo. Upload image(s), and start chatting about it, or simply try one of the examples below. If you don't upload an image, you will receive an error. ",
91
  stop_btn="Stop Generation", multimodal=True)
92
- demo.launch(debug=True)
 
16
  def bot_streaming(message, history):
17
 
18
  txt = message.text
19
+ ext_buffer = f"{txt}"
20
 
21
  if message.files:
22
  if len(message.files) == 1:
23
+ image = [message.files[0].path]
24
+ # interleaved images or video
25
  elif len(message.files) > 1:
26
+ image = [msg.path for msg in message.files]
27
  else:
28
 
29
  def has_file_data(lst):
 
38
  if all(isinstance(sub_item, str) for sub_item in item):
39
  latest_text_only_index = i
40
 
41
+ 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)]
42
 
43
  if message.files is None:
44
+ gr.Error("You need to upload an image or video for LLaVA to work.")
45
 
46
  image_extensions = Image.registered_extensions()
47
  image_extensions = tuple([ex for ex, f in image_extensions.items()])
48
+ if len(image) == 1:
49
+ image = Image.open(image[0]).convert("RGB")
50
+ prompt = f"{message.text}<image>"
51
 
52
+ elif len(image) > 1:
53
  image_list = []
54
+ user_prompt = message.text
55
 
56
+ for img in image:
57
+ img = Image.open(img).convert("RGB")
58
+ image_list.append(img)
59
 
60
  toks = "<image>" * len(image_list)
61
  prompt = user_prompt + toks
62
 
63
+ image = image_list
64
 
65
 
66
+ inputs = processor(prompt, image, return_tensors="pt").to("cuda", torch.float16)
67
  streamer = TextIteratorStreamer(processor, {"skip_special_tokens": True})
68
  generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=250)
69
  generated_text = ""
 
78
 
79
  buffer += new_text
80
 
81
+ generated_text_without_prompt = buffer#[len(ext_buffer):]
82
  time.sleep(0.01)
83
  yield buffer
84
 
 
91
  textbox=gr.MultimodalTextbox(file_count="multiple"),
92
  description="Try [Chameleon-7B](https://huggingface.co/facebook/chameleon-7b) by Meta with transformers in this demo. Upload image(s), and start chatting about it, or simply try one of the examples below. If you don't upload an image, you will receive an error. ",
93
  stop_btn="Stop Generation", multimodal=True)
94
+ demo.launch(debug=True)