Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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 |
-
|
23 |
-
# interleaved images
|
24 |
elif len(message.files) > 1:
|
25 |
-
|
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 |
-
|
41 |
|
42 |
if message.files is None:
|
43 |
-
gr.Error("You need to upload an image or
|
44 |
|
45 |
image_extensions = Image.registered_extensions()
|
46 |
image_extensions = tuple([ex for ex, f in image_extensions.items()])
|
47 |
-
if len(
|
48 |
-
image = Image.open(
|
49 |
-
prompt = f"{
|
50 |
|
51 |
-
elif len(
|
52 |
image_list = []
|
53 |
-
user_prompt =
|
54 |
|
55 |
-
for
|
56 |
-
|
|
|
57 |
|
58 |
toks = "<image>" * len(image_list)
|
59 |
prompt = user_prompt + toks
|
60 |
|
61 |
-
|
62 |
|
63 |
|
64 |
-
inputs = processor(prompt,
|
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)
|