Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -22,8 +22,6 @@ model = LlavaQwen2ForCausalLM.from_pretrained(
|
|
22 |
torch_dtype=torch.float16,
|
23 |
trust_remote_code=True)
|
24 |
|
25 |
-
model.to("cuda:0")
|
26 |
-
|
27 |
class KeywordsStoppingCriteria(StoppingCriteria):
|
28 |
def __init__(self, keywords, tokenizer, input_ids):
|
29 |
self.keywords = keywords
|
@@ -97,13 +95,13 @@ def bot_streaming(message, history):
|
|
97 |
tokenize=False,
|
98 |
add_generation_prompt=True)
|
99 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
100 |
-
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
101 |
stop_str = '<|im_end|>'
|
102 |
keywords = [stop_str]
|
103 |
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
|
104 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
105 |
|
106 |
-
image_tensor = model.process_images([image], model.config).to(
|
107 |
generation_kwargs = dict(input_ids=input_ids, images=image_tensor, streamer=streamer, max_new_tokens=100, stopping_criteria=[stopping_criteria])
|
108 |
generated_text = ""
|
109 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
|
|
22 |
torch_dtype=torch.float16,
|
23 |
trust_remote_code=True)
|
24 |
|
|
|
|
|
25 |
class KeywordsStoppingCriteria(StoppingCriteria):
|
26 |
def __init__(self, keywords, tokenizer, input_ids):
|
27 |
self.keywords = keywords
|
|
|
95 |
tokenize=False,
|
96 |
add_generation_prompt=True)
|
97 |
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
98 |
+
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
99 |
stop_str = '<|im_end|>'
|
100 |
keywords = [stop_str]
|
101 |
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
|
102 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
103 |
|
104 |
+
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
|
105 |
generation_kwargs = dict(input_ids=input_ids, images=image_tensor, streamer=streamer, max_new_tokens=100, stopping_criteria=[stopping_criteria])
|
106 |
generated_text = ""
|
107 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|